Reputation: 20019
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?
Upvotes: 1
Views: 1454
Reputation: 20019
I figured it out using the benchmark code as inspiration.
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 );
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)));
}
Upvotes: 2
Reputation: 1061
I'm not familiar with Lucene 4.1 only version 2.9.
But when I'm creating facets inside my result I normally use the Lucene.Net.Search.SimpleFacetedSearch.dll, below a sample code of my project.
Wouter
Dictionary<String, long> facetedResults = new Dictionary<String, long>();
try
{
SimpleFacetedSearch.MAX_FACETS = int.MaxValue;
SimpleFacetedSearch sfs = new SimpleFacetedSearch(indexReader, field);
SimpleFacetedSearch.Hits facetedHits = sfs.Search(query);
long totalHits = facetedHits.TotalHitCount;
for (int ihitsPerFacet = 0; ihitsPerFacet < facetedHits.HitsPerFacet.Count(); ihitsPerFacet++)
{
long hitCountPerGroup = facetedHits.HitsPerFacet[ihitsPerFacet].HitCount;
SimpleFacetedSearch.FacetName facetName = facetedHits.HitsPerFacet[ihitsPerFacet].Name;
if (hitCountPerGroup > 0)
facetedResults.Add(facetName.ToString(), hitCountPerGroup);
}
}
catch (Exception ex)
{
facetedResults.Add(ex.Message, -1);
}
Upvotes: 0