Lucene.NET& Facete搜索解决方案

时间:2011-04-13 08:19:19

标签: asp.net search lucene lucene.net

嘿刚刚开始使用Lucene.NET,有人想知道是否有人有一个带有分面搜索的Lucene.NET的实例。

我知道以下链接http://www.devatwork.nl/articles/lucenenet/faceted-search-and-drill-down-lucenenet/

这看起来很棒,但它只是告诉我分面搜索中的结果数量,但实际上并不是我如何检索索引和这些结果的详细信息。即在Lucene.NET中的正常搜索中。

即从该链接中他有以下代码段

    private static void FacetedSearch(string indexPath, string genre, string term){
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);
}

这将告诉我,即搜索词“都柏林”有2条记录,并且属于“金融”类型,这是完美的,但文章似乎跳过它说我如何检索这些结果的索引的部分并在屏幕上显示。

他确实在下面的链接中解释了这一点,以进行正常搜索但不进行面部搜索..

即正常搜索

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

// create a query which searches through the title and description, the term can be in the title or the description
Query searchQuery = MultiFieldQueryParser.Parse(term, new[] {"title", "description"}, new[] {BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD}, new StandardAnalyzer());

// perform the search
Hits hits = searcher.Search(searchQuery);

// loop through all the hits and show their title
for (int hitIndex = 0; hitIndex < hits.Length(); hitIndex++)
{
// get the corresponding document
Document hitDocument = hits.Doc(hitIndex);

// write its title to the console
Console.WriteLine(hitDocument.GetField("title").StringValue());
}
}

http://www.devatwork.nl/articles/lucenenet/search-basics-lucenenet/

非常感谢任何帮助

编辑:

或者我应该进行搜索查询,然后对结果进行过滤?

1 个答案:

答案 0 :(得分:0)

BitArray表示命中。每个1都有一个索引,它等于文档ID

所以1001001意味着索引中位置0,3和6的文档与您的搜索匹配。你只需要从lucene索引中检索它们。

var searcher = new IndexSearcher(indexPath);

// get document at position 0
var doc = searcher.Doc( 0 );