Solr 5 – JSON Request API, part one

Solr 5 introduced a lot of changes to the world of the API’s and functionalities around Solr. JSON API and facets were the one that were moved from HeliosSearch project and resulted in users being able to send queries using JSON, which is a bit more human friendly compared to long queries constructed as URI. In this post we will introduce you to the world on JSON request API in Solr.

Running a query

I’m sure you are familiar on how to run queries to Solr in the standard, old fashioned way, right? So, to get documents that match the solr term in the _text_ field we would just run a command like this:

curl -XGET 'localhost:8983/solr/gettingstarted/select?q=_text_:solr&indent=true'

Just for the record, I’m using the schemaless example provided with Solr and I indexed the data from the docs directory.

We can do the same using the JSON API of Solr. For example, a JSON structured query that would return exactly the same results as the one shown above would look as follows:

curl -XGET 'localhost:8983/solr/gettingstarted/query' -d '{
 "query":"_text_:solr"
}'

As you can see, we just specify the query using a JSON object and the query property in it and we send it as a request body to the /query handler. We could also send the query to the /select handler, but the /query one uses JSON response by default, so it seems more appropriate for JSON queries 🙂

We can also use the standard parameter names and pass them in the body of the request, just like this:

curl -XGET 'localhost:8983/solr/gettingstarted/query' -d 'q=_text_:solr'

So, this time we didn’t have the JSON structure in the request body, we just pass the parameters there and separate them with & character, just like when sending a query using URI request.

Paging

Of course, we can also page through the results when using JSON API. We do that by using the limit and and offset parameters. The limit parameter specifies the maximum number of returned documents and the offset parameter is responsible for telling Solr from which document the results should be returned. For example, to get 20 documents starting from the 11th one, we would run the following query:

curl -XGET 'localhost:8983/solr/gettingstarted/query' -d '{
 "query":"_text_:solr",
 "limit":20,
 "offset":10
}'

Sorting

The next thing is the ability to sort the results and we can do that using Solr JSON API as well. To sort our results on the basis of the id field, in the descending order we would run the following query:

curl -XGET 'localhost:8983/solr/gettingstarted/query' -d '{
 "query":"_text_:solr",
 "sort":"id desc"
}'

It is very similar to what we are used to when using the standard URI request queries, but we are using the sort property.

Filtering

Finally, we have filtering. We can filter our data by using the filter property. For example, to narrow down our data to only the ones that have text/html value in the content_type field we would run the following query:

curl -XGET 'localhost:8983/solr/gettingstarted/query' -d '{
 "query":"_text_:solr",
 "filter":"content_type:text/html"
}'

What’s next

As you can see the JSON API of Solr is very easy to use and allows us to use more structured queries when talking to Solr. In the next entry we will discuss the JSON facets API, how to use it and what are other differences when it comes to JSON facets and the traditional implementation, because making the request is not the only difference when we compare those two.

Leave a Reply

Your email address will not be published. Required fields are marked *