1

我尝试建立一个包含一些方面的索引,我正在遵循用户指南。

但是,我遇到了一个问题;用户指南中的下一行给出了错误。

DocumentBuilder categoryDocBuilder = new CategoryDocumentBuilder(taxo);

theDocumentBuilder和 theCatergoryDocumentBuilder都不存在于lucene-facet..

我在 Jira-issues 中找不到 API 更改。有没有人有这个工作并愿意分享它应该如何完成?

4

2 回答 2

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)));
}
于 2013-02-08T15:38:53.473 回答
0

我不熟悉仅 Lucene 4.1 版本 2.9。

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

沃特

        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);
        }
于 2013-02-08T15:09:50.377 回答