如果搜索词没有空格,则lucene.net匹配

时间:2016-11-19 05:25:03

标签: c# asp.net-mvc lucene lucene.net

我使用lucene.net在我的c#asp.net应用程序的帖子中执行搜索,这是我的索引中的示例文档:

var doc = new Document();
var title = new Field("Title", "the album hardwired to self-destruct released", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS);
title.Boost = 5;
doc.Add(title);
var ns_title = new Field("NoSpace_Title", "thealbumhardwiredtoselfdesctructreleased", Field.Store.NO, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS);
ns_title.Boost = 5;
doc.Add(ns_title);
doc.Add(new Field("Body", "the body text of the post", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
doc.Add(new Field("Id", "1", Field.Store.YES, Field.Index.NOT_ANALYZED));
writer.AddDocument(doc);

问题
如果我搜索selfdestructself destruct,我就会被点击 如果我搜索selfdestruct我就不会受到打击。

搜索方法

var searchWords = s.Split(' ').ToList();
var directory = GetDirectory();
var reader = IndexReader.Open(directory, true);
var searcher = new IndexSearcher(reader);
var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
var parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, "Title,NoSpace_Title,Body".Split(','), analyzer);
var booleanQuery = new BooleanQuery();

// Title:selfdestruct*NoSpace_Title:selfdestruct*Body:selfdestruct*
s = string.Join(" ", searchWords.Select(x => x.Contains("*") ? x : x + "*"));
Query query = parser.Parse(QueryParser.Escape(s));
query.Boost = 5;
booleanQuery.Add(query, Occur.SHOULD);

// Title:*selfdestruct*,NoSpace_Title:*selfdestruct*,Body:*selfdestruct*
// (I suppose this should work and get hit but it doesn't)
s = "*" + string.Join("", searchWords) + "*";
Query query2 = parser.Parse(QueryParser.Escape(s));
query2.Boost = 3;
booleanQuery.Add(query2, Occur.SHOULD);

// Title:selfdestruct~0.85 (fuzzy search)
s = string.Join(" ", searchWords.Select(x => x.Contains("~") ? x : x + "~0.85"));
Query query3 = parser.Parse(QueryParser.Escape(s));
booleanQuery.Add(query3, Occur.SHOULD);

var collector = TopScoreDocCollector.Create(1000, true);
searcher.Search(booleanQuery, collector);
var hits = collector.TopDocs().ScoreDocs;
var docs = hits.Select(x => searcher.Doc(x.Doc)).ToList();

1 个答案:

答案 0 :(得分:2)

您可以通过在分析仪中添加ShingleFilter来支持此功能。

ShingleFilter会将相邻的令牌组合成单个令牌,以便在没有空格的情况下搜索它们。默认情况下,它也会输出Unigrams(也就是说,它也会保留单个标记)。因此,当您索引&#34; self-destruct&#34;时,它将索引标记&#34; self&#34;,&#34; destruct&#34;和&#34; selfdestruct&#34;。< / p>

在不创建自定义分析器的情况下,一种简单的方法是使用ShingleAnalyzerWrapper

var analyzer = new ShingleAnalyzerWrapper(
        new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30),
        2);