简体   繁体   English

Lucene搜索匹配词组中的任何单词

[英]Lucene search match any word at phrase

i wanna search a string with lots of words, and retrieves documents that matches with any of them. 我想搜索包含许多单词的字符串,并检索与其中任何一个匹配的文档。 My indexing method is the folowing: 我的索引方法如下:

 Document document = new Document();
 document.add(new TextField("termos", text, Field.Store.YES));
 document.add(new TextField("docNumber",fileNumber,Field.Store.YES));

 config = new IndexWriterConfig(analyzer);
 Analyzer analyzer = CustomAnalyzer.builder()
            .withTokenizer("standard")
            .addTokenFilter("lowercase")
            .addTokenFilter("stop")
            .addTokenFilter("porterstem")
            .addTokenFilter("capitalization")
            .build();
 config = IndexWriterConfig(analyzer);
 writer = new IndexWriter(indexDirectory, config);
 writer.addDocument(document);
 writer.commit();

And here is my search method. 这是我的搜索方法。 I dont wanna look for specific phrase, but any of word in that. 我不想寻找特定的词组,但是其中的任何单词。 The analyzer for search is the same that for index. 用于搜索的分析器与用于索引的分析器相同。

Query query = new QueryBuilder(analyzer).createPhraseQuery("termos","THE_PHRASE");
String indexDir = rootProjectFolder + "/indexDir/";
IndexReader reader = DirectoryReader.open(indexDir);
IndexSearcher searcher = new IndexSearcher(reader);
TopScoreDocCollector collector = TopScoreDocCollector.create(1000,1000);
searcher.search(query,collector);

Im new on Lucene. 我是Lucene的新手。 Someone can help me? 有人可以帮我吗?

Using createPhraseQuery("termos", "list of words") will precisely try to match the phrase "list of words" with a phrase slop of 0. 使用createPhraseQuery("termos", "list of words")将精确地尝试将短语“单词列表”与短语坡度0匹配。

If you want to match any term in a list of words, you can use createBooleanQuery : 如果要匹配单词列表中的任何术语,可以使用createBooleanQuery

new QueryBuilder(analyzer).createBooleanQuery("termos", terms, BooleanClause.Occur.SHOULD);

As an alternative, you can also use createMinShouldMatchQuery() so that you can require a fraction of the number of query terms to match, eg. 作为替代方案,您也可以使用createMinShouldMatchQuery() ,例如,您只需要一部分查询条件即可进行匹配。 to match at least 10 percent of the terms : 至少匹配10%的条款:

new QueryBuilder(analyzer).createMinShouldMatchQuery("termos", terms, 0.1f));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM