在Lucene搜索中将+作为特殊字符进行处理 [英] Handling + as a special character in Lucene search

查看:96
本文介绍了在Lucene搜索中将+作为特殊字符进行处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的输入字符串包含诸如c ++之类的术语时,如何确保lucene给我返回相关的搜索结果? Lucene似乎忽略了++字符.

How do i make sure lucene gives me back relevant search results when my input string contains terms like c++? Lucene seems to ignore ++ characters.

代码详细信息: 当我执行此行时,我得到一个空白的搜索查询.

Code details: When I execute this line,I get a blank search query.

queryField = multiFieldQueryParser.Parse(inpKeywords);

keywordsQuery.Add(queryField, BooleanClause.Occur.SHOULD);

这是我的自定义分析器:

And here is my custom analyzer:

public class CustomAnalyzer : Analyzer
    {
        private static readonly WhitespaceAnalyzer whitespaceAnalyzer = new WhitespaceAnalyzer();
    public override TokenStream TokenStream(String fieldName, System.IO.TextReader reader)
        {
            TokenStream result = whitespaceAnalyzer.TokenStream(fieldName, reader);
            result = new StandardTokenizer(reader);
            result = new LowerCaseFilter(result);
            result = new StopFilter(result, stop_words);
            return result;
        }
}

我正在以这种方式执行搜索查询:

And I'm executing search query this way:

indexSearcher.Search(searchQuery, collector);

我确实尝试使用queryField = multiFieldQueryParser.Parse(QueryParser.Escape(inpKeywords));,但仍然无法正常工作.这是被执行并返回零命中的查询. "+(())"

I did try queryField = multiFieldQueryParser.Parse(QueryParser.Escape(inpKeywords));,but it still does not work. Here is the query which get executed and returns zero hits. "+(())"

谢谢.

推荐答案

由于+是特殊字符,因此需要转义. 此处(请参见页面.)

Since, + is a special character, it needs to be escaped. The list of all characters that need to be escaped is here (See bottom of the page.)

您还需要注意在编制索引时使用的分析仪.例如,StandardAnalyzer将跳过+.在索引和搜索时,您可能需要使用WhiteSpaceAnalyzer之类的东西,这将在令牌流中保留特殊字符.请记住,在建立索引和搜索时,您需要使用相同的分析器.

You also need to be careful about the analyzer you use while indexing. For example, StandardAnalyzer will skip +. You may need to use something like WhiteSpaceAnalyzer while indexing and searching, which will preserve special characters in the tokenstream. Keep in mind that you need to use the same analyzer while indexing and searching.

这篇关于在Lucene搜索中将+作为特殊字符进行处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