Lucene 4.3 - Facet demo

package com.fox.facet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.facet.index.FacetFields;
import org.apache.lucene.facet.params.FacetSearchParams;
import org.apache.lucene.facet.search.CountFacetRequest;
import org.apache.lucene.facet.search.DrillDownQuery;
import org.apache.lucene.facet.search.FacetResult;
import org.apache.lucene.facet.search.FacetsCollector;
import org.apache.lucene.facet.taxonomy.CategoryPath;
import org.apache.lucene.facet.taxonomy.TaxonomyReader;
import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/** Shows simple usage of faceted indexing and search. */
public class SimpleFacetsExample {

    private final Directory indexDir = new RAMDirectory();
    private final Directory taxoDir = new RAMDirectory();

    /** Empty constructor */
    public SimpleFacetsExample() {
    }

    private void add(IndexWriter indexWriter, FacetFields facetFields, String... categoryPaths) throws IOException {
        Document doc = new Document();

        List<CategoryPath> paths = new ArrayList<CategoryPath>();
        for (String categoryPath : categoryPaths) {
            paths.add(new CategoryPath(categoryPath, ‘/‘));
        }
        facetFields.addFields(doc, paths);
        indexWriter.addDocument(doc);
    }

    /** Build the example index. */
    private void index() throws IOException {
        IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(Version.LUCENE_43, new WhitespaceAnalyzer(
                Version.LUCENE_43)));

        // Writes facet ords to a separate directory from the main index
        DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);

        // Reused across documents, to add the necessary facet fields
        FacetFields facetFields = new FacetFields(taxoWriter);

        add(indexWriter, facetFields, "Author/Bob", "Publish Date/2010/10/15");
        add(indexWriter, facetFields, "Author/Lisa", "Publish Date/2010/10/20");
        add(indexWriter, facetFields, "Author/Lisa", "Publish Date/2012/1/1");
        add(indexWriter, facetFields, "Author/Susan", "Publish Date/2012/1/7");
        add(indexWriter, facetFields, "Author/Frank", "Publish Date/1999/5/5");

        indexWriter.close();
        taxoWriter.close();
    }

    /** User runs a query and counts facets. */
    private List<FacetResult> search() throws IOException {
        DirectoryReader indexReader = DirectoryReader.open(indexDir);
        IndexSearcher searcher = new IndexSearcher(indexReader);
        TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);

        // Count both "Publish Date" and "Author" dimensions
        FacetSearchParams fsp = new FacetSearchParams(new CountFacetRequest(new CategoryPath("Publish Date"), 10),
                new CountFacetRequest(new CategoryPath("Author"), 10));

        // Aggregatses the facet counts
        FacetsCollector fc = FacetsCollector.create(fsp, searcher.getIndexReader(), taxoReader);

        // MatchAllDocsQuery is for "browsing" (counts facets
        // for all non-deleted docs in the index); normally
        // you‘d use a "normal" query, and use MultiCollector to
        // wrap collecting the "normal" hits and also facets:
        searcher.search(new MatchAllDocsQuery(), fc);

        // Retrieve results
        List<FacetResult> facetResults = fc.getFacetResults();

        indexReader.close();
        taxoReader.close();

        return facetResults;
    }

    /** User drills down on ‘Publish Date/2010‘. */
    private List<FacetResult> drillDown() throws IOException {
        DirectoryReader indexReader = DirectoryReader.open(indexDir);
        IndexSearcher searcher = new IndexSearcher(indexReader);
        TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);

        // Now user drills down on Publish Date/2010:
        FacetSearchParams fsp = new FacetSearchParams(new CountFacetRequest(new CategoryPath("Author"), 10));
        DrillDownQuery q = new DrillDownQuery(fsp.indexingParams, new MatchAllDocsQuery());
        q.add(new CategoryPath("Publish Date/2010", ‘/‘));
        FacetsCollector fc = FacetsCollector.create(fsp, searcher.getIndexReader(), taxoReader);
        searcher.search(q, fc);

        // Retrieve results
        List<FacetResult> facetResults = fc.getFacetResults();

        indexReader.close();
        taxoReader.close();

        return facetResults;
    }

    /** Runs the search example. */
    public List<FacetResult> runSearch() throws IOException {
        index();
        return search();
    }

    /** Runs the drill-down example. */
    public List<FacetResult> runDrillDown() throws IOException {
        index();
        return drillDown();
    }

    /** Runs the search and drill-down examples and prints the results. */
    public static void main(String[] args) throws Exception {
        System.out.println("Facet counting example:");
        System.out.println("-----------------------");
        List<FacetResult> results = new SimpleFacetsExample().runSearch();
        for (FacetResult res : results) {
            System.out.println(res);
        }

        System.out.println("\n");
        System.out.println("Facet drill-down example (Publish Date/2010):");
        System.out.println("---------------------------------------------");
        results = new SimpleFacetsExample().runDrillDown();
        for (FacetResult res : results) {
            System.out.println(res);
        }
    }

}

