我正在努力将Lucene集成到我们的应用程序中。Lucene目前正在工作,例如,当我搜索"Upload“时,文档中有一些名为"Upload”的文本,然后它就工作了,但是当我搜索"Uplo“时,它就不起作用了。有什么想法吗?
代码:
Directory directory = FSDirectory.open(path);
IndexReader indexReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
QueryParser queryParser = new QueryParser("contents", new SimpleAnalyzer());
Query query = queryParser.parse(text);
TopDocs topDocs = indexSearcher.search(query, 50);
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
org.apache.lucene.document.Document document = indexSearcher.doc(scoreDoc.doc);
objectIds.add(Integer.valueOf(document.get("id")));
System.out.println("");
System.out.println("id " + document.get("id"));
System.out.println("content " + document.get("contents"));
}
return objectIds;谢谢。
发布于 2017-05-03 11:51:53
‘'Upload’可能是Lucene索引中的一个令牌,其中令牌将是最小的不可分离的实体。如果您想匹配'Uplo‘这样的部分单词,那么最好选择Lucene NGram索引。请注意,如果您使用NGram索引,您将对倒排索引有更高的空间要求。
发布于 2017-05-03 10:12:42
可以使用通配符搜索。
"?“用于单字符通配符搜索的符号和用于多字符通配符搜索的"*“符号(0或多个字符)。
例子- "Uplo*“
发布于 2017-05-03 10:30:19
变化
Query query = queryParser.parse(text);至
Query query = queryParser.parse("*"+text+"*");Lucene支持单个和多个字符通配符在单个术语(而不是短语查询)中的搜索。
若要执行单个字符通配符搜索,请使用"?“符号。
若要执行多字符通配符搜索,请使用"*“符号。
单字符通配符搜索查找与替换的单个字符匹配的术语。例如,要搜索“文本”或“测试”,可以使用以下搜索:
te?t多个字符通配符搜索寻找0或多个字符。例如,要搜索测试、测试或测试人员,可以使用搜索:
test*还可以在术语中间使用通配符搜索。
te*t注意:您不能使用*或?作为搜索的第一个字符的符号。
https://stackoverflow.com/questions/43756904
复制相似问题