Apache Lucene 5.5.3-搜索以特殊字符结尾的字符串

Apache Lucene 5.5.3-搜索以特殊字符结尾的字符串,lucene,special-characters,Lucene,Special Characters,我正在使用ApacheLucene5.5.3。我在代码中使用了org.apache.lucene.analysis.standard.StandardAnalyzer,并使用下面的代码片段创建索引 Document doc = new Document(); doc.add(new TextField("userName", getUserName(), Field.Store.YES)); 现在,如果我搜索字符串“ALL-”,那么我不会得到任何搜索结果,但是如果我搜索字符串“ALL Cat

我正在使用ApacheLucene5.5.3。我在代码中使用了
org.apache.lucene.analysis.standard.StandardAnalyzer
,并使用下面的代码片段创建索引

Document doc = new Document();

doc.add(new TextField("userName", getUserName(), Field.Store.YES));
现在,如果我搜索字符串“ALL-”,那么我不会得到任何搜索结果,但是如果我搜索字符串“ALL Categories”,那么我会得到一些搜索结果

对于具有特殊字符“+”、“.”、“!”的字符串,也会发生同样的情况等等

以下是我的搜索代码:-

Directory directory = new RAMDirectory();
IndexReader reader = DirectoryReader.open(directory);
IndexSearcher searcher = new IndexSearcher(reader);
Document document = new Document();
document.add(new TextField("body", ALL-THE  GLITTERS IS NOT GOLD, Field.Store.YES));

IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig(buildAnalyzer()));
writer.addDocument(document);
writer.commit();

Builder builder = new BooleanQuery.Builder();

Query query1 = new QueryParser(IndexAttribute.USER_NAME, buildAnalyzer()).parse(searchQUery+"*");
Query query2 = new QueryParser(IndexAttribute.IS_VETERAN, buildAnalyzer()).parse(""+isVeteran);
builder.add(query1, BooleanClause.Occur.MUST);
builder.add(query2, BooleanClause.Occur.MUST);

Query q = builder.build();

TopDocs docs = searcher.search(q, 10);
ScoreDoc[] hits = docs.scoreDocs;

private static Analyzer buildAnalyzer() throws IOException {
    return CustomAnalyzer.builder().withTokenizer("whitespace").addTokenFilter("lowercase")
            .addTokenFilter("standard").build();
}
因此,请就此向我提出建议。

请参阅“了解Lucene 5.5.3中的特殊字符”一节

正如上面文章中所建议的,您需要放置一个
\
,或者您可以使用
QueryParser
类的方法
公共静态字符串转义(String s)
,以实现相同的目的。

请参阅一节了解Lucene 5.5.3中的特殊字符


正如上面文章中所建议的,您需要放置一个
\
,或者您可以使用
QueryParser
类的
公共静态字符串转义(String s)
方法来实现相同的功能。

我得到了使用通配符查询、StringField和多字段QueryParser组合的解决方案。除了这些类之外,我们还要做的是转义查询字符串中的空格

我通过WildcardQuery、StringField和MultiFieldQueryParser组合得到了解决方案。除了这些类之外,我们还要做的是转义查询字符串中的空格

,这样您的字符串末尾就有了hypen?什么是索引值?也显示您的搜索代码。是的,我在末尾有一个特殊字符。我的索引值是“所有的闪光都不是金子”。对于不成功的搜索结果,您需要显示
q.toString()
的值,并指定变量的值-
searchQUery
&
isveiller
。那么您的字符串末尾有hypen吗?什么是索引值?也显示您的搜索代码。是的,我在末尾有一个特殊字符。我的索引值是“所有的闪光都不是金子”。对于不成功的搜索结果,您需要显示
q.toString()
的值,并指定变量的值-
searchQUery
&
isveiller
。感谢Sabir的回复。我试过了,但没有达到预期效果。我用WildcardQuery、StringField和MultiFieldQueryParser组合得到了解决方案。除了这些类之外,我们要做的就是转义查询字符串中的空格。感谢Sabir的回复。我试过了,但没有达到预期效果。我用WildcardQuery、StringField和MultiFieldQueryParser组合得到了解决方案。除了这些类之外,我们要做的就是转义查询字符串中的空格。