Spring Solr Minimum Daily Configuration Requirements

We’re barely scratching the surface with Solr in NixMash Spring, so it seems a good time to look at the minimum configuration requirements to enjoy using Solr in Spring.

We’re going to look at the following issues:

  1. Solr Dependencies
  2. Our Application Context Solr Configuration
  3. Repository Configuration and Considerations
  4. The Service Implementation

At this point our Spring Solr application performs a couple of simple queries and that’s it. I often like to start at the end, so let’s look at a query result for “all products that are currently In Stock.”

Solr Dependencies

To support Solr we add Spring Boot Starter Data Solr and Apache Solr Core. (The project is also called “Solr” and is a Gradle Child Project.) We’re using Solr Core 4.10.4 because as I said in another post I couldn’t get Solr Core 5.x libraries to work with Spring Boot 1.2.6. The excludes are to prevent the multiple log binding warnings and can be ignored.

Application Context Solr Configuration

Things start to get interesting when configuring our Application Context for Solr. We have two Solr Context files, one for the Http Solr Server prod config and another for the dev profile Embedded Solr config.

The Configuration class contains 3 primary beans which work together to enable a Solr Repository with full CRUD capabilities. We need 1) a SolrServer bean, 2) a SolrTemplate which implements the SolrOperations Interface on our SolrServer bean, and 3) our Search Repository bean which relies on the other two beans.

The configuration I’m using above is the Java equivalent of Christoph Strobl’s Custom Solr Example XML application context file in his Spring Solr Repository Example project on GitHub.

Repository Configuration and Considerations

With the Solr Context we’re using here we need to add a @NoRepositoryBean annotation to our SolrProductRepository because the repository is an “intermediate repository interface” to the Solr searchRepository bean we created above. @NoRepositoryBean prevents Spring from creating an instance of the repository at runtime. Also, while not shown, the ProductRepository base class extends CrudRepository<Product, String>.

Notice the commented-out @Resource SolrTemplate lines in the SolrProductRepository class above. I left those there to demonstrate that if we would have not created our searchRepository bean in our context we could have annotated the class as a @Repository and used our SolrTemplate @Resource to query our Solr database. That would have worked, but we would not have had access to all of the good SimpleSolrRepository operations like those shown below.

The Service Implementation

The final piece is our Service Implementation where we simply reference our Repository @Resource and go to town. Notice we’re using both a Method Name Query .findByAvailableTrue() and a CRUD .findAll() query from the SimpleSolrRepository base class.

Now that we covered the basics we’ll dive deeper into Spring Solr in our next installment. All source code referenced here is available in branch v0.2.3 of NixMash Spring on GitHub.