JSON

JSON response writer converts results into JSON format. This is a default response writer for Solr, so if we do not set the wt parameter, the default output will be in JSON format. We can configure the MIME type for any response writer in the solrconfig.xml file. The default MIME type for JSON response writer is application/json; however, we can override it as per our search needs. For example, we can override the MIME type configuration in techproducts solrconfig.xml:

<queryResponseWriter name="json" class="solr.JSONResponseWriter">
<!-- For the purposes of the tutorial, JSON responses are written as
plain text so that they are easy to read in *any* browser.
If you expect a MIME type of "application/json" just remove this override.
-->
<str name="content-type">text/plain; charset=UTF-8</str>
</queryResponseWriter>

JSON response writer parameters

This is a list of JSON response writer parameters that we need to configure to get a response in the expected format:

Parameter Behavior Value Description
json.nl

Controls the output format of NamedList, where the order is more important than access by name. NamedList is currently used for field faceting data.

flat (default) NamedList is represented as a flat array, with alternating names and values.

Input:

NamedList("a"=1, "bar"="foo", null=3, null=null)

Output:

["a",1, "bar","foo", null,3, null,null]

map NamedList can have optional keys and repeated keys. It preserves the order.

Input:
NamedList("a"=1, "bar"="foo", null=3, null=null)

Output:
{"a":1, "bar":"foo", "":3, "":null}

arrarr NamedList is represented as an array of two element arrays.

Input:
NamedList("a"=1, "bar"="foo", null=3, null=null)

Output:
[["a",1], ["bar","foo"], [null,3], [null,null]]

arrmap NamedList is represented as an array of JSON objects.

Input:
NamedList("a"=1, "bar"="foo", null=3, null=null)

Output:
[{"a":1}, {"b":2}, 3, null]

arrntv NamedList is represented as an array of name type value JSON objects.

Input:
NamedList("a"=1, "bar"="foo", null=3, null=null)

Output:
[{"name":"a","type":"int","value":1}, {"name":"bar","type":"str","value":"foo"}, {"name":null,"type":"int","value":3}, {"name":null,"type":"null","value":null}]

json.wrf Adds a wrapper function around the JSON response. Useful in AJAX with dynamic script tags for specifying a JavaScript callback function. function

Example: Searching for id=SP2514N.

URL: http://localhost:8983/solr/techproducts/select?q=SP2514N&wt=json.

Response:

{
"responseHeader":{
"status":0,
"QTime":1,
"params":{
"q":"SP2514N",
"wt":"json",
"_":"1514797189592"}},
"response":{"numFound":1,"start":0,"docs":[
{
"id":"SP2514N",
"name":"Samsung SpinPoint P120 SP2514N - hard drive - 250 GB - ATA-133",
"manu":"Samsung Electronics Co. Ltd.",
"manu_id_s":"samsung",
"cat":["electronics",
"hard drive"],
"features":["7200RPM, 8MB cache, IDE Ultra ATA-133",
"NoiseGuard, SilentSeek technology, Fluid Dynamic Bearing (FDB) motor"],
"price":92.0,
"price_c":"92.0,USD",
"popularity":6,
"inStock":true,
"manufacturedate_dt":"2006-02-13T15:26:37Z",
"store":"35.0752,-97.032",
"_version_":1583131913641525248,
"price_c____l_ns":9200}]
}
}

We can analyze response writer using the Solr console admin as well. Go to the Solr console admin, select techproducts, and click on the Query tab. Insert your text query in the q field, select the wt parameter as json and click on the Execute Query button at the bottom. The query URL and response output will be displayed as follows:

Solr console admin, response writer configuration, and output
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.59.219