1.facet的直观认识

facet:面、切面、方面。个人理解就是维度,在满足query的前提下,观察结果在各维度上的分布(一个维度下各子类的数目)。

如jd上搜“手机”,得到4009个商品。其中品牌、网络、价格就是商品的维度(facet),点击某个品牌或者网络,获取更细分的结果。

点击品牌小米,获得小米手机的结果,显示27个。

点击移动4G,获得移动4G、小米手机,显示4个。

2.facet特性

  • facet counting:返回一个facet下某子类的结果数。如上面的品牌维度下小米子类中满足查询"手机"的结果有27个。
  • facet associations:一个文档与某子类的关联度,如一本书30%讲lucene,70%讲solor,这个百分比就是书与分类的关联度(匹配度、信心度)。
  • multiple facet requests:支持多facet查询(多维度查询)。如查询品牌为小米、网络为移动4G的手机。

3.实例

一个facet简单使用例子,依赖于lucene-facet-4.10.0。讲述了从搜手机到品牌、到网络向下browser的过程。

public class SimpleFacetsExample {
private final Directory indexDir = new RAMDirectory();
private final Directory taxoDir = new RAMDirectory();
private final FacetsConfig config = new FacetsConfig(); /** Empty constructor */
public SimpleFacetsExample() {
config.setHierarchical("Publish Date", true);
} /** Build the example index. */
private void index() throws IOException {
IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(Version.LUCENE_4_10_0,
new WhitespaceAnalyzer()));
// Writes facet ords to a separate directory from the main index
DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir); Document doc = new Document();
doc.add(new TextField("device", "手机", Field.Store.YES));
doc.add(new TextField("name", "米1", Field.Store.YES));
doc.add(new FacetField("brand", "小米"));
doc.add(new FacetField("network", "移动4G"));
indexWriter.addDocument(config.build(taxoWriter, doc)); doc = new Document();
doc.add(new TextField("device", "手机", Field.Store.YES));
doc.add(new TextField("name", "米4", Field.Store.YES));
doc.add(new FacetField("brand", "小米"));
doc.add(new FacetField("network", "联通4G"));
indexWriter.addDocument(config.build(taxoWriter, doc)); doc = new Document();
doc.add(new TextField("device", "手机", Field.Store.YES));
doc.add(new TextField("name", "荣耀6", Field.Store.YES));
doc.add(new FacetField("brand", "华为"));
doc.add(new FacetField("network", "移动4G"));
indexWriter.addDocument(config.build(taxoWriter, doc)); doc = new Document();
doc.add(new TextField("device", "电视", Field.Store.YES));
doc.add(new TextField("name", "小米电视2", Field.Store.YES));
doc.add(new FacetField("brand", "小米"));
indexWriter.addDocument(config.build(taxoWriter, doc)); taxoWriter.close();
indexWriter.close();
} private void facetsWithSearch() throws IOException {
DirectoryReader indexReader = DirectoryReader.open(indexDir);
IndexSearcher searcher = new IndexSearcher(indexReader);
TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir); FacetsCollector fc = new FacetsCollector();
//1.查询手机
System.out.println("-----手机-----");
TermQuery query = new TermQuery(new Term("device", "手机"));
FacetsCollector.search(searcher, query, 10, fc);
Facets facets = new FastTaxonomyFacetCounts(taxoReader, config, fc);
List<FacetResult> results = facets.getAllDims(10);
//手机总共有3个,品牌维度:小米2个,华为1个;网络维度:移动4G 2个,联通4G 1个
for (FacetResult tmp : results) {
System.out.println(tmp);
}
//2.drill down,品牌选小米
System.out.println("-----小米手机-----");
DrillDownQuery drillDownQuery = new DrillDownQuery(config, query);
drillDownQuery.add("brand", "小米");
FacetsCollector fc1 = new FacetsCollector();//要new新collector,否则会累加
FacetsCollector.search(searcher, drillDownQuery, 10, fc1);
facets = new FastTaxonomyFacetCounts(taxoReader, config, fc1);
results = facets.getAllDims(10);
//获得小米手机的分布,总数2个,网络:移动4G 1个,联通4G 1个
for (FacetResult tmp : results) {
System.out.println(tmp);
} //3.drill down,小米移动4G手机
System.out.println("-----移动4G小米手机-----");
drillDownQuery.add("network", "移动4G");
FacetsCollector fc2 = new FacetsCollector();
FacetsCollector.search(searcher, drillDownQuery, 10, fc2);
facets = new FastTaxonomyFacetCounts(taxoReader, config, fc2);
results = facets.getAllDims(10);
for (FacetResult tmp : results) {
System.out.println(tmp);
} //4.drill sideways,横向浏览
//如果已经进入了小米手机,但是还想看到其他牌子(华为)的手机数目,就用到了sideways
System.out.println("-----小米手机drill sideways-----");
DrillSideways ds = new DrillSideways(searcher, config, taxoReader);
DrillDownQuery drillDownQuery1 = new DrillDownQuery(config, query);
drillDownQuery1.add("brand", "小米");
DrillSidewaysResult result = ds.search(drillDownQuery1, 10);
results = result.facets.getAllDims(10);
for (FacetResult tmp : results) {
System.out.println(tmp);
} indexReader.close();
taxoReader.close();
} /** Runs the search and drill-down examples and prints the results. */
public static void main(String[] args) throws Exception {
SimpleFacetsExample example = new SimpleFacetsExample();
example.index();
example.facetsWithSearch();
}
}

输出:

