Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Solr 5 – JSON Request API, Part 1

DZone's Guide to

Solr 5 – JSON Request API, Part 1

The JSON API is very easy to use and allows us to utilize more structured queries when talking to Solr. In this post, we will introduce you to the world of the JSON request API in Solr.

· Web Dev Zone
Free Resource

Tips, tricks and tools for creating your own data-driven app, brought to you in partnership with Qlik.

Solr 5 introduced a lot of changes to the world of the API’s and functionalities around Solr. JSON API and Facets were the ones that were moved from the 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 of the JSON request API in Solr.

Running a Query

I’m sure you are familiar on how to run queries to Solr in the standard 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

Next 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 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 the 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.

Explore data-driven apps with less coding and query writing, brought to you in partnership with Qlik.

Topics:
solr ,json api ,web dev

Published at DZone with permission of Rafał Kuć, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}