2013-01-23 27 views
5

我已經通過本網站上的相關問題,但還沒有找到相關的解決方案。使用SolrJ和Solr4分面

當使用形式

&facet=true&facet.field=country 

響應的HTTP請求查詢我Solr4指數包含了所有不同的國家,每個國家計數一起。

如何使用SolrJ獲取此信息? 我曾嘗試以下,但它只有在所有國家返回總計數,不是每個國家:

solrQuery.setFacet(true); 
solrQuery.addFacetField("country"); 

以下似乎工作,但我不希望有明確設置的所有分組事先:

solrQuery.addFacetQuery("country:usa"); 
solrQuery.addFacetQuery("country:canada"); 

其次,我不確定如何從QueryResponse對象中提取構面數據。

所以兩個問題:

1)使用SolrJ我怎麼能突間在外地,沒有明確指定組返回分組?

2)使用SolrJ如何從QueryResponse對象中提取構面數據?

謝謝。

更新:

我也試過類似謝爾蓋的響應(下圖)的東西。

List<FacetField> ffList = resp.getFacetFields(); 
log.info("size of ffList:" + ffList.size()); 
for(FacetField ff : ffList){ 
    String ffname = ff.getName(); 
    int ffcount = ff.getValueCount(); 
    log.info("ffname:" + ffname + "|ffcount:" + ffcount);   
} 

上述代碼顯示大小= 1的ffList,循環經歷1次迭代。在輸出ffname =「country」中,ffcount是匹配原始查詢的總行數。

這裏沒有按國家細分。

我應該提到,在同一個solrQuery對象上,我也調用addField和addFilterQuery。不知道這小面的影響:

solrQuery.addField("user-name"); 
solrQuery.addField("user-bio"); 
solrQuery.addField("country"); 
solrQuery.addFilterQuery("user-bio:" + "(Apple OR Google OR Facebook)"); 

更新2:

我想我得到了它,又基於什麼謝爾蓋下面說。我使用FacetField.getValues()提取List對象。

List<FacetField> fflist = resp.getFacetFields(); 
for(FacetField ff : fflist){ 
    String ffname = ff.getName(); 
    int ffcount = ff.getValueCount(); 
    List<Count> counts = ff.getValues(); 
    for(Count c : counts){ 
     String facetLabel = c.getName(); 
     long facetCount = c.getCount(); 
    } 
} 

在上面的代碼標籤可變每個小面組相匹配,並且計數是該分組對應的計數。

回答

7

其實你只需要設置小領域和方面將被激活(檢查SolrJ源代碼):

solrQuery.addFacetField("country"); 

你在哪裏找小信息?它必須是在QueryResponse.getFacetFieldsgetValues.getCount

+0

謝謝謝爾蓋。我更新了我的問題以包含您提到的方法。 –

1

在Solr的迴應,你應該使用QueryResponse.getFacetFields()獲得的FacetFields其中數字「國家」 List。因此「國家」是由QueryResponse.getFacetFields().get(0)

idenditfied那麼你遍歷它使用

QueryResponse.getFacetFields().get(0).getValues().get(i) 

得到Count對象的List並獲得使用QueryResponse.getFacetFields().get(0).getValues().get(i).getName() 小的值名稱和使用

QueryResponse.getFacetFields().get(0).getValues().get(i).getCount() 
相應的權重