Lucene 中的 ES Match 查询模拟
ES Match query analogue in Lucene
我在 ES 中使用这样的查询 运行:
boolQuery.must(QueryBuilders.matchQuery("field", value).minimumShouldMatch("50%"))
这个查询在 Lucene 中的直接模拟是什么?
匹配查询,据我所知,基本上是分析查询,并根据分析器找到的所有术语创建一个 BooleanQuery。您可以通过 QueryParser
.
传递文本来关闭 sorta
但您可以像这样复制它:
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;
}
我在 ES 中使用这样的查询 运行:
boolQuery.must(QueryBuilders.matchQuery("field", value).minimumShouldMatch("50%"))
这个查询在 Lucene 中的直接模拟是什么?
匹配查询,据我所知,基本上是分析查询,并根据分析器找到的所有术语创建一个 BooleanQuery。您可以通过 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;
}