Lucene .NET Multi Facets

时间:2011-08-03 07:43:51

标签: lucene.net

如果不使用最新版本的Lucene并启用了分面,我正在尝试实现多方面的技巧。我发现了一个很棒的帖子,但是不确定多值方面的下一步。

以下示例显示了单个示例的代码,例如类别字段,并为每个字段计数。

private static void FacetedSearch(string indexPath, string genre, string term)
{
    // create searcher
    var searcher = new IndexSearcher(indexPath);

    // first get the BitArray result from the genre query
    var genreQuery = new TermQuery(new Term("genre", genre));
    var genreQueryFilter = new QueryFilter(genreQuery);
    BitArray genreBitArray = genreQueryFilter.Bits(searcher.GetIndexReader());
    Console.WriteLine("There are " + GetCardinality(genreBitArray) + " document with the genre " + genre);

    // Next perform a regular search and get its BitArray result
    Query searchQuery = MultiFieldQueryParser.Parse(term, new[] {"title", "description"}, 
                                        new[] {BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD}, 
                                        new StandardAnalyzer());
    var searchQueryFilter = new QueryFilter(searchQuery);
    BitArray searchBitArray = searchQueryFilter.Bits(searcher.GetIndexReader());
    Console.WriteLine("There are " + GetCardinality(searchBitArray) + " document containing the term " + term);

    // Now do the faceted search magic, combine the two bit arrays using a binary AND operation
    BitArray combinedResults = searchBitArray.And(genreBitArray);
    Console.WriteLine("There are " + GetCardinality(combinedResults) + 
                " document containing the term " + term + " and which are in the genre " + genre);
}

但是,如果我有两个单独的字段,例如类别和主题,以便我可以:

  • Category1,Category2

  • 主题1,主题2

在我的用户界面中,如果每个都是复选框,我可以同时选择category1和category2,那么topic1,topic2的计数将与我只选择category1的计数不同。

不确定如何对多个实例进行bitarray搜索。

2 个答案:

答案 0 :(得分:4)

请在Lucene.net顶部查看分面浏览引擎:http://bobo.codeplex.com/ (它是http://javasoze.github.com/bobo/)的端口

如果它不能解决您的需求,请告诉我。

答案 1 :(得分:0)

要计算分面数,您需要获得两次搜索的基数。在上面的示例中,您首次搜索的是带有genre = genre的项目,然后在标题和说明中计算您的字词的构面。

要实现您的目标,您需要创建基本搜索,这将是产品同时属于category1和category2的查询,因此要执行此操作,您将获得一个BooleanQuery来获取初始结果集,就像

var booleanQuery = new BooleanQuery();
booleanQuery.Add(new Term("category", "Category1"));
booleanQuery.Add(new Term("category", "Category2"));
var categoryFilter = new QueryFilter(booleanQuery);
var catBits = categoryFilter.Bits(searcher.GetIndexReader());

然后计算每个方面,您将为每个方面单独创建一个查询(在循环中),然后在类别查询和每个方面查询之间进行getcardinality调用。

foreach (var topic in myTopics) {
    var topicQueryFilter = new QueryFilter(new TermQuery(new Term("topic", topic)));
    var topicBits = topicQueryFilter.Bits(searcher.GetIndexReader());

    var facetResults = catBits.And(topicBits);
    var topicCount = GetCardinality(facetResults);
}