Lucene學習之Facet
阿新 • • 發佈:2019-01-02
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結果(統計某一組域的結果的總數)。