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=[“[email protected]”,”abc”], calculation_policy=text_value, username=abc, [email protected], 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?

3 Answers 3

7

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 );
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.