elasticsearch - Lucene中的ES Match查询模拟

标签 elasticsearch lucene full-text-search

我使用这样的查询在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/

相关文章:

elasticsearch - 怎么提到kibana可以使用的用于elasticsearch的json文件?

java - 为什么 Lucene 使用 maxDoc 而不是 numDocs 来计算术语 idf?

带有@符号的MySQL全文搜索产生错误 "syntax error, unexpected ' @', expecting $end"

java - 使用用于 ElasticSearch 的 Java API 进行分页

elasticsearch - ElasticSearch查询时间-如何减少响应时间

elasticsearch - 我可以知道 "more like this"查询的选定术语吗

mysql - 数据未在ajax solr中建立索引

java - Lucene 和访问控制(评论的可见性)

mysql - 整数值的 SQL 全文搜索解决方法

sql-server - 为什么我的单字符全文搜索不起作用?