Search in sources :

Example 11 with BooleanClause

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

the class MapperQueryParser method getFieldQuery.

@Override
protected Query getFieldQuery(String field, String queryText, int slop) throws ParseException {
    Collection<String> fields = extractMultiFields(field);
    if (fields != null) {
        if (settings.useDisMax()) {
            List<Query> queries = new ArrayList<>();
            boolean added = false;
            for (String mField : fields) {
                Query q = super.getFieldQuery(mField, queryText, slop);
                if (q != null) {
                    added = true;
                    q = applySlop(q, slop);
                    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 = super.getFieldQuery(mField, queryText, slop);
                if (q != null) {
                    q = applySlop(q, slop);
                    clauses.add(new BooleanClause(applyBoost(mField, q), BooleanClause.Occur.SHOULD));
                }
            }
            // happens for stopwords
            if (clauses.isEmpty())
                return null;
            return getBooleanQueryCoordDisabled(clauses);
        }
    } else {
        return super.getFieldQuery(field, queryText, slop);
    }
}
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 12 with BooleanClause

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

the class MapperQueryParser method getFieldQuery.

@Override
public Query getFieldQuery(String field, String queryText, boolean quoted) throws ParseException {
    FieldQueryExtension fieldQueryExtension = FIELD_QUERY_EXTENSIONS.get(field);
    if (fieldQueryExtension != null) {
        return fieldQueryExtension.query(context, queryText);
    }
    Collection<String> fields = extractMultiFields(field);
    if (fields != null) {
        if (fields.size() == 1) {
            return getFieldQuerySingle(fields.iterator().next(), queryText, quoted);
        }
        if (settings.useDisMax()) {
            List<Query> queries = new ArrayList<>();
            boolean added = false;
            for (String mField : fields) {
                Query q = getFieldQuerySingle(mField, queryText, quoted);
                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 = getFieldQuerySingle(mField, queryText, quoted);
                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 getFieldQuerySingle(field, queryText, quoted);
    }
}
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 13 with BooleanClause

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

the class MapperQueryParser method getRegexpQuery.

@Override
protected Query getRegexpQuery(String field, String termStr) throws ParseException {
    Collection<String> fields = extractMultiFields(field);
    if (fields != null) {
        if (fields.size() == 1) {
            return getRegexpQuerySingle(fields.iterator().next(), termStr);
        }
        if (settings.useDisMax()) {
            List<Query> queries = new ArrayList<>();
            boolean added = false;
            for (String mField : fields) {
                Query q = getRegexpQuerySingle(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 = getRegexpQuerySingle(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 getRegexpQuerySingle(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 14 with BooleanClause

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

the class BlendedTermQuery method commonTermsBlendedQuery.

public static BlendedTermQuery commonTermsBlendedQuery(Term[] terms, final float[] boosts, final boolean disableCoord, final float maxTermFrequency) {
    return new BlendedTermQuery(terms, boosts) {

        @Override
        protected Query topLevelQuery(Term[] terms, TermContext[] ctx, int[] docFreqs, int maxDoc) {
            BooleanQuery.Builder highBuilder = new BooleanQuery.Builder();
            highBuilder.setDisableCoord(disableCoord);
            BooleanQuery.Builder lowBuilder = new BooleanQuery.Builder();
            lowBuilder.setDisableCoord(disableCoord);
            for (int i = 0; i < terms.length; i++) {
                Query query = new TermQuery(terms[i], ctx[i]);
                if (boosts != null && boosts[i] != 1f) {
                    query = new BoostQuery(query, boosts[i]);
                }
                if ((maxTermFrequency >= 1f && docFreqs[i] > maxTermFrequency) || (docFreqs[i] > (int) Math.ceil(maxTermFrequency * maxDoc))) {
                    highBuilder.add(query, BooleanClause.Occur.SHOULD);
                } else {
                    lowBuilder.add(query, BooleanClause.Occur.SHOULD);
                }
            }
            BooleanQuery high = highBuilder.build();
            BooleanQuery low = lowBuilder.build();
            if (low.clauses().isEmpty()) {
                BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder();
                queryBuilder.setDisableCoord(disableCoord);
                for (BooleanClause booleanClause : high) {
                    queryBuilder.add(booleanClause.getQuery(), Occur.MUST);
                }
                return queryBuilder.build();
            } else if (high.clauses().isEmpty()) {
                return low;
            } else {
                return new BooleanQuery.Builder().setDisableCoord(true).add(high, BooleanClause.Occur.SHOULD).add(low, BooleanClause.Occur.MUST).build();
            }
        }
    };
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 15 with BooleanClause

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

the class Queries method fixNegativeQueryIfNeeded.

public static Query fixNegativeQueryIfNeeded(Query q) {
    if (isNegativeQuery(q)) {
        BooleanQuery bq = (BooleanQuery) q;
        BooleanQuery.Builder builder = new BooleanQuery.Builder();
        builder.setDisableCoord(bq.isCoordDisabled());
        for (BooleanClause clause : bq) {
            builder.add(clause);
        }
        builder.add(newMatchAllQuery(), BooleanClause.Occur.MUST);
        return builder.build();
    }
    return q;
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) 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