2

How do I specify the SolrQuery to return the data in json format? Heres the code I am using to query data: Once I get the SolrDocument how can I convert it to a json Object/string?

 SolrQuery sQuery = new SolrQuery();
 sQuery.setQuery("name:test");
 QueryResponse resp = server.query(sQuery);
 SolrDocumentList docs = resp.getResults();

3 Answers 3

17

Import the JSONUtil class from solrj, it has json converter for solr documents or you can write one easily if you see the code of that class:

import org.apache.noggit.JSONUtil;

SolrQuery sQuery = new SolrQuery();
sQuery.setQuery("name:test");
QueryResponse resp = server.query(sQuery);
SolrDocumentList docs = resp.getResults();
String returnValue = JSONUtil.toJSON(docs); //this has the json documents
Sign up to request clarification or add additional context in comments.

2 Comments

I've just found out the hard way that JSONUtil is freaking buggy and produces some weird recursive documents.
I upgraded from Solr 5.2 to 7.6 and when converting Solr Docs to JSON I was having a StackOverflowError (com.fasterxml.jackson.databind.type.TypeFactory). Changing the JSON writter to noggit's one fixed the issue.
3

SolrJ is designed to retrieve/convert the document as your defined POJO. If you want to convert the document to JSON, you would need to use a library like Jackson to do the conversion from SolrDocument to JSON.

Not sure if this would be an option for you, but wanted to mention that you might consider getting the response from Solr already formatted as JSON. See SolJSON for more details. You would not need to use SolrJ in this case. The easiest thing would be to use a straight http call (via a java http client library) and get a JSON response back from Solr using the SolJSON techniques.

7 Comments

Can u please tell me how I would specify retrieval of the solr doc in json using java - the SolJSON documentation does not specify how to set the request param in Java?
This previous discussion on the Solr User list should help - lucene.472066.n3.nabble.com/SolrJ-Response-JSON-td1002024.html
It did not really answer my question seems like I had to read the SolrDocument and add the key value pairs into a JSONObject for me to get a valid json string to return to the user. Regarding the POJO way since my Pojo contains complex types I had to add the complex type as a SolrPlugin & defined it in the schema.xml but when I saved the POJO the complex type saved as null - any suggestions on how to get that working?
Complex types are not supported with SolrJ only simple types - See this previous answer - stackoverflow.com/questions/6633684/…
Additionally, there is not a native way to get JSON directly from Solr using the SolrJ library. As I stated, the Jackson library can handle the SolrDocument to JsonObject conversion for you. Or you could just use a straight http call (via a java http client library) and get a JSON response back from Solr using the SolJson techniques.
|
0

You can also do it using GSON.

SolrDocumentList SolrResponse = searchWithSolr();
Gson gson = new Gson();
if (SolrResponse.size()!=0) {
  System.out.println(gson.toJson(SolrResponse));
  interpolMap.put("interpol", gson.toJson(InterpolSolrResponse));
}

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.