Search in sources :

Example 51 with BooleanClause

use of org.apache.lucene.search.BooleanClause in project elasticsearch by elastic.

the class MapperQueryParser method getBooleanQueryCoordDisabled.

/**
     * @deprecated review all use of this, don't rely on coord
     */
@Deprecated
protected Query getBooleanQueryCoordDisabled(List<BooleanClause> clauses) throws ParseException {
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    builder.setDisableCoord(true);
    for (BooleanClause clause : clauses) {
        builder.add(clause);
    }
    return fixNegativeQueryIfNeeded(builder.build());
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery)

Example 52 with BooleanClause

use of org.apache.lucene.search.BooleanClause in project elasticsearch by elastic.

the class MapperQueryParser method getWildcardQuery.

@Override
protected Query getWildcardQuery(String field, String termStr) throws ParseException {
    if (termStr.equals("*") && field != null) {
        if ("*".equals(field)) {
            return newMatchAllDocsQuery();
        }
        String actualField = field;
        if (actualField == null) {
            actualField = this.field;
        }
        // effectively, we check if a field exists or not
        return FIELD_QUERY_EXTENSIONS.get(ExistsFieldQueryExtension.NAME).query(context, actualField);
    }
    Collection<String> fields = extractMultiFields(field);
    if (fields != null) {
        if (fields.size() == 1) {
            return getWildcardQuerySingle(fields.iterator().next(), termStr);
        }
        if (settings.useDisMax()) {
            List<Query> queries = new ArrayList<>();
            boolean added = false;
            for (String mField : fields) {
                Query q = getWildcardQuerySingle(mField, termStr);
                if (q != null) {
                    added = true;
                    queries.add(applyBoost(mField, q));
                }
            }
            if (!added) {
                return null;
            }
            return new DisjunctionMaxQuery(queries, settings.tieBreaker());
        } else {
            List<BooleanClause> clauses = new ArrayList<>();
            for (String mField : fields) {
                Query q = getWildcardQuerySingle(mField, termStr);
                if (q != null) {
                    clauses.add(new BooleanClause(applyBoost(mField, q), BooleanClause.Occur.SHOULD));
                }
            }
            // happens for stopwords
            if (clauses.isEmpty())
                return null;
            return getBooleanQueryCoordDisabled(clauses);
        }
    } else {
        return getWildcardQuerySingle(field, termStr);
    }
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) ArrayList(java.util.ArrayList)

Example 53 with BooleanClause

use of org.apache.lucene.search.BooleanClause in project elasticsearch by elastic.

the class MapperQueryParser method getPrefixQuery.

@Override
protected Query getPrefixQuery(String field, String termStr) throws ParseException {
    Collection<String> fields = extractMultiFields(field);
    if (fields != null) {
        if (fields.size() == 1) {
            return getPrefixQuerySingle(fields.iterator().next(), termStr);
        }
        if (settings.useDisMax()) {
            List<Query> queries = new ArrayList<>();
            boolean added = false;
            for (String mField : fields) {
                Query q = getPrefixQuerySingle(mField, termStr);
                if (q != null) {
                    added = true;
                    queries.add(applyBoost(mField, q));
                }
            }
            if (!added) {
                return null;
            }
            return new DisjunctionMaxQuery(queries, settings.tieBreaker());
        } else {
            List<BooleanClause> clauses = new ArrayList<>();
            for (String mField : fields) {
                Query q = getPrefixQuerySingle(mField, termStr);
                if (q != null) {
                    clauses.add(new BooleanClause(applyBoost(mField, q), BooleanClause.Occur.SHOULD));
                }
            }
            // happens for stopwords
            if (clauses.isEmpty())
                return null;
            return getBooleanQueryCoordDisabled(clauses);
        }
    } else {
        return getPrefixQuerySingle(field, termStr);
    }
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) ArrayList(java.util.ArrayList)

Example 54 with BooleanClause

use of org.apache.lucene.search.BooleanClause in project elasticsearch by elastic.

the class MapperQueryParser method getRangeQuery.

@Override
protected Query getRangeQuery(String field, String part1, String part2, boolean startInclusive, boolean endInclusive) throws ParseException {
    if ("*".equals(part1)) {
        part1 = null;
    }
    if ("*".equals(part2)) {
        part2 = null;
    }
    Collection<String> fields = extractMultiFields(field);
    if (fields == null) {
        return getRangeQuerySingle(field, part1, part2, startInclusive, endInclusive, context);
    }
    if (fields.size() == 1) {
        return getRangeQuerySingle(fields.iterator().next(), part1, part2, startInclusive, endInclusive, context);
    }
    if (settings.useDisMax()) {
        List<Query> queries = new ArrayList<>();
        boolean added = false;
        for (String mField : fields) {
            Query q = getRangeQuerySingle(mField, part1, part2, startInclusive, endInclusive, context);
            if (q != null) {
                added = true;
                queries.add(applyBoost(mField, q));
            }
        }
        if (!added) {
            return null;
        }
        return new DisjunctionMaxQuery(queries, settings.tieBreaker());
    } else {
        List<BooleanClause> clauses = new ArrayList<>();
        for (String mField : fields) {
            Query q = getRangeQuerySingle(mField, part1, part2, startInclusive, endInclusive, context);
            if (q != null) {
                clauses.add(new BooleanClause(applyBoost(mField, q), BooleanClause.Occur.SHOULD));
            }
        }
        // happens for stopwords
        if (clauses.isEmpty())
            return null;
        return getBooleanQueryCoordDisabled(clauses);
    }
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) ArrayList(java.util.ArrayList)

Example 55 with BooleanClause

use of org.apache.lucene.search.BooleanClause in project elasticsearch by elastic.

the class BoolQueryBuilder method addBooleanClauses.

private static void addBooleanClauses(QueryShardContext context, BooleanQuery.Builder booleanQueryBuilder, List<QueryBuilder> clauses, Occur occurs) throws IOException {
    for (QueryBuilder query : clauses) {
        Query luceneQuery = null;
        switch(occurs) {
            case MUST:
            case SHOULD:
                luceneQuery = query.toQuery(context);
                break;
            case FILTER:
            case MUST_NOT:
                luceneQuery = query.toFilter(context);
                break;
        }
        booleanQueryBuilder.add(new BooleanClause(luceneQuery, occurs));
    }
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery)

Aggregations

BooleanClause (org.apache.lucene.search.BooleanClause)102 BooleanQuery (org.apache.lucene.search.BooleanQuery)92 Query (org.apache.lucene.search.Query)56 TermQuery (org.apache.lucene.search.TermQuery)46 Term (org.apache.lucene.index.Term)44 BoostQuery (org.apache.lucene.search.BoostQuery)33 ArrayList (java.util.ArrayList)23 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)21 PhraseQuery (org.apache.lucene.search.PhraseQuery)20 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)19 SynonymQuery (org.apache.lucene.search.SynonymQuery)18 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)18 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)17 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)16 SpanQuery (org.apache.lucene.search.spans.SpanQuery)15 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)14 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)14 WildcardQuery (org.apache.lucene.search.WildcardQuery)14 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)13 PrefixQuery (org.apache.lucene.search.PrefixQuery)13