簡體   English   中英

Lucene 中的 ES 匹配查詢模擬

[英]ES Match query analogue in Lucene

我使用這樣的查詢在 ES 中運行:

boolQuery.must(QueryBuilders.matchQuery("field", value).minimumShouldMatch("50%"))

Lucene 中此查詢的直接模擬是什么?

據我了解,Match Query 基本上會分析查詢,並根據分析器找到的所有術語創建一個 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM