Search in sources :

Example 11 with BoostQuery

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

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

use of org.apache.lucene.search.BoostQuery 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)

Example 14 with BoostQuery

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

the class BlendedTermQuery method booleanBlendedQuery.

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

        @Override
        protected Query topLevelQuery(Term[] terms, TermContext[] ctx, int[] docFreqs, int maxDoc) {
            BooleanQuery.Builder booleanQueryBuilder = new BooleanQuery.Builder();
            booleanQueryBuilder.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]);
                }
                booleanQueryBuilder.add(query, BooleanClause.Occur.SHOULD);
            }
            return booleanQueryBuilder.build();
        }
    };
}
Also used : 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 BoostQuery

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

the class XMoreLikeThis method addToQuery.

/**
     * Add to an existing boolean query the More Like This query from this PriorityQueue
     */
private void addToQuery(PriorityQueue<ScoreTerm> q, BooleanQuery.Builder query) {
    ScoreTerm scoreTerm;
    float bestScore = -1;
    while ((scoreTerm = q.pop()) != null) {
        Query tq = new TermQuery(new Term(scoreTerm.topField, scoreTerm.word));
        if (boost) {
            if (bestScore == -1) {
                bestScore = (scoreTerm.score);
            }
            float myScore = (scoreTerm.score);
            tq = new BoostQuery(tq, boostFactor * myScore / bestScore);
        }
        try {
            query.add(tq, BooleanClause.Occur.SHOULD);
        } catch (BooleanQuery.TooManyClauses ignore) {
            break;
        }
    }
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery)

Aggregations

BoostQuery (org.apache.lucene.search.BoostQuery)109 Query (org.apache.lucene.search.Query)91 BooleanQuery (org.apache.lucene.search.BooleanQuery)80 TermQuery (org.apache.lucene.search.TermQuery)71 Term (org.apache.lucene.index.Term)48 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)43 PhraseQuery (org.apache.lucene.search.PhraseQuery)32 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)27 PrefixQuery (org.apache.lucene.search.PrefixQuery)27 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)25 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)22 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)21 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)20 SynonymQuery (org.apache.lucene.search.SynonymQuery)19 WildcardQuery (org.apache.lucene.search.WildcardQuery)19 BooleanClause (org.apache.lucene.search.BooleanClause)18 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)17 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)16 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)16 ArrayList (java.util.ArrayList)14