Search in sources :

Example 6 with DisjunctionMaxQuery

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

the class MultiMatchQueryTests method testCrossFieldMultiMatchQuery.

public void testCrossFieldMultiMatchQuery() throws IOException {
    QueryShardContext queryShardContext = indexService.newQueryShardContext(randomInt(20), null, () -> {
        throw new UnsupportedOperationException();
    });
    queryShardContext.setAllowUnmappedFields(true);
    Query parsedQuery = multiMatchQuery("banon").field("name.first", 2).field("name.last", 3).field("foobar").type(MultiMatchQueryBuilder.Type.CROSS_FIELDS).toQuery(queryShardContext);
    try (Engine.Searcher searcher = indexService.getShard(0).acquireSearcher("test")) {
        Query rewrittenQuery = searcher.searcher().rewrite(parsedQuery);
        BooleanQuery.Builder expected = new BooleanQuery.Builder();
        expected.add(new TermQuery(new Term("foobar", "banon")), BooleanClause.Occur.SHOULD);
        Query tq1 = new BoostQuery(new TermQuery(new Term("name.first", "banon")), 2);
        Query tq2 = new BoostQuery(new TermQuery(new Term("name.last", "banon")), 3);
        expected.add(new DisjunctionMaxQuery(Arrays.<Query>asList(tq1, tq2), 0f), BooleanClause.Occur.SHOULD);
        assertEquals(expected.build(), rewrittenQuery);
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) MultiPhrasePrefixQuery(org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) QueryBuilders.multiMatchQuery(org.elasticsearch.index.query.QueryBuilders.multiMatchQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) MultiMatchQueryBuilder(org.elasticsearch.index.query.MultiMatchQueryBuilder) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery) Engine(org.elasticsearch.index.engine.Engine)

Example 7 with DisjunctionMaxQuery

use of org.apache.lucene.search.DisjunctionMaxQuery 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 8 with DisjunctionMaxQuery

use of org.apache.lucene.search.DisjunctionMaxQuery 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 9 with DisjunctionMaxQuery

use of org.apache.lucene.search.DisjunctionMaxQuery 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 10 with DisjunctionMaxQuery

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

the class BlendedTermQuery method dismaxBlendedQuery.

public static BlendedTermQuery dismaxBlendedQuery(Term[] terms, final float[] boosts, final float tieBreakerMultiplier) {
    return new BlendedTermQuery(terms, boosts) {

        @Override
        protected Query topLevelQuery(Term[] terms, TermContext[] ctx, int[] docFreqs, int maxDoc) {
            List<Query> queries = new ArrayList<>(ctx.length);
            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]);
                }
                queries.add(query);
            }
            return new DisjunctionMaxQuery(queries, tieBreakerMultiplier);
        }
    };
}
Also used : 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) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) ArrayList(java.util.ArrayList) BoostQuery(org.apache.lucene.search.BoostQuery)

Aggregations

DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)49 Query (org.apache.lucene.search.Query)38 BooleanQuery (org.apache.lucene.search.BooleanQuery)34 BoostQuery (org.apache.lucene.search.BoostQuery)30 TermQuery (org.apache.lucene.search.TermQuery)26 Term (org.apache.lucene.index.Term)25 ArrayList (java.util.ArrayList)22 PhraseQuery (org.apache.lucene.search.PhraseQuery)20 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)17 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)16 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)15 SynonymQuery (org.apache.lucene.search.SynonymQuery)15 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)15 BooleanClause (org.apache.lucene.search.BooleanClause)13 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)13 SpanQuery (org.apache.lucene.search.spans.SpanQuery)12 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)10 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)9 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)9 TopDocs (org.apache.lucene.search.TopDocs)9