`
--晴天--
  • 浏览: 3378 次
社区版块
存档分类
最新评论

SimpleFacetsExample

阅读更多

/**
 * @author wuchangyang
 */
package 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;

/**
 * @author wuchangyang
 * Shows simple usage of faceted indexing and search
 */
public class SimpleFacetsExample {
  private final Directory indexDir = new RAMDirectory();
  private final Directory taxoDir = new RAMDirectory();
  public SimpleFacetsExample(){
  
  }
 
  private void add(IndexWriter writer,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);
   writer.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 spearate directory from the main index
   DirectoryTaxonomyWriter taxonomyWriter=new DirectoryTaxonomyWriter(taxoDir);
   //Reuse across documents,to add the necessary facet fields
   FacetFields facetFields=new FacetFields(taxonomyWriter);
   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();
   taxonomyWriter.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"), 5),
      new CountFacetRequest(new CategoryPath("Author"), 5));
   // 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'. drills down(深化)*/
     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/10/15", '/'));
      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);
    }
  }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics