Spring Solr Named and Annotated Query Examples

We’re looking at Custom Spring Solr Queries, so after using the Java Criteria API in our last post we’re going to move on to Spring Solr Named and Annotated Queries. We’re going to perform the same Solr query using a Named Query and an Annotated Query and see the differences.

We’re going to call our Solr queries¬†from SolrUI using our ProductService. Nothing new here, just setting the stage.

Both queries are identical, as I mentioned. Here they are defined in ProductRepository.

Our Named Query is Product.findByNameOrCategory and is defined in our named-queries.properties file.

The Solr query defined in our named-queries.properties file.

Customizing Named-Queries.Properties

By default, Spring looks for a¬†jpa-named-queries.properties file located in resources/META-INF for named query definitions. We’re changing that to named-queries.properties in our resources folder by setting the namedQueriesLocation property of @EnableSolrRepostories.

The Result

For completeness, here is the result of this Solr Query for both of our query types for “canon.”

The full source for this post is available in NixMash Spring on GitHub.