我希望我的 Lucene 查询包含类似于以下内容的内容:
公司名称:梅赛德斯卡车
它将与 companyName 字段中的字符串“mercedes trucks”进行完全匹配。
companyName 是一个未标记的字段,但任何带有空格的内容都会返回空结果。
new TermQuery(new Term("companyName", "mercedes trucks"));
如果涉及空格,则始终产生 0 个结果。否则我的程序运行良好。
我希望我的 Lucene 查询包含类似于以下内容的内容:
公司名称:梅赛德斯卡车
它将与 companyName 字段中的字符串“mercedes trucks”进行完全匹配。
companyName 是一个未标记的字段,但任何带有空格的内容都会返回空结果。
new TermQuery(new Term("companyName", "mercedes trucks"));
如果涉及空格,则始终产生 0 个结果。否则我的程序运行良好。
也许更换:
mercedes trucks
和
mercedes?trucks
为我工作。
像这样使用 PhraseQuery:
//create the query objects
BooleanQuery query = new BooleanQuery();
PhraseQuery q2 = new PhraseQuery();
//grab the search terms from the query string
string[] str = Sitecore.Context.Request.QueryString[BRAND_TERM].Split(' ');
//build the query
foreach(string word in str)
{
//brand is the field I'm searching in
q2.Add(new Term("brand", word.ToLower()));
}
//finally, add it to the BooleanQuery object
query.Add(q2, BooleanClause.Occur.MUST);
//Don't forget to run the query
Hits hits = searcher.Search(query);
希望这可以帮助!
您在搜索时使用的分析器可能与创建索引时使用的分析器不同。
在搜索时尝试使用 KeywordAnalyzer。它将创建搜索字符串的单个标记,这可能是您正在寻找的。
您是否考虑过使用PhraseQuery?该字段是否必须取消标记?我相信 unkenized 是用于 ids 等,而不是用于具有多个单词作为内容的字段。
I'm guessing here - does exactMask
add quotes around the string? You should simply use the string "mercedes truck", without manipulating it.
new TermQuery(new Term("companyName", "mercedes trucks"));
我发现可行的最佳方法是使用关键字分析器和以下查询“mercedes?trucks”来解析查询。
Even I am facing the same issue. You have to do the following thing to get rid of from this issue. 1)When add the field value to the document remove the spaces in between. 2)Make the field value in lowercase. 3)Make the search text in lowercase. 4)Remove the white spaces in the search text. Regards ~shef