Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Content-Type in Solr Responses

Tags:

json

html

solr

I have been learning about Solr and have run into an issue. If I make a query to Solr like this:

curl "http://localhost:8983/solr/myCollection/select?q=*:*&wt=json&rows=0"

I get a webpage back that has the JSON response. However, the webpage header says nothing about the Content-Type.

I would like to get the Content-Type in the header so that people querying Solr can use pretty print tools to clean up the Solr response and make it more readable without having to provide the "indent=on" option all the time.

Any help?

Thanks

<html>
  <head>
    <link rel="alternate stylesheet" type="text/css"
     href="resource://gre-resources/plaintext.css"
     title="Wrap Long Lines">  
    <style type="text/css"></style>
  </head>
  <body>
    <pre>{"responseHeader":{"status":0,"QTime":0,"params":
         {"q":"*:*","rows":"0","wt":"json"}},"response":
         {"numFound":27394430,"start":0,"docs":[]}}
    </pre>
  </body>
</html>

EDIT: Thanks to the answer below, I got it fixed. I made the change by using the Config API:

curl http://localhost:8983/solr/collectionName/config
  -H 'Content-Type:application/json' -d '{
  "update-queryresponsewriter" : {
    "name":"json",
    "class":"solr.JSONResponseWriter",
    "defaults": {
      "name":"application/json; charset=UTF-8"
    }
  }
}'

Just as a note, the change didn't take effect immediately for me, so you might have to wait a minute for your changes to show up.

like image 844
kgrimes2 Avatar asked Aug 30 '25 16:08

kgrimes2


1 Answers

As the example solrconfig.xml suggests, you should fix the Content-Type by removing the following section:

  <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>

Modern browsers without JSON viewer will display JSON as if it were plain text, so unless you want to see the data in things like IE6 you will be fine.

Discovered via http://grokbase.com/t/lucene/dev/1372n9jyha/solr-content-type-for-json

like image 84
Jan Tojnar Avatar answered Sep 02 '25 06:09

Jan Tojnar