我找到了一个很好的示例,可以在基本查询中获取构面计数。它存储基本查询的位数组,以在每次计算构面时提高性能。

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

唯一的问题是,我使用的是Lucene.NET(2.9)的较新版本,而Filter.Bits已过时。我们被告知使用DocIdSet代替(而不是BitArray)。

我无法找到如何使用docIdSet执行bitArray.And(bitArray)。我看了看反射器,发现其中有And操作的OpenIdSet。我只是在说不确定OpenIdSet是否是要走的路。

提前致谢!

最佳答案

找到了。

            var productsDISI = new OpenBitSetDISI(productResults.Iterator(), 25000);
            var termQuery = new TermQuery(new Term("Spec" + expectedFacet.SpecificationId, expectedFacet.SpecificationOptionId.ToString()));
            var termQueryFilter = new QueryWrapperFilter(termQuery);
            var termIterator = termQueryFilter.GetDocIdSet(productReader).Iterator();
            productsDISI.InPlaceAnd(termIterator);
            var total = productsDISI.Cardinality();

事实证明也快得多。

关于Lucene.NET 2.9和BitArray/DocIdSet,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2946608/

10-13 02:14