2012-03-07 73 views
1

我有以下代碼,它只是搜索Solr服務器。如何在Solr中將ResponseWriter設置爲非默認值

SolrServer server = new CommonsHttpSolrServer(url); 
SolrQuery searchquery = new SolrQuery("company profile"); 
QueryResponse response = server.query(searchquery) 

我想在json中的響應,而不是默認的是xml。於是我走進solrconfig.xml中的文件並啓用以下行:

<queryResponseWriter name="json" class="org.apache.solr.request.JSONResponseWriter" /> 

然而,從控制檯,我仍然得到重量= javabin編碼搜索查詢請求。

而且,我已經修改了上面的代碼是這樣的:

SolrServer server = new CommonsHttpSolrServer(url); 
SolrQuery searchquery = new SolrQuery("company profile"); 
searchquery.setParam("wt", "json"); 
QueryResponse response = server.query(searchquery) 

但我仍然得到wt=javabin編碼和wt=json還追加,使得查詢現在看起來是這樣的:

webapp/solr path=/select params={wt=javabin&wt=json} 

有什麼我做錯了嗎?

由於

回答

4

SolrJ僅支持javabinxml格式(可配置與CommonHttpSolrServer.setParser)。

但是,您爲什麼要使用JSON? Javabin是迄今爲止提供最佳解壓縮速度的格式,其缺點是不可讀的,與JSON和XML相反(在這種情況下這不是問題,因爲SolrJ正在爲您解析結果)。

+0

OMG!我一直都不知道。這是一個很大的退步。我需要json,因爲我的客戶端集成是Javascript。 – drecute 2012-03-07 17:37:01

+1

這並不意味着你不能從Javascript中查詢Solr,只需使用wt = json進行一個普通的HTTP調用並在javascript中解析它即可。 SolrJ只是一個庫,可以很容易地從任何JVM語言中調用Solr。 – jpountz 2012-03-07 18:04:45

+0

我發現json-lib很有趣。我將簡單地使用它來將XML轉換爲json,然後讓JavaScript從中取出它。 – drecute 2012-03-07 18:27:11

0

隨着SolrJ和JSON-lib中,我能得到這樣的JSON響應:

SolrServer server = new CommonsHttpSolrServer(url); 
SolrQuery searchquery = new SolrQuery("company profile"); 
QueryResponse response = server.query(searchquery) 

JSONArray jsonObject = JSONArray.fromObject(response.getResults()); 
log.log(Level.INFO, "received jsonObject is {0}", jsonObject.toString()); 
Iterator data = jsonObject.iterator(); 

SolrResultBean bean = new SolrResultBean(); 
List output = new ArrayList(); 
while(data.hasNext()) { 
    output.add(data.next()); 
} 
log.log(Level.INFO, "json items in the List are {0}", output); 
bean.setObject(output); 

// wicket page redirect 
setResponsePage(SearchPage.class, new PageParameters()); 
相關問題