Using SolrJ with Java Objects

Last time we talked about setting up our Solr environment for the NixMashup Links-to-Posts application I’m building. Today we’re going to look at entering and searching Solr with the SolrJ API and our Java NixMashupLink object.

For review let’s look at the fields representing our indexed NixMashupLink object as defined in our Solr NixMashup Collection’s schema.xml.

Now a look at our POJO, the NixMashupLink Java Object. You’ll notice we’re importing our Apache Solrj Client Bean as well as using a @Field annotation to map the individual fields to their Solr counterparts.

Entering Records With SolrJ

We can enter multiple records into Solr but for our purposes today we will demonstrate entering a single record with our populated NixMashupLink object. As you can see from the handling of the Tags and Link DateTime fields, SolrJ is a very accommodating API. Our Solr “tag” field contains multiple values and passing a simple List<String> does the job. The Solr Date type for “post_datetime” happily accepts our java.sql.Timestamp LinkDatetime property.

Searching with SolrJ

Using SolrJ to search our index is even easier than adding records.

And our result.

One tip on using SolrJ is to be sure to add all SolrJ supporting libraries from the Solr distribution (dist/solrj-lib) to your Build Path. Here’s what my project looks like in Eclipse where I placed my SolrJ support JARs in their own folder.