Search code examples
javasolrsolrj

Using solrj to retrieve a SolrDocument in json format


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();

Solution

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