我使用这样的查询在ES中运行:
boolQuery.must(QueryBuilders.matchQuery("field", value).minimumShouldMatch("50%"))
在Lucene中,此查询的直接模拟是什么?
最佳答案
据我了解,匹配查询基本上是对查询进行分析,并根据分析器找到的所有条件创建一个 bool(boolean) 查询。您只需将文本通过QueryParser
传递,就可以关闭排序。
但是您可以复制如下内容:
public static Query makeMatchQuery (String fieldname, String value) throws IOException {
//get a builder to start adding clauses to.
BooleanQuery.Builder qbuilder = new BooleanQuery.Builder();
//We need to analyze that value, and get a tokenstream to read terms from
Analyzer analyzer = new StandardAnalyzer();
TokenStream stream = analyzer.tokenStream(fieldname, new StringReader(value));
stream.reset();
//Iterate the token stream, and add them all to our query
int countTerms = 0;
while(stream.incrementToken()) {
countTerms++;
Query termQuery = new TermQuery(new Term(
fieldname,
stream.getAttribute(CharTermAttribute.class).toString()));
qbuilder.add(termQuery, BooleanClause.Occur.SHOULD);
}
stream.close();
analyzer.close();
//The min should match is a count of clauses, not a percentage. So for 50%, count/2
qbuilder.setMinimumNumberShouldMatch(countTerms / 2);
Query finalQuery = qbuilder.build();
return finalQuery;
}
关于elasticsearch - Lucene中的ES Match查询模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55477575/