4

I am trying to return solr response in JSON format. However I am not able to get the JSON response. Below is my solr config:'

<queryResponseWriter name="json" default="true" class="org.apache.solr.request.JSONResponseWriter">
    <str name="content-type">application/json</str>
</queryResponseWriter>

Below is my java code:

HttpSolrServer server = new HttpSolrServer(serverUrl);
SolrQuery query = new SolrQuery();
query.setQuery(query);
query.set("indent","true");
query.set("wt","json");
QueryResponse response = server.query(query);

System.out.println(response.getResults().get(index));

However output is displyed in following format.

{numFound=1,start=0,docs=[SolrDocument{_uniqueKey=[“abc@gmail.com”,”abc”], calculation_policy=text_value, username=abc, email=abc@gmail.com, display_order=10, last_login=Mon Jan 26 11:27:35 PST 2015, created=Mon Jan 26 11:27:35 PST 2015}]}

However I get JSON response after execute following line:

System.out.println(new Gson().toJson(queryResponse.getResults().get(index)));

Can someone please tell me what step am I missing?

user1247412
  • 647
  • 7
  • 16
  • 29

3 Answers3

6

With newer versions of Solr (starting with 4.7.0) it is possible to return the query response directly in json-format. This can be done with the NoOpResponseParser.

SolrQuery query = new SolrQuery();
QueryRequest req = new QueryRequest(query);

NoOpResponseParser rawJsonResponseParser = new NoOpResponseParser();
rawJsonResponseParser.setWriterType("json");
req.setResponseParser(rawJsonResponseParser);

NamedList<Object> resp = mySolrClient.request(req);
String jsonResponse = (String) resp.get("response");

System.out.println(jsonResponse );
laberning
  • 769
  • 7
  • 19
0

Perhaps this link may clarify: quoted format json from Solrj

Else maybe you can achieve what you need just through an Http Request as I know that indeed has json support.

Community
  • 1
  • 1
David Kaplan
  • 150
  • 1
  • 10
0

By default Solrj uses wt=javabin&version=2 as default for fetching results as described in http://wiki.apache.org/solr/javabin

However to show the response in json either use GSON or Jackson(Object Mapper).

Swaraj
  • 589
  • 4
  • 15