Result:

Facet counting example:
-----------------------
Request: Publish Date nRes=10 nLbl=10
Num valid Descendants (up to specified depth): 3
    Publish Date (0.0)
          Publish Date/2012 (2.0)
          Publish Date/2010 (2.0)
          Publish Date/1999 (1.0)
Request: Author nRes=10 nLbl=10
Num valid Descendants (up to specified depth): 4
    Author (0.0)
          Author/Lisa (2.0)
          Author/Frank (1.0)
          Author/Susan (1.0)
          Author/Bob (1.0)

Facet drill-down example (Publish Date/2010):
---------------------------------------------
Request: Author nRes=10 nLbl=10
Num valid Descendants (up to specified depth): 2
    Author (0.0)
          Author/Lisa (1.0)
          Author/Bob (1.0)
时间: 2024-10-04 23:53:37

Lucene 4.3 - Facet demo的相关文章

lucene 4.0 - Facet demo

package com.fox.facet; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyze

Lucene 4.8 - Facet Demo

package com.fox.facet; /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF lice

一步一步跟我学习lucene(16)---lucene搜索之facet查询查询示例(2)

本篇是接一步一步跟我学习lucene(14)---lucene搜索之facet索引原理和facet查询实例(http://blog.csdn.net/wuyinggui10000/article/details/45973769),上篇主要是统计facet的dim和每个种类对应的数量,个人感觉这个跟lucene的group不同的在于facet的存储类似于hash(key-field-value)形式的,而group则是单一的map(key-value)形式的,虽然都可以统计某一品类的数量,显然f

一步一步跟我学习lucene(14)---lucene搜索之facet查询原理和facet查询实例

Facet说明 我们在浏览网站的时候,经常会遇到按某一类条件查询的情况,这种情况尤以电商网站最多,以天猫商城为例,我们选择某一个品牌,系统会将该品牌对应的商品展示出来,效果图如下: 如上图,我们关注的是品牌,选购热点等方面,对于类似的功能我们用lucene的term查询当然可以,但是在数据量特别大的情况下还用普通查询来实现显然会因为FSDirectory.open等耗时的操作造成查询效率的低下,同时普通查询是全部document都扫描一遍,这样显然造成了查询效率低: lucene提供了facet

lucene学习记录(一)--lucene demo的学习

敬伟大的实践出真知! 以前研究过全文检索,不过当时重点放在了使用上,而且当时重点放在了基于lucene之上的工具zoie,没有时间好好研究一下真正的实现内容.故现在闲暇时间好好看看官网,研究一下lucene这个全文检索的根.由于水平有限,很多地方比较浅显而且可能会有错误,请看官海涵,敬请指正! 本篇文章直接跳过lucene的各种介绍,援引等等,直接从lucene自带的demo开始记录. 我使用的lucene版本是4.10.2.下载地址:下载,因为我使用的Windows环境,故直接下载了zip包,

Facet with Lucene

Facets with Lucene Posted on August 1, 2014 by Pascal Dimassimo in Latest Articles During the development of our latest product, Norconex Content Analytics, we decided to add facets to the search interface. They allow for exploring the indexed conten

Lucene 之 Facet

说到Facet,我还真找不到一个合适的中文词汇来描述它,英文翻译是方面,感觉不够贴切,大家也不必纠结它的中文叫法是啥,你只需要知道使用Facet能解决什么类型的问题就行了,来看几个典型的应用案例:           看了上面几张图,大家应该知道Facet是用来干嘛的了,如果非要用语言描述Facet的用途,那Facet的用途就是根据域的域值进行分组统计,注意这 里的域必须是FacetField,你Facet域的域值有几个就会分几组,并统计在Query查询条件下各组的命中结果数量.但通常不需要显示

Lucene教程具体解释

注明:本文是由本人在开发有关基于lucene资源检索系统时的一点总结,当中一部分是自己依据开发过程自己总结的,也有部分是摘自网络,因无法获取当时摘文的地址,所以在此没有写源地址. 转载请声明出处 Lucene-3.0.0配置 一.Lucene开发环境配置 step1.Lucene开发包下载 step2.Java开发环境配置 step3.Tomcat安装 step4.Lucene开发环境配置 解压下载的lucene-3.0.0.zip,能够看到lucene-core-3.0.0.jar和lucen

Lucene .NET 全文检索

近期做项目中有用到过Lucene,那个模块是由一位前端大神负责的,空闲时间我也做了个关于Lucene做全文检索的Demo,记录下来,方便以后学习. 关于Lucene的原理,网上有长篇大论的文章,有兴趣的话可以去阅读,再次我就直奔主题,在代码中分析其原理. 1.创建索引(此处我用的是盘古分词) 注:在后台代码的第一行上加上 #define notes这样一行代码,目的是可以用外侧代码的#if,作用嘛 用过之后就很明白了,嘿嘿. #region 创建索引 void CreateIndex(objec