我知道已经问过类似的问题,但我找不到任何适合我正在寻找的答案。
基本上,我想搜索短语并只返回仅具有该确切短语而不是部分匹配的匹配项。
例如,如果我搜索“This is”,则包含“This is a phrase”的文档不应返回命中。
举个例子:Exact Phrase search using Lucene?
"foo bar" 不应返回命中,因为它只是部分匹配。我正在寻找的完整匹配将是“foo bar baz”。
这是代码,感谢 WhiteFang34 在上面的链接中发布此代码(我已简单地转换为 c#):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using Lucene.Net.Documents;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Analysis;
using Lucene.Net.Store;
using Lucene.Net.Index;
namespace LuceneStatic
{
public static class LuceneStatic
{
public static void LucenePhraseQuery()
{
// setup Lucene to use an in-memory index
Lucene.Net.Store.Directory directory = new RAMDirectory();
Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);
var mlf = Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIMITED;
IndexWriter writer = new IndexWriter(directory, analyzer, true, mlf);
// index a few documents
writer.AddDocument(createDocument("1", "foo bar baz"));
writer.AddDocument(createDocument("2", "red green blue"));
writer.AddDocument(createDocument("3", "test foo bar test"));
writer.Close();
// search for documents that have "foo bar" in them
String sentence = "foo bar";
IndexSearcher searcher = new IndexSearcher(directory, true);
PhraseQuery query = new PhraseQuery();
string[] words = sentence.Split(' ');
foreach (var word in words)
{
query.Add(new Term("contents", word));
}
// display search results
List<string> results = new List<string>();
TopDocs topDocs = searcher.Search(query, 10);
foreach (ScoreDoc scoreDoc in topDocs.ScoreDocs)
{
Document doc = searcher.Doc(scoreDoc.doc);
results.Add(doc.Get("contents"));
}
}
private static Document createDocument(string id, string content)
{
Document doc = new Document();
doc.Add(new Field("id", id, Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.Add(new Field("contents", content, Field.Store.YES, Field.Index.ANALYZED,
Field.TermVector.WITH_POSITIONS_OFFSETS));
return doc;
}
}
}
我已经使用不同的分析器和不同的方法来解决这个问题,但我无法获得所需的结果。我需要匹配完整的短语“foo bar baz”,但“foo bar”不应该返回任何命中。