设为首页 收藏本站
查看: 1168|回复: 0

[经验分享] Solr

[复制链接]
累计签到:4 天
连续签到:1 天
发表于 2015-7-17 12:51:44 | 显示全部楼层 |阅读模式
  solr将以导航为目的的查询结果称为facet. 它并不会修改查询结果信息, 只是在查询结果上根据分类添加了count信息, 然后用户根据count信息做进一步的查询, 比如淘宝的查询列表中, 上面会表示不同的类目相关查询结果的数量.   
比如搜索数码相机, 在搜索结果栏会根据厂商, 分辨率等维度列出, 这里厂商, 分辨率就是一个个facet.   
然后在厂商下面会有nikon, canon, sony等品牌, 这个叫约束(constraints)   
接下来是根据选择, 列出当前的导航路径, 这个叫面包屑(breadcrumb).   
solr有几种facet:   
普通facet, 比如从厂商品牌的维度建立fact   
查询facet, 比如根据价格查询时, 将根据价格, 设置多个区间, 比如0-10, 10-20, 20-30等   
日期facet, 也是一种特殊的范围查询, 比如按照月份进行facet.   
facet的主要好处就是可以任意对搜索条件进行组合, 避免无效搜索, 改善搜索体验.   
facet都是在查询时通过参数指定. 比如   
在http api中这样写:
  引用
  "&facet=true&facet.field=manu"
  .   
java代码这样写:
  Java代码 DSC0000.png
  
       
  • new SolrQuery("*:*").setFacet(true).addFacetField("manu");
  而xml返回的结果为这样:
  Xml代码
  
       
  •    
  •    
  • 17   
  • 12   
  • 12   
  • 9   
  • 4   
  •    

  通过java代码可以这样获取facet结果:
  Java代码
  
       
  • List facetFields = queryResponse.getFacetFields();
  在已有的查询基础上增加facet query, 可以这样写:
  Java代码
  
       
  • solrQuery.addFacetQuery("quality:[* TO 10]")
  比如对价格按照指定的区间进行facet, 可以这样加上facet后缀:
  引用
  &facet=true&facet.query=price:[* TO 100]   
&facet.query=price:[100 TO 200];&facet.query=[price:200 TO 300]   
&facet.query=price:[300 TO 400];&facet.query=[price:400 TO 500]   
&facet.query=price:[500 TO *]
  如果要对价格在400到500期间的产品做进一步的搜索, 那么可以这样写(使用了solr的过滤查询):
  引用
  http://localhost:8983/solr/select?q=camera &facet=on&facet.field=manu&facet.field=camera_type &fq=price:[400 to 500]
  注意这里的facet field不再包含price了   
如果这里对类型做进一步的查询, 那么query语句可以这样写:
  引用
  http://localhost:8983/solr/select?q=camera &facet=on&facet.field=manu &fq=price:[400 to 500] &fq=camera_type:SLR
  facet的使用场景:   
1.类目导航   
2.自动提示, 需要借助一个支持多值的tag field.   
3.热门关键词排行, 也需要借助一个tag field
  
  
  I've gone through the related questions on this site but haven't found a relevant solution.
  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.
  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");
  Secondly, I'm not sure how to extract the facet data from the QueryResponse object.
  So two questions:
  1) Using SolrJ how can I facet on a field and return the groupings without explicitly specifying the groups?
  2) Using SolrJ how can I extract the facet data from the QueryResponse object?
  Thanks.
  Update:
  I also tried something similar to Sergey's response (below).

List 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);           
}
  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.
  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)");
  Update 2:
  I think I got it, again based on what Sergey said below. I extracted the List object using FacetField.getValues().

List fflist = resp.getFacetFields();
for(FacetField ff : fflist){
String ffname = ff.getName();
int ffcount = ff.getValueCount();
List counts = ff.getValues();
for(Count c : counts){
String facetLabel = c.getName();
long facetCount = c.getCount();
}
}
  In the above code the label variable matches each facet group and count is the corresponding count for that grouping.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-87681-1-1.html 上篇帖子: [置顶] SOLR 4.4 部署 下篇帖子: Apache Solr查询语法(转)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表