Solr: Search using JSON Request API

By: Dikshant Shahi | June 5, 2015

Solr 5.1 introduces the new JSON request API!

Solr supports multiple query response formats such as xml, json, csv, velocity UI, etc. but for making request you were always supposed to provide Http request parameters. Below is an example.

$ curl http://localhost:8983/solr/core0/select?q=journal:(treatment for nausea)&wt=json

Using wt parameter, you specify the response format you are seeking the result in. Below is the JSON response you get on setting wt=json.

response": {
    "numFound": 10,
    "start": 0,
    "docs": [
        "journal": "Cancer treatment reports"
        "journal": "Cancer treatment centers"

But query request format was standard i.e. through request parameters. This works good for simple requests but if the query string is long or query contains many parameters or you need to apply per-field parameter, there it becomes less convenient.
Solr 5.1 supports JSON request API, where search request can have a JSON body. Below example specifies the search request in JSON format.

$ curl http://localhost:8983/solr/core0/select -d '
    query:"journal:(treatment for nausea)",

JSON request can also be specified as value for ‘json’ parameter. If the same request contains JSON body and JSON parameter, Solr smartly merges both.

$ curl http://localhost:8983/solr/core0/select -d 'json={query:"journal:(treatment for nausea)"}&wt=json'

For per field request parameters, the hierarchical structure of JSON provides better formatting and would make a natural choice. Now, your client application can now be JSON in-JSON out.

Dikshant Shahi (8 Posts)

Dikshant works as Solution Architect at The Digital Group. He takes interest in Semantic Search, Information Retrieval, Natural Language Processing and Machine Learning. He is the author of book "Apache Solr: A Practical Approach to Enterprise Search".

This post has been viewed 6,568 times