Search in sources :

Example 1 with BoostingQuery

use of org.apache.lucene.queries.BoostingQuery in project elasticsearch by elastic.

the class CustomFieldQuery method flatten.

@Override
void flatten(Query sourceQuery, IndexReader reader, Collection<Query> flatQueries, float boost) throws IOException {
    if (sourceQuery instanceof BoostQuery) {
        BoostQuery bq = (BoostQuery) sourceQuery;
        sourceQuery = bq.getQuery();
        boost *= bq.getBoost();
        flatten(sourceQuery, reader, flatQueries, boost);
    } else if (sourceQuery instanceof SpanTermQuery) {
        super.flatten(new TermQuery(((SpanTermQuery) sourceQuery).getTerm()), reader, flatQueries, boost);
    } else if (sourceQuery instanceof ConstantScoreQuery) {
        flatten(((ConstantScoreQuery) sourceQuery).getQuery(), reader, flatQueries, boost);
    } else if (sourceQuery instanceof FunctionScoreQuery) {
        flatten(((FunctionScoreQuery) sourceQuery).getSubQuery(), reader, flatQueries, boost);
    } else if (sourceQuery instanceof MultiPhrasePrefixQuery) {
        flatten(sourceQuery.rewrite(reader), reader, flatQueries, boost);
    } else if (sourceQuery instanceof FiltersFunctionScoreQuery) {
        flatten(((FiltersFunctionScoreQuery) sourceQuery).getSubQuery(), reader, flatQueries, boost);
    } else if (sourceQuery instanceof MultiPhraseQuery) {
        MultiPhraseQuery q = ((MultiPhraseQuery) sourceQuery);
        convertMultiPhraseQuery(0, new int[q.getTermArrays().length], q, q.getTermArrays(), q.getPositions(), reader, flatQueries);
    } else if (sourceQuery instanceof BlendedTermQuery) {
        final BlendedTermQuery blendedTermQuery = (BlendedTermQuery) sourceQuery;
        flatten(blendedTermQuery.rewrite(reader), reader, flatQueries, boost);
    } else if (sourceQuery instanceof ESToParentBlockJoinQuery) {
        ESToParentBlockJoinQuery blockJoinQuery = (ESToParentBlockJoinQuery) sourceQuery;
        flatten(blockJoinQuery.getChildQuery(), reader, flatQueries, boost);
    } else if (sourceQuery instanceof BoostingQuery) {
        BoostingQuery boostingQuery = (BoostingQuery) sourceQuery;
        //flatten positive query with query boost
        flatten(boostingQuery.getMatch(), reader, flatQueries, boost);
        //flatten negative query with negative boost
        flatten(boostingQuery.getContext(), reader, flatQueries, boostingQuery.getBoost());
    } else if (sourceQuery instanceof SynonymQuery) {
        // SynonymQuery should be handled by the parent class directly.
        // This statement should be removed when https://issues.apache.org/jira/browse/LUCENE-7484 is merged.
        SynonymQuery synQuery = (SynonymQuery) sourceQuery;
        for (Term term : synQuery.getTerms()) {
            flatten(new TermQuery(term), reader, flatQueries, boost);
        }
    } else {
        super.flatten(sourceQuery, reader, flatQueries, boost);
    }
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) TermQuery(org.apache.lucene.search.TermQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) ESToParentBlockJoinQuery(org.elasticsearch.index.search.ESToParentBlockJoinQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) BoostingQuery(org.apache.lucene.queries.BoostingQuery) MultiPhrasePrefixQuery(org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery)

Example 2 with BoostingQuery

use of org.apache.lucene.queries.BoostingQuery in project elasticsearch by elastic.

the class BoostingQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    Query positive = positiveQuery.toQuery(context);
    Query negative = negativeQuery.toQuery(context);
    return new BoostingQuery(positive, negative, negativeBoost);
}
Also used : Query(org.apache.lucene.search.Query) BoostingQuery(org.apache.lucene.queries.BoostingQuery) BoostingQuery(org.apache.lucene.queries.BoostingQuery)

Example 3 with BoostingQuery

use of org.apache.lucene.queries.BoostingQuery in project lucene-solr by apache.

the class BoostingQueryBuilder method getQuery.

@Override
public Query getQuery(Element e) throws ParserException {
    Element mainQueryElem = DOMUtils.getChildByTagOrFail(e, "Query");
    mainQueryElem = DOMUtils.getFirstChildOrFail(mainQueryElem);
    Query mainQuery = factory.getQuery(mainQueryElem);
    Element boostQueryElem = DOMUtils.getChildByTagOrFail(e, "BoostQuery");
    float boost = DOMUtils.getAttribute(boostQueryElem, "boost", DEFAULT_BOOST);
    boostQueryElem = DOMUtils.getFirstChildOrFail(boostQueryElem);
    Query boostQuery = factory.getQuery(boostQueryElem);
    Query bq = new BoostingQuery(mainQuery, boostQuery, boost);
    boost = DOMUtils.getAttribute(e, "boost", 1.0f);
    if (boost != 1f) {
        return new BoostQuery(bq, boost);
    }
    return bq;
}
Also used : Query(org.apache.lucene.search.Query) BoostingQuery(org.apache.lucene.queries.BoostingQuery) BoostQuery(org.apache.lucene.search.BoostQuery) Element(org.w3c.dom.Element) BoostingQuery(org.apache.lucene.queries.BoostingQuery) BoostQuery(org.apache.lucene.search.BoostQuery)

Aggregations

BoostingQuery (org.apache.lucene.queries.BoostingQuery)3 BoostQuery (org.apache.lucene.search.BoostQuery)2 Query (org.apache.lucene.search.Query)2 Term (org.apache.lucene.index.Term)1 BlendedTermQuery (org.apache.lucene.queries.BlendedTermQuery)1 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)1 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)1 SynonymQuery (org.apache.lucene.search.SynonymQuery)1 TermQuery (org.apache.lucene.search.TermQuery)1 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)1 MultiPhrasePrefixQuery (org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery)1 FiltersFunctionScoreQuery (org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery)1 FunctionScoreQuery (org.elasticsearch.common.lucene.search.function.FunctionScoreQuery)1 ESToParentBlockJoinQuery (org.elasticsearch.index.search.ESToParentBlockJoinQuery)1 Element (org.w3c.dom.Element)1