使用SolrJ和Solr4进行刻面 [英] Faceting using SolrJ and Solr4
问题描述
我已经浏览了本网站上的相关问题但未找到相关解决方案。
I've gone through the related questions on this site but haven't found a relevant solution.
使用表单的HTTP请求查询我的Solr4索引时
When querying my Solr4 index using an HTTP request of the form
&facet=true&facet.field=country
回复包含所有不同的国家/地区以及每个国家/地区的计数。
The response contains all the different countries along with counts per country.
如何使用SolrJ获取此信息?
我尝试过以下操作,但它只返回所有国家/地区的总计数,而不是每个国家/地区:
How can I get this information using SolrJ? I have tried the following but it only returns total counts across all countries, not per country:
solrQuery.setFacet(true);
solrQuery.addFacetField("country");
以下似乎有效,但我不想事先明确设置所有分组:
The following does seem to work, but I do not want to have to explicitly set all the groupings beforehand:
solrQuery.addFacetQuery("country:usa");
solrQuery.addFacetQuery("country:canada");
其次,我不确定如何从QueryResponse对象中提取构面数据。
Secondly, I'm not sure how to extract the facet data from the QueryResponse object.
所以有两个问题:
1)使用SolrJ如何在一个字段上进行分面并返回分组而不明确指定组?
1) Using SolrJ how can I facet on a field and return the groupings without explicitly specifying the groups?
2)使用SolrJ如何从QueryResponse对象中提取构面数据?
2) Using SolrJ how can I extract the facet data from the QueryResponse object?
谢谢。
更新:
我也尝试了类似谢尔盖的回复(下方) 。
I also tried something similar to Sergey's response (below).
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是与原始查询匹配的总行数。
The above code shows ffList with size=1 and the loop goes through 1 iteration. In the output ffname="country" and ffcount is the total number of rows that match the original query.
这里没有按国家/地区细分。
There is no per-country breakdown here.
我应该在同一个solrQuery对象上提到我还调用addField和addFilterQuery。不确定这是否会影响分面:
I should mention that on the same solrQuery object I am also calling addField and addFilterQuery. Not sure if this impacts faceting:
solrQuery.addField("user-name");
solrQuery.addField("user-bio");
solrQuery.addField("country");
solrQuery.addFilterQuery("user-bio:" + "(Apple OR Google OR Facebook)");
更新2:
我想我得到了它,再次基于谢尔盖在下面说的话。我使用FacetField.getValues()提取了List对象。
I think I got it, again based on what Sergey said below. I extracted the List object using FacetField.getValues().
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();
}
}
在上面的代码中,label变量与每个facet组匹配和count是该分组的相应计数。
In the above code the label variable matches each facet group and count is the corresponding count for that grouping.
推荐答案
实际上你只需要设置facet字段并激活facet(检查SolrJ源代码):
Actually you need only to set facet field and facet will be activated (check SolrJ source code):
solrQuery.addFacetField("country");
您在哪里寻找方面信息?它必须在 QueryResponse.getFacetFields
( getValues.getCount
)
Where did you look for facet information? It must be in QueryResponse.getFacetFields
(getValues.getCount
)
这篇关于使用SolrJ和Solr4进行刻面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!