Lucene学习之Facet

来源:互联网 发布:快手字幕软件 编辑:程序博客网 时间:2024/05/22 17:19

Facet简单来说就是点击某个品牌或者网络,获取更细分的结果。也就是站在不同的方面去搜索会得到不同的结果,其主要API支持,我们通过一段代码来看

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.DrillDownQuery;import org.apache.lucene.facet.DrillSideways;import org.apache.lucene.facet.FacetField;import org.apache.lucene.facet.FacetResult;import org.apache.lucene.facet.Facets;import org.apache.lucene.facet.FacetsCollector;import org.apache.lucene.facet.FacetsConfig;import org.apache.lucene.facet.taxonomy.FastTaxonomyFacetCounts;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;public class SimpleFacetsExample {    private final Directory indexDir = new RAMDirectory();    private final Directory taxoDir = new RAMDirectory();    private final FacetsConfig config = new FacetsConfig();    public SimpleFacetsExample(){        //设置多值域        this.config.setHierarchical("Author", true);        this.config.setHierarchical("Publish Date", true);    }    private void index() throws IOException{        IndexWriter indexWriter = new IndexWriter(this.indexDir,                new IndexWriterConfig(                        new WhitespaceAnalyzer()).setOpenMode                        (IndexWriterConfig.OpenMode.CREATE));//create a new index        DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(this.taxoDir);        Document doc = new Document();        doc.add(new FacetField("Author",new String[]{"Bob"}));        doc.add(new FacetField("Publish Date",new String[]{"2010","10","15"}));        indexWriter.addDocument(this.config.build(taxoWriter, doc));        doc = new Document();        doc.add(new FacetField("Author",new String[]{"Lisa"}));        doc.add(new FacetField("Publish Date",new String[]{"2010","10","20"}));        indexWriter.addDocument(this.config.build(taxoWriter,doc));        doc = new Document();        doc.add(new FacetField("Author",new String[]{"Lisa"}));        doc.add(new FacetField("Publish Date",new String[]{"2012","1","1"}));        indexWriter.addDocument(this.config.build(taxoWriter,doc));        doc = new Document();        doc.add(new FacetField("Author",new String[]{"Susan"}));        doc.add(new FacetField("Publish Date",new String[]{"2012","1","7"}));        indexWriter.addDocument(this.config.build(taxoWriter,doc));        doc = new Document();        doc.add(new FacetField("Author",new String[]{"Frank"}));        doc.add(new FacetField("Publish Date",new String[]{"1999","5","5"}));        indexWriter.addDocument(this.config.build(taxoWriter,doc));        indexWriter.close();        taxoWriter.close();    }        private List<FacetResult> facetsWithSearch() throws IOException{            DirectoryReader indexReader = DirectoryReader.open(this.indexDir);            IndexSearcher searcher = new IndexSearcher(indexReader);            TaxonomyReader taxoReader = new DirectoryTaxonomyReader(this.taxoDir);            FacetsCollector fc = new FacetsCollector();            FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc);//收集命中结果            List<FacetResult> results = new ArrayList<FacetResult>();            Facets facets = new FastTaxonomyFacetCounts(taxoReader,this.config,fc);//建立分组            results.add(facets.getTopChildren(10, "Author", new String[0]));//向结果集中添加搜索结果            results.add(facets.getTopChildren(10, "Publish Date", new String[0]));//只选取年份            indexReader.close();            return results;        }        private List<FacetResult> facetsOnly() throws IOException{            DirectoryReader indexReader = DirectoryReader.open(this.indexDir);            IndexSearcher searcher = new IndexSearcher(indexReader);            TaxonomyReader taxoReader = new DirectoryTaxonomyReader(this.taxoDir);            FacetsCollector fc = new FacetsCollector();//搜索结果收集器            searcher.search(new MatchAllDocsQuery(),null, fc);//没有使用facet收集器            List<FacetResult> results = new ArrayList<FacetResult>();            Facets facets = new FastTaxonomyFacetCounts(taxoReader,this.config,fc);            results.add(facets.getTopChildren(10, "Author"));            results.add(facets.getTopChildren(10, "Publish Date"));//            indexReader.close();            return results;        }        private FacetResult drillDown() throws IOException {              DirectoryReader indexReader = DirectoryReader.open(this.indexDir);              IndexSearcher searcher = new IndexSearcher(indexReader);              TaxonomyReader taxoReader = new DirectoryTaxonomyReader(this.taxoDir);             DrillDownQuery q = new DrillDownQuery(this.config);// A Query for drill-down over facet categories             q.add("Publish Date", new String[] { "2010" }); //新建查询            FacetsCollector fc = new FacetsCollector();              FacetsCollector.search(searcher, q, 10, fc);              Facets facets = new FastTaxonomyFacetCounts(taxoReader, this.config, fc);              FacetResult result = facets.getTopChildren(10, "Author", new String[0]); //只选取Author域的内容            //FacetResult result = facets.getTopChildren(10, "Author", new String[0]);            indexReader.close();              taxoReader.close();              return result;              }           private List<FacetResult> drillSideways() throws IOException {               DirectoryReader indexReader = DirectoryReader.open(this.indexDir);               IndexSearcher searcher = new IndexSearcher(indexReader);               TaxonomyReader taxoReader = new DirectoryTaxonomyReader(this.taxoDir);               DrillDownQuery q = new DrillDownQuery(this.config);               q.add("Publish Date", new String[] { "2010" });               DrillSideways ds = new DrillSideways(searcher, this.config, taxoReader);             //Computes drill down and sideways counts for the provided DrillDownQuery               DrillSideways.DrillSidewaysResult result = ds.search(q, 10);               //Use one of the static search methods to do the search, and then get              //the hits and facet results from the returned DrillSideways.DrillSidewaysResult             List<FacetResult> facets = result.facets.getAllDims(10);               indexReader.close();               taxoReader.close();               return facets;               }           public List<FacetResult> runFacetOnly() throws IOException {               index();               return facetsOnly();               }           public List<FacetResult> runSearch() throws IOException {               index();               return facetsWithSearch();               }           public FacetResult runDrillDown() throws IOException {               index();               return drillDown();               }           public List<FacetResult> runDrillSideways() throws IOException {               index();               return drillSideways();               }           public static void main(String[] args) throws Exception {               SimpleFacetsExample example = new SimpleFacetsExample();                             // two               System.out.println("Facet counting example (combined facets and search):");               System.out.println("-----------------------");               List<FacetResult> results = example.runSearch();               System.out.println("Author: " + results.get(0));               System.out.println("Publish Date: " + results.get(1));               // one               System.out.println("Facet counting example(only):");               System.out.println("-----------------------");               List<FacetResult> results1 = example.runFacetOnly();               System.out.println("Author: " + results1.get(0));               System.out.println("Publish Date: " + results1.get(1));                       // three               System.out.println("Facet drill-down example (Publish Date/2010):");               System.out.println("---------------------------------------------");               System.out.println("Author: " + example.runDrillDown());                       // four               System.out.println("Facet drill-sideways example (Publish Date/2010):");               System.out.println("---------------------------------------------");               for (FacetResult result : example.runDrillSideways()) {                   System.out.println(result);               }               }  }

可以看到,Facets的构是由

Facets facets = new FastTaxonomyFacetCounts(taxoReader,this.config,fc);

FastTaxonomyFacetCounts的构造函数完成的,而FastTaxonomyFacetCounts的构造则需要Facets模块专门的FacetConfig以及FacetCollector,,然后,facet模块中还提供了DrilldownQuery来在facet之上钻取数据,辅助DrilldownQuery的有一个继承自Object的类DrillSideways,该类直接由reader,FacetConfig还有FacetCollector构建,并且调用方法DrillSidewaysResult()来获取facet结果(统计某一组域的结果的总数)。
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
运行结果

0 0
原创粉丝点击