Search code examples
apilucenefacet

Lucene 4.1 how to index facets?


I try to build a index with some facets, I'm folliwing the User Guide.

However, I run into a problem; the next line in the User Guide gives errors.

DocumentBuilder categoryDocBuilder = new CategoryDocumentBuilder(taxo);

Both the DocumentBuilder and the CatergoryDocumentBuilder do not exist in lucene-facet..

I cannot find the API changes in the Jira-issues.. Does anyone have this working and cares to share how it should be done?


Solution

  • I figured it out using the benchmark code as inspiration.

    Indexing

    Directory dir      = FSDirectory.open( new File("index" ));
    Directory dir_taxo = FSDirectory.open( new File("index-taxo" ));
    IndexWriter writer = newIndexWriter(dir);
    TaxonomyWriter taxo = new DirectoryTaxonomyWriter(dir_taxo, OpenMode.CREATE);
    FacetFields ff= new FacetFields(taxo);
    
    //for all documents:
    d=new Document();
    List<CategoryPath>=new ArrayList<CategoryPath>();    
    
    for (all fields in doc)
    {
        d.addField( ....)
    }
    for (all categories in doc)
    { 
        CategoryPath cp = new CategoryPath(field, value);
        categories.add( cp);
        taxo.addCategory(cp); //not sure if necessary
    }
    
    ff.addFields(d, categories);
    w.addDocument( d );
    

    Searching:

    Directory dir = FSDirectory.open( new File("index" ));
    Directory dir_taxo = FSDirectory.open( new File("index-taxo" ));
    
    final DirectoryReader indexReader = DirectoryReader.open(dir);
    IndexSearcher searcher = new IndexSearcher(indexReader);
    TaxonomyReader taxo = new DirectoryTaxonomyReader(dir_taxo);
    
    Query q = new TermQuery(new Term(...));
    TopScoreDocCollector tdc = TopScoreDocCollector.create(1, true);
    FacetSearchParams facetSearchParams = new FacetSearchParams(new CountFacetRequest(new CategoryPath("mycategory"),10));
    FacetsCollector facetsCollector = new FacetsCollector(facetSearchParams, indexReader, taxo);
    long ts= System.currentTimeMillis();
    searcher.search(q, MultiCollector.wrap(tdc, facetsCollector));
    List<FacetResult> res = facetsCollector.getFacetResults();  
    long te= System.currentTimeMillis();
    for (FacetResult fr:res)
    {
        for ( FacetResultNode sr : fr.getFacetResultNode().getSubResults())
            {
               System.out.println(String.format( "%s :  %f", sr.getLabel(), sr.getValue()));
            }
        }
        System.out.println(String.format("Search took: %d millis", (te-ts)));
    }