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.

Solr -- Solr Facet 2的更多相关文章

  1. Solr -- Solr Facet 1

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

  2. 电商指尖---(9).net发展Solr中间Facet特征

    上一节中我们演示了在SolrAdmin中使用Facet功能来进行分组统计.这一节我们看看如何使用.NET开发Solr中的Facet功能.在讲Facet功能的同一时候, 我们看下.Net中如何使用Sol ...

  3. solr中facet及facet.pivot理解(整合两篇文章保留参考)

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

  4. solr中facet及facet.pivot理解

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

  5. Solr中Facet用法和Group用法

    Group分组划分结果,返回的是分组结果: Facet分组统计,侧重统计,返回的是分组后的数量: 一.Group用法: //组查询基础配置params.set(GroupParams.GROUP, & ...

  6. 指尖上的电商---(8)Solr中Facet的使用方法

    在大型电子商务站点中,在商品列表页,我们都能够看到商品按分类,品牌,价格的分类显示,例如以下图,这些我们能够使用solr中的facet功能实现. facet的基本功能就是对搜索结果中的商品进行分类. ...

  7. Solr 01 - 什么是Solr + Solr安装包目录结构说明

    目录 1 Solr概述 1.1 Solr是什么 1.2 Solr与Lucene的区别 2 Solr文件说明 2.1 Solr的目录结构 2.2 其他常用概念说明 2.3 创建基础文件目录 2.4 so ...

  8. [solr]solr的安装

    solr是什么? 翻译: SolrTM is the popular, blazing fast open source enterprise search platform from the Apa ...

  9. (三)Solr——Solr的基本使用

    1. Schema.xml 在schema.xml文件中,主要配置了solrcore的一些数据信息,包括Field和FieldType的定义等信息,在solr中,Field和FieldType都需要先 ...

随机推荐

  1. springMVC+freemarker中Could not resolve view with name... 问题解决

    用到springMVC+freemarker,自己在做demo过程中报: 严重: Servlet.service() for servlet springmvc threw exception jav ...

  2. 我的插件のinput文本框实现宽度自适应

    先来最后结果:DEMO 介绍一下如何让一个文本框的宽度能够随着文本框中的内容的宽度增长而增长,也就是能够实现宽度自适应效果. 代码实例如下: <!DOCTYPE html> <htm ...

  3. WisDom.Net 框架设计(七) 验证框架

    WisDom.Net-验证框架 1.分类 这里我们将数据验证分为以下几种 数据类型校验      主要用于确保数据类型输入的正确  比如年龄一项输入 A岁 ,显然不合法 域检查             ...

  4. html语言中的meta元素

    1.定义语言  格式:〈meta http-equiv=″Content-Type″ content=″text/html; charset=gb2312″〉  这是META最常见的用法,在制作网页时 ...

  5. android中ListView控件

    今天学习了ListView控件和页面跳转,下面大致介绍下: 第一步:创建显示内容的文件vlist.xml: <?xml version="1.0" encoding=&quo ...

  6. wordpress 后台显示空白现象

    简单的说两句,出现此种现象的因素可能在于主题或者插件再或者是因为(恶意)插件(误更改)更改了某个重要的文件出现错误.本次我遇到的是插件的错误,具体是什么错误,我也没有去深究,重要的是结果! 使用排查的 ...

  7. 继续(3n+1)猜想

    卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程中遇到的每一个数.例如对n=3进行验证的时候, ...

  8. ubuntu亮度无法自动调节终极解决方案

    友情链接:IT狂人博客 这个问题纠结了我快两年,主要是自己懒,写了个脚本来调节亮度,不过还是稍显不便.近日兴起折腾了一番,终于找到问题根结了. There are many ways to contr ...

  9. 为什么每个浏览器都有Mozilla

    你是否好奇标识浏览器身份的User-Agent,为什么每个浏览器都有Mozilla字样? Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 ...

  10. 一、C# 概述

    1.托管执行环境 2.程序集 3.关键字:C#保留字 4.除了C#定义的关键字之外,开发者可以提供他们自己的名称,编程语言将这些名称称为标识符 5.如果关键字包含一个"@"前缀,那 ...