2012-07-05 68 views
2

我正在進行搜索,嘗試將HttpSolrServer的響應轉換爲json格式。答案是SolrDocumentList。我現在所擁有的代碼是:將solr響應(solrdocumentlist)轉換爲json(或xml)

SolrQuery solrQuery = new SolrQuery(query); 
solrQuery.setParam("wt", "json"); //doesn't affect the return format 

QueryResponse rsp = solrServer.query(solrQuery); 
SolrDocumentList docs = rsp.getResults(); 

return docs.toString(); 

當我打印出來的回報,這回來爲:

{numFound=2,start=0,docs=[SolrDocument{cat=[electronics, camera], features=[3x zoop, 7.1 megapixel Digital ELPH, movie clips up to 640x480 @30 fps, 2.0" TFT LCD, 118,000 pixels, built in flash, red-eye reduction], id=9885A004, inStock=true, includes=32MB SD card, USB cable, AV cable, battery, manu=Canon Inc., manufacturedate_dt=Mon Feb 13 10:26:37 EST 2006, name=Canon PowerShot SD500, popularity=7, price=329.95, store=45.17614,-93.87341, weight=6.4}, SolrDocument{cat=[electronics, multifunction printer, printer, scanner, copier], features=[Multifunction ink-jet color photo printer, Flatbed scanner, optical scan resolution of 1,200 x 2,400 dpi, 2.5" color LCD preview screen, Duplex Copying, Printing speed up to 29ppm black, 19ppm color, Hi-Speed USB, memory card: CompactFlash, Micro Drive, SmartMedia, Memory Stick, Memory Stick Pro, SD Card, and MultiMediaCard], id=0579B002, inStock=true, manu=Canon Inc., name=Canon PIXMA MP500 All-In-One Photo Printer, popularity=6, price=179.99, store=45.17614,-93.87341, weight=352.0}]}} 

使用他們的數據。例如在canon搜索。

如果我不是做return rsp.toString();我回來的頭信息與它爲:

{responseHeader={status=0,QTime=1,params={indent=true,q=canon\*,wt=xml,version=2.2}},response={numFound=2,start=0,docs=[SolrDocument{cat=[electronics, camera], features=[3x zoop, 7.1 megapixel Digital ELPH, movie clips up to 640x480 @30 fps, 2.0" TFT LCD, 118,000 pixels, built in flash, red-eye reduction], id=9885A004, inStock=true, includes=32MB SD card, USB cable, AV cable, battery, manu=Canon Inc., manufacturedate_dt=Mon Feb 13 10:26:37 EST 2006, name=Canon PowerShot SD500, popularity=7, price=329.95, store=45.17614,-93.87341, weight=6.4}, SolrDocument{cat=[electronics, multifunction printer, printer, scanner, copier], features=[Multifunction ink-jet color photo printer, Flatbed scanner, optical scan resolution of 1,200 x 2,400 dpi, 2.5" color LCD preview screen, Duplex Copying, Printing speed up to 29ppm black, 19ppm color, Hi-Speed USB, memory card: CompactFlash, Micro Drive, SmartMedia, Memory Stick, Memory Stick Pro, SD Card, and MultiMediaCard], id=0579B002, inStock=true, manu=Canon Inc., name=Canon PIXMA MP500 All-In-One Photo Printer, popularity=6, price=179.99, store=45.17614,-93.87341, weight=352.0}]}} 

我知道,與HttpSolrServer響應格式只能目前是XML或javabin(我已經設置爲XML)的。這似乎對實際返回的結果及其格式沒有影響。

我似乎無法找到任何有關將響應轉換爲json的信息。有任何想法嗎?

回答

3

雖然這是一個老問題,但我已經想出了一個類似的問題,在挖掘代碼之後,我能夠將響應轉換爲json,儘管我仍然對代碼的速度和性能持懷疑態度情況下,存在過多的請求數來了..

所以下面的片的,我寫代碼(I用於從拋棄JSON lib和忽略的for循環笨結構):

QueryResponse qp = server.query(solrQuery); 
SolrDocumentList docList= qp.getResults(); 
JSONObject returnResults = new JSONObject(); 
Map<Integer, Object> solrDocMap = new HashMap<Integer, Object>(); 
int counter = 1; 
for(Map singleDoc : docList) 
{ 
    solrDocMap.put(counter, new JSONObject(singleDoc)); 
    counter++; 
} 
returnResults.put("docs", solrDocMap); 

基本上這會讓你在json中得到結果... solrj不允許你使用任何其他種類的javabin ,儘管你可能會在查詢集重量明確

希望它有助於

4

對於你正在嘗試做的事情,你不需要使用solrj庫。您可以使用CommonsHTTPClient直接通過HTTP發送查詢參數,並設置'wt = json' - 以JSON格式檢索響應。

2

下面的代碼工作

SolrDocumentList list = new SolrDocumentList(); 
JSONArray jArray =new JSONArray(); 
QueryResponse result = solr.query(parameters);   
list=result.getResults(); 

for (int i = 0; i < list.size(); i++) { 
    JSONObject json = new JSONObject(list.get(i)); 
    jArray.put(json);   
} 

還要添加參數json.nl=map

來源:http://wiki.apache.org/solr/SolJSON

-1

導入GSON和使用它的實用方法。

+0

請詳細說明。我不認爲這是一條路。 – 2017-07-07 18:11:45