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代码

  1. new SolrQuery("*:*").setFacet(true).addFacetField("manu");

而xml返回的结果为这样:

Xml代码

  1. <lst name="facet_fields">
  2. <lst name="manu">
  3. <int name="Canon USA">17</int>
  4. <int name="Olympus">12</int>
  5. <int name="Sony">12</int>
  6. <int name="Panasonic">9</int>
  7. <int name="Nikon">4</int>
  8. </lst>
  9. </lst>

通过java代码可以这样获取facet结果:

Java代码

  1. List<FacetField> facetFields = queryResponse.getFacetFields();

在已有的查询基础上增加facet query, 可以这样写:

Java代码

  1. 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<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);
}

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<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();}
}

In the above code the label variable matches each facet group and count is the corresponding count for that grouping.

转载于:https://www.cnblogs.com/wych/p/4097859.html

Solr -- Solr Facet 2相关推荐

  1. Solr 使用Facet分组过程中与分词的矛盾解决办法

    对于一般查询而言 , 分词和存储都是必要的 . 比如 CPU 类型 "Intel 酷睿 2 双核 P7570", 拆分成 "Intel"," 酷睿 & ...

  2. Solr -- Solr Facet 1

    一.Facet介绍 solr facet 是solr搜索的一大特色,facet不好翻译,有说是垂直搜索,有说是分片搜索,但都不是很好,还是懒得翻译了,就叫facet ,具体功能看下面的例子意会吧. 比 ...

  3. solr中facet及facet.pivot理解

    Facet['fæsɪt]很难翻译,只能靠例子来理解了.Solr作者Yonik Seeley也给出更为直接的名字:导航(Guided Navigation).参数化查询(Paramatic Searc ...

  4. cloudtalk 无法连接到消息服务器,solr - Solr Cloud down无法与Zookeeper对话客户端会话超时 - 堆栈内存溢出...

    我有在16GB RAM内存上运行的solr云,用于分片的2个solr节点(相同ip),嵌入式zookeeper. 我在默认配置上运行solr,尽管默认配置随附-Xms5g-Xmx5g,但我在Solr仪 ...

  5. [solr] solr Similarity:切换不同相似度计算方法

    Similarity改变solr的打分机制: solr本身已经提供了几种算法 org.apache.solr.search.similarities.BM25SimilarityFactory org ...

  6. Solr Facet技术的应用与研究

    问题背景 在<搜索引擎关键字智能提示的一种实现>一文中介绍过,美团的CRM系统负责管理销售人员的门店(POI)和项目(DEAL)信息,提供统一的检索功能,其索引层采用的是SolrCloud ...

  7. 搜索引擎之---Apache solr的实现

    Solr 是一种可供企业使用的.基于 Lucene 的搜索服务器,它支持层面搜索.命中醒目显示和多种输出格式.在这篇分两部分的文章中,Lucene Java™ 的提交人 Grant Ingersoll ...

  8. 【转载】solr教程,值得刚接触搜索开发人员一看

    转载:http://blog.csdn.net/awj3584/article/details/16963525 Solr调研总结 开发类型 全文检索相关开发 Solr版本 4.2 文件内容 本文介绍 ...

  9. solr java浏览器访问_solr解决访问安全

    Tomcat7,solr3.6,mmseg1.8 1:环境的搭建 1:解压tomcat,solr,mmseg4j 2:复制dist文件夹下apache-solr.war到tomcat的webapp文件 ...

最新文章

  1. 我是怎么通过技术白手起家创业的。
  2. VTK:可编程源用法实战
  3. 将C ++类型的属性公开给QML
  4. 第五章 随机事件及其概率
  5. 原创 | 我说我了解集合类,面试官竟然问我为啥HashMap的负载因子不设置成1!?...
  6. vue 监听渲染变化
  7. Linux下如何编译Android源码~~~
  8. 438.找到字符串中所有字母异位词
  9. 【Unity】NGUI下载与导入
  10. 台式计算机垃圾清理程序,手把手教你制作一键清理电脑垃圾bat程序
  11. python中show函数的用法_python学习笔记之——函数模块
  12. PLC基础——1.自保持回路
  13. Mongoose Schema hasn't been registered for model
  14. 利用51单片机,矩阵键盘按键显示0--F在数码管
  15. InfluxDB入门系列教程④ InfluxDB Studio可视化数据库管理工具
  16. STM32小项目之dht11在oled上显示温湿度
  17. 欧拉φ函数和欧拉降幂公式
  18. red5 FAQ - 刚接触red5的可以看看
  19. mybatis中mapper.xml模板
  20. Linux下的c++系统检测工具:网络编程小插曲

热门文章

  1. DOS命令八大经典案例
  2. Spring编程式和声明式事务实例讲解
  3. 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](三)
  4. 确认AD DS域是否正常
  5. linxu命令之cp 拷贝整个目录下的所有文件
  6. 让Windows Server 2008+IIS 7+ASP.NET突破默认限制,支持海量并发连接数
  7. [原创]WildPackets Omnipeek介绍
  8. FSMO角色的Windows界面查看和转移示例
  9. ArcSDE 9.1 for Oracle10g on Solaris 10
  10. 写给大忙人的ELK最新版6.2.4学习笔记-Logstash和Filebeat解析(java异常堆栈下多行日志配置支持)...