0

您好我正在尝试使用 Lucene 库来搜索超过 17 万条记录的自动完成系统。

但是有一个小问题。

例如,当我搜索 Candice Gra(...) 时,它会带来类似的记录

Candice Jackson
Candice Hamilton
Candice Hayes

但是不要 Candice Graham让 Lucene 发现Candice Graham我需要Candice Graham准确输入。

这是我正在构建查询的代码。

Directory directory = FSDirectory.Open(new DirectoryInfo(context.Server.MapPath("
ISet<string> stopWordSet = new HashSet<string>(stopWords);
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30, stopWordSet);

IndexReader indexReader = IndexReader.Open(directory, true);
Searcher indexSearch = new IndexSearcher(indexReader);

//Singe Field Search
var queryParser = new QueryParser(Version.LUCENE_30,
                        "Title",
                        analyzer);
string strQuery = string.Format("{0}", q);
var query = queryParser.Parse(strQuery);

如果我像这样构建 strQuery(* 附加到查询中)

string strQuery = string.Format("{0}*", q);

但是使用这种方式也会带来不相关的记录。例如,如果我再次搜索 Candice Gra(...) 它会返回类似的记录

Grass
Gravity
Gray (etc.)

顺便说一句,我使用了 KeywordAnalyzer 和 SimpleAnalyzer,但这些也不起作用。有任何想法吗?

4

2 回答 2

2

如果您希望它们包含在搜索中,您应该转义您的空间;

var query = queryParser.Parse(QueryParser.Escape(strQuery));
于 2013-09-17T12:18:04.383 回答
1

我认为您需要在这两个词之间放置一个 AND 关键字。

“坎迪斯”和“格拉”

http://lucene.apache.org/core/2_9_4/queryparsersyntax.html#AND

于 2013-09-16T11:56:46.767 回答