A Spring Solr Criteria API Query Example

Last time we covered Solr CRUD operations in Spring Data Solr. Today we're going to begin a series on Custom Solr Query types, beginning with the Java Criteria API.

Here's a quick definition of Criteria Queries:

Criteria queries are a programmatic, type-safe way to express a query. They are type-safe in terms of using interfaces and classes to represent various structural parts of a query. (source)

We begin in our SolrUI class, passing the following query to our ProductService.

We build our Criteria Query in our ProductRepository. The code was based on Petri Kainulainen's Criteria API Example on GitHub and detailed in this excellent post.

Notice how we're using the Product Class fields to build the query. We'll be saving it as a Criteria object called conditions. By the way, that's a Solr Criteria object.

org.springframework.data.solr.core.query.Criteria

Here's the Solr query we built with “Canon Camera memory.”

And our output.

As usual, all of the source code covered in this post is available in NixMash Spring on GitHub.

Posted December 03, 2015 10:35 AM EST

More Like This Post