开发者

Lucene .NET Multi Facets

Without using the latest version of Lucene with faceting enabled, I am trying to implement multi-facets hitcounts. I have found a great post to get started, but not sure of the next step of multi-value facets.

The below example shows code for a singular example, e.g. Category field, and getting count for each one.

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 "开发者_Go百科 + 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);
}

But, if I have two separate fields, e.g. Category and Topic, so that I could have:

  • Category1, Category2

  • Topic1, Topic2

in my UI, if each of these is a checkbox, I could select both category1, and category2, then the counts for topic1, topic2 would be different than if I just select category1.

Not sure how to do the bitarray searches for that multiple instance.


Please take a look at faceted browse engine on a top of Lucene.net here: http://bobo.codeplex.com/ (it is a port from http://javasoze.github.com/bobo/)

Let me know if it doesn't solve your needs.


To work out the facet counts you need to get the cardinality of two searches. In your example above your first search is for items with genre = genre, and then counting the facets for your term in title and description.

To achieve what you are after, you need to create your base search, which will be a query where the product is in both category1 AND category2, so to do this you would have a BooleanQuery to get your initial result set, this would be something like

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

To then count for each facet you would create a query for each facet individually (in a loop) and then do your getcardinality call between your category query and each facet query.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