1

I use solrj to submit a query to solr , that returns the result in json format.

SolrQuery query = new SolrQuery();    
SolrQuery query = new SolrQuery();
query.setParam("kw", keyword);
query.setParam("lc", location);
query.setParam("wt", "json");
query.setParam(CommonParams.QT, "/lds");
QueryResponse qResponse = solrServer.query(query);
searchResultStr = qResponse.toString();

But the searchResultStr does not have a string in JSON format. Instead it has something like this:

{responseHeader={status=0,QTime=21},location={has_zipcode=true,location_param=94085}}

But if i directly hit the solr url in the browser, I get the correct JSBON format:

{"responseHeader":{"status":0,"QTime":15},"location": {"has_zipcode":true,"location_param":"94085"}}

Iswanto San
  • 18,263
  • 13
  • 58
  • 79
user1288954
  • 61
  • 1
  • 2
  • 5

2 Answers2

0

For a JSON output you will have to query a HTTPSolrServer using curl as mentioned in the answer here. Using EmbeddedSolrServer will not help i.e. solrj.

When you use solrj to query you will have to get the SolrDocumentList from the QueryResponseobject and convert it into JSON format by iterating through each SolrDocument and entering the data into JSON the way you want.

Community
  • 1
  • 1
JHS
  • 7,761
  • 2
  • 29
  • 53
0

Solr QueryResponse returns a hashmap, so use any hashmap to json converter to get the json out of it. Here I have used Gson() for that conversion.

QueryResponse response = solr.query(query);
Gson gson = new Gson(); 
String json = gson.toJson(response.getResults());
System.out.println(json);
Prasad
  • 61
  • 4
  • 11