2013-02-09 16 views
1

我使用solrj向solr提交查詢,該查詢以json格式返回結果。solrj QueryResponse轉換爲json

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

但searchResultStr沒有JSON格式的字符串。相反,它有這樣的事情:

{responseHeader = {狀態= 0,QTIME = 21},位置= {has_zipcode = TRUE,location_param = 94085}}

但是,如果我直接打Solr的URL在瀏覽器中,我得到了正確的JSBON格式:

{ 「responseHeader」:{ 「地位」:0, 「QTIME」:15}, 「位置」:{ 「has_zipcode」:真」 location_param「:」94085「}}

回答

0

對於JSON輸出,您必須使用curl查詢HTTPSolrServer,如答案here中所述。使用EmbeddedSolrServer不會幫助,即solrj

當您使用solrj查詢,你將不得不從QueryResponse對象得到SolrDocumentList,並通過每個迭代SolrDocument和輸入數據到JSON你所希望的方式轉換爲JSON格式。

0

Solr QueryResponse返回一個哈希映射,所以使用任何哈希映射到json轉換器來獲取它的json。在這裏我使用了Gson()進行轉換。

QueryResponse response = solr.query(query); 
Gson gson = new Gson(); 
String json = gson.toJson(response.getResults()); 
System.out.println(json);