Lucene 4.1如何索引facets?

时间:2013-02-08 14:33:50

标签: api lucene facet

我尝试使用一些方面构建索引,我正在使用用户指南。

然而,我遇到了一个问题; “用户指南”中的下一行会出错。

DocumentBuilder categoryDocBuilder = new CategoryDocumentBuilder(taxo);

DocumentBuilderCatergoryDocumentBuilderlucene-facet都不存在。

我无法在Jira问题中找到API更改。有没有人有这个工作并且愿意分享它应该如何完成?

2 个答案:

答案 0 :(得分:2)

我认为使用基准代码作为灵感。

索引

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)));
}

答案 1 :(得分:0)

我不熟悉Lucene 4.1仅版本2.9。

但是当我在结果中创建facet时,我通常使用Lucene.Net.Search.SimpleFacetedSearch.dll,在我项目的示例代码下面。

的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);
        }