Lucene 布尔查询

发布于 2024-07-22 17:45:38 字数 60 浏览 7 评论 0原文

如何在 Lucene 搜索中将 booleanQuery 与 StandardAnalyzer 结合使用?

How to use booleanQuery with StandardAnalyzer in Lucene Search?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

や莫失莫忘 2024-07-29 17:45:38

我想您指的是使用 QueryParser 对象解析布尔查询,对吗? Lucene 查询语法文档 应该包含您需要的一切。

I presume you are referring to parsing boolean queries using the QueryParser object, correct? The Lucene query syntax documentation should have everything you need.

梦开始←不甜 2024-07-29 17:45:38

根据文档,布尔查询应该由 BooleanQuery.QueryBuilder 创建一次,然后被认为是不可变的。 请参阅 BooleanQuery.Builder.add(org.apache.lucene.search.BooleanClause)

代码示例可以通过 http://programtalk.com/java-api-usage-examples/org.apache.lucene.util.QueryBuilder/
给出的一个例子是:

public Query parse(Type type, String fieldName, Object value) throws IOException {
    final String field;
    MappedFieldType fieldType = context.fieldMapper(fieldName);
    if (fieldType != null) {
        field = fieldType.name();
    } else {
        field = fieldName;
    }
    /*
     * If the user forced an analyzer we really don't care if they are
     * searching a type that wants term queries to be used with query string
     * because the QueryBuilder will take care of it. If they haven't forced
     * an analyzer then types like NumberFieldType that want terms with
     * query string will blow up because their analyzer isn't capable of
     * passing through QueryBuilder.
     */
    boolean noForcedAnalyzer = this.analyzer == null;
    if (fieldType != null && fieldType.tokenized() == false && noForcedAnalyzer) {
        return blendTermQuery(new Term(fieldName, value.toString()), fieldType);
    }
    Analyzer analyzer = getAnalyzer(fieldType);
    assert analyzer != null;
    MatchQueryBuilder builder = new MatchQueryBuilder(analyzer, fieldType);
    builder.setEnablePositionIncrements(this.enablePositionIncrements);
    Query query = null;
    switch(type) {
        case BOOLEAN:
            if (commonTermsCutoff == null) {
                query = builder.createBooleanQuery(field, value.toString(), occur);
            } else {
                query = builder.createCommonTermsQuery(field, value.toString(), occur, occur, commonTermsCutoff, fieldType);
            }
            break;
        case PHRASE:
            query = builder.createPhraseQuery(field, value.toString(), phraseSlop);
            break;
        case PHRASE_PREFIX:
            query = builder.createPhrasePrefixQuery(field, value.toString(), phraseSlop, maxExpansions);
            break;
        default:
            throw new IllegalStateException("No type found for [" + type + "]");
    }
    if (query == null) {
        return zeroTermsQuery();
    } else {
        return query;
    }
}

According to the document, boolean queries should be created once by BooleanQuery.QueryBuilder and then considered immutable. See BooleanQuery.Builder.add(org.apache.lucene.search.BooleanClause)

The code example can be found by http://programtalk.com/java-api-usage-examples/org.apache.lucene.util.QueryBuilder/
and one example given is:

public Query parse(Type type, String fieldName, Object value) throws IOException {
    final String field;
    MappedFieldType fieldType = context.fieldMapper(fieldName);
    if (fieldType != null) {
        field = fieldType.name();
    } else {
        field = fieldName;
    }
    /*
     * If the user forced an analyzer we really don't care if they are
     * searching a type that wants term queries to be used with query string
     * because the QueryBuilder will take care of it. If they haven't forced
     * an analyzer then types like NumberFieldType that want terms with
     * query string will blow up because their analyzer isn't capable of
     * passing through QueryBuilder.
     */
    boolean noForcedAnalyzer = this.analyzer == null;
    if (fieldType != null && fieldType.tokenized() == false && noForcedAnalyzer) {
        return blendTermQuery(new Term(fieldName, value.toString()), fieldType);
    }
    Analyzer analyzer = getAnalyzer(fieldType);
    assert analyzer != null;
    MatchQueryBuilder builder = new MatchQueryBuilder(analyzer, fieldType);
    builder.setEnablePositionIncrements(this.enablePositionIncrements);
    Query query = null;
    switch(type) {
        case BOOLEAN:
            if (commonTermsCutoff == null) {
                query = builder.createBooleanQuery(field, value.toString(), occur);
            } else {
                query = builder.createCommonTermsQuery(field, value.toString(), occur, occur, commonTermsCutoff, fieldType);
            }
            break;
        case PHRASE:
            query = builder.createPhraseQuery(field, value.toString(), phraseSlop);
            break;
        case PHRASE_PREFIX:
            query = builder.createPhrasePrefixQuery(field, value.toString(), phraseSlop, maxExpansions);
            break;
        default:
            throw new IllegalStateException("No type found for [" + type + "]");
    }
    if (query == null) {
        return zeroTermsQuery();
    } else {
        return query;
    }
}
爱你不解释 2024-07-29 17:45:38

布尔查询。 BooleanQuery 是布尔子句的容器,它们是可选的、必需的或禁止的子查询。 通常,您可以使用如下所示的 API 方法向 BooleanQuery 添加一个子句:

public void add(Query query, boolean required, booleanbanned)

BooleanQuery. BooleanQuery is a container of Boolean clauses, that are optional, required or prohibited subqueries. You can normally add a clause to BooleanQuery making use of an API method that looks like:

public void add(Query query, boolean required, boolean prohibited)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文