-----手机-----
//总数3个,2个子类
dim=brand path=[] value=3 childCount=2
小米 (2)
华为 (1) dim=network path=[] value=3 childCount=2
移动4G (2)
联通4G (1) -----小米手机-----
//普通向下浏览,丢失了同一维度,其他子类的统计
dim=brand path=[] value=2 childCount=1
小米 (2) dim=network path=[] value=2 childCount=2
移动4G (1)
联通4G (1) -----移动4G小米手机-----
dim=brand path=[] value=1 childCount=1
小米 (1) dim=network path=[] value=1 childCount=1
移动4G (1) -----小米手机drill sideways-----
//drill sideways, 保留了该drill维度的其他子类统计
dim=brand path=[] value=3 childCount=2
小米 (2)
华为 (1)
//小米手机中的网络分布
dim=network path=[] value=2 childCount=2
移动4G (1)
联通4G (1)

Lucene系列-facet的更多相关文章

  1. Lucene系列二:Lucene(Lucene介绍、Lucene架构、Lucene集成)

    一.Lucene介绍 1. Lucene简介 最受欢迎的java开源全文搜索引擎开发工具包.提供了完整的查询引擎和索引引擎,部分文本分词引擎(英文与德文两种西方语言).Lucene的目的是为软件开发人 ...

  2. Lucene系列-facet--转

    https://blog.csdn.net/whuqin/article/details/42524825 1.facet的直观认识 facet:面.切面.方面.个人理解就是维度,在满足query的前 ...

  3. lucene中facet实现统计分析的思路——本质上和word count计数无异,像splunk这种层层聚合(先filed1统计,再field2统计,最后field3统计)lucene是排序实现

    http://stackoverflow.com/questions/185697/the-most-efficient-way-to-find-top-k-frequent-words-in-a-b ...

  4. Lucene系列-FieldCache

    域缓存,加载所有文档中某个特定域的值到内存,便于随机存取该域值. 用途及使用场景 当用户需要访问各文档中某个域的值时,IndexSearcher.doc(docId)获得Document的所有域值,但 ...

  5. [lucene系列笔记1]lucene6的安装与配置(Windows系统)

    lucene是一个java开源的高效全文检索工具包,最近做项目要用到,把学习的过程记录一下. 第一步:下载安装jdk 1.首先从官网下载jdk(下载之前先查看你的电脑是多少位操作系统,如果是32就下载 ...

  6. Lucene系列-索引文件

    本文介绍下lucene生成的索引有哪些文件组成,每个文件包含了什么信息.基于Lucene 4.10.0. 数据结构 索引(index)包含了存储的文档(document)正排.倒排信息,用于文本搜索. ...

  7. Lucene系列-近实时搜索(1)

    近实时搜索(near-real-time)可以搜索IndexWriter还未commit的内容,介于immediate和eventual之间,在数据比较大.更新较频繁的情况下使用.本文主要来介绍下如何 ...

  8. Lucene系列-搜索

    Lucene搜索的时候就要构造查询语句,本篇就介绍下各种Query.IndexSearcher是搜索主类,提供的常用查询接口有: TopDocs search(Query query, int n); ...

  9. Lucene系列-分析器

    分析器介绍 搜索的基础是对文本信息进行分析,Lucene的分析工具在org.apache.lucene.analysis包中.分析器负责对文本进行分词.语言处理得到词条,建索引和搜索的时候都需要用到分 ...

随机推荐

  1. react-router配合webpack实现按需加载

    很久没有写博客了.一直感觉没有什么要写的,但是这个东西确实有必要的.使用react开发,不可能一直打包到一个文件.小项目肯定没有问题,但是变大一旦到几兆,这个问题就很严重.现在又Commonjs,AM ...

  2. 拒绝try.catch泛滥,学习委托有感

    读了一位博友关于使用委托避免重复的try.catch的随笔(原文地址:http://www.cnblogs.com/foolishfox/archive/2010/07/30/1788416.html ...

  3. Opacity多浏览器透明度兼容处理

    用来设定元素透明度的 Opacity 是CSS 3里的一个属性.当然现在还只有少部分浏览器支持. 不过各个浏览器都有自己的私有属性来支持,其中包括老版本的Mozilla和Safari: IE: fil ...

  4. 在Windows7 下调试CodeSmith 注意事项

    编写CodeSmith模板和编写程序一样,也需要进行调试,CodeSmith支持使用CLR’s Just-in-Time debugger调试模板. 要调试模板,首先要在CodeTemplate声明中 ...

  5. 提取c#代码文件中的方法块

       此方法是取C#文件里面的方法块,并删除缩进符,感觉写得还是比较容易懂的,所以收藏下,以便将来用到. private static string GetCodeBlock(string allCo ...

  6. <转>简单之美——系统设计黄金法则

    作者: 包云岗  发布时间: 2012-05-19 13:06  阅读: 3036 次  推荐: 1   原文链接   [收藏] 最近多次看到系统设计与实现的文章与讨论,再加上以前读过的其他资料以及自 ...

  7. GetRelevantAnimTimeRemainingFraction节点Bug

    初始设置: 动画状态机: MoveStop->Idle的条件: 使用该节点的目的: 在动画蓝图的过渡条件中使用该节点,保证上一个状态的动作完成后进入下一个动作.需求是MoveStop动作完成后才 ...

  8. 查看上下文切换的多的进程(find which process take the most context switch)

    这是原文链接http://serverfault.com/questions/190049/find-out-which-task-is-generating-a-lot-of-context-swi ...

  9. mysql 性能配置优化

    修改mysql配置文件 my.cnf ,内容如下: [mysqld]datadir=/data/mysql/datasocket=/var/lib/mysql/mysql.sockuser=mysql ...

  10. Texture2D.GetPixelBilinear(float u, float v)的使用,官方例子注释

    using UnityEngine; using System.Collections; public class TEST : MonoBehaviour { public Texture2D so ...