Search in sources :

Example 1 with SpanBoostQuery

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

the class AbstractQueryTestCase method assertLuceneQuery.

/**
     * Checks the result of {@link QueryBuilder#toQuery(QueryShardContext)} given the original {@link QueryBuilder}
     * and {@link QueryShardContext}. Verifies that named queries and boost are properly handled and delegates to
     * {@link #doAssertLuceneQuery(AbstractQueryBuilder, Query, SearchContext)} for query specific checks.
     */
private void assertLuceneQuery(QB queryBuilder, Query query, SearchContext context) throws IOException {
    if (queryBuilder.queryName() != null) {
        Query namedQuery = context.getQueryShardContext().copyNamedQueries().get(queryBuilder.queryName());
        assertThat(namedQuery, equalTo(query));
    }
    if (query != null) {
        if (queryBuilder.boost() != AbstractQueryBuilder.DEFAULT_BOOST) {
            assertThat(query, either(instanceOf(BoostQuery.class)).or(instanceOf(SpanBoostQuery.class)));
            if (query instanceof SpanBoostQuery) {
                SpanBoostQuery spanBoostQuery = (SpanBoostQuery) query;
                assertThat(spanBoostQuery.getBoost(), equalTo(queryBuilder.boost()));
                query = spanBoostQuery.getQuery();
            } else {
                BoostQuery boostQuery = (BoostQuery) query;
                assertThat(boostQuery.getBoost(), equalTo(queryBuilder.boost()));
                query = boostQuery.getQuery();
            }
        }
    }
    doAssertLuceneQuery(queryBuilder, query, context);
}
Also used : Query(org.apache.lucene.search.Query) SpanBoostQuery(org.apache.lucene.search.spans.SpanBoostQuery) BoostQuery(org.apache.lucene.search.BoostQuery) TermQuery(org.apache.lucene.search.TermQuery) SpanBoostQuery(org.apache.lucene.search.spans.SpanBoostQuery) SpanBoostQuery(org.apache.lucene.search.spans.SpanBoostQuery) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 2 with SpanBoostQuery

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

the class SpanMultiTermQueryBuilderTests method doAssertLuceneQuery.

@Override
protected void doAssertLuceneQuery(SpanMultiTermQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException {
    if (queryBuilder.innerQuery().boost() != AbstractQueryBuilder.DEFAULT_BOOST) {
        assertThat(query, instanceOf(SpanBoostQuery.class));
        SpanBoostQuery boostQuery = (SpanBoostQuery) query;
        assertThat(boostQuery.getBoost(), equalTo(queryBuilder.innerQuery().boost()));
        query = boostQuery.getQuery();
    }
    assertThat(query, instanceOf(SpanMultiTermQueryWrapper.class));
    SpanMultiTermQueryWrapper spanMultiTermQueryWrapper = (SpanMultiTermQueryWrapper) query;
    Query multiTermQuery = queryBuilder.innerQuery().toQuery(context.getQueryShardContext());
    if (queryBuilder.innerQuery().boost() != AbstractQueryBuilder.DEFAULT_BOOST) {
        assertThat(multiTermQuery, instanceOf(BoostQuery.class));
        BoostQuery boostQuery = (BoostQuery) multiTermQuery;
        multiTermQuery = boostQuery.getQuery();
    }
    assertThat(multiTermQuery, instanceOf(MultiTermQuery.class));
    assertThat(spanMultiTermQueryWrapper.getWrappedQuery(), equalTo(new SpanMultiTermQueryWrapper<>((MultiTermQuery) multiTermQuery).getWrappedQuery()));
}
Also used : SpanMultiTermQueryWrapper(org.apache.lucene.search.spans.SpanMultiTermQueryWrapper) Query(org.apache.lucene.search.Query) SpanBoostQuery(org.apache.lucene.search.spans.SpanBoostQuery) TermQuery(org.apache.lucene.search.TermQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) BoostQuery(org.apache.lucene.search.BoostQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) SpanBoostQuery(org.apache.lucene.search.spans.SpanBoostQuery) SpanBoostQuery(org.apache.lucene.search.spans.SpanBoostQuery) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 3 with SpanBoostQuery

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

the class SpanMultiTermQueryBuilderTests method testToQueryInnerSpanMultiTerm.

public void testToQueryInnerSpanMultiTerm() throws IOException {
    Query query = new SpanOrQueryBuilder(createTestQueryBuilder()).toQuery(createShardContext());
    //verify that the result is still a span query, despite the boost that might get set (SpanBoostQuery rather than BoostQuery)
    assertThat(query, instanceOf(SpanQuery.class));
}
Also used : Query(org.apache.lucene.search.Query) SpanBoostQuery(org.apache.lucene.search.spans.SpanBoostQuery) TermQuery(org.apache.lucene.search.TermQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) BoostQuery(org.apache.lucene.search.BoostQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery)

Example 4 with SpanBoostQuery

use of org.apache.lucene.search.spans.SpanBoostQuery in project lucene-solr by apache.

the class SpanNearClauseFactory method makeSpanClause.

public SpanQuery makeSpanClause() {
    SpanQuery[] spanQueries = new SpanQuery[size()];
    Iterator<SpanQuery> sqi = weightBySpanQuery.keySet().iterator();
    int i = 0;
    while (sqi.hasNext()) {
        SpanQuery sq = sqi.next();
        float boost = weightBySpanQuery.get(sq);
        if (boost != 1f) {
            sq = new SpanBoostQuery(sq, boost);
        }
        spanQueries[i++] = sq;
    }
    if (spanQueries.length == 1)
        return spanQueries[0];
    else
        return new SpanOrQuery(spanQueries);
}
Also used : SpanBoostQuery(org.apache.lucene.search.spans.SpanBoostQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery)

Example 5 with SpanBoostQuery

use of org.apache.lucene.search.spans.SpanBoostQuery in project lucene-solr by apache.

the class BoostingTermBuilder method getSpanQuery.

@Override
public SpanQuery getSpanQuery(Element e) throws ParserException {
    String fieldName = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName");
    String value = DOMUtils.getNonBlankTextOrFail(e);
    SpanQuery btq = new PayloadScoreQuery(new SpanTermQuery(new Term(fieldName, value)), new AveragePayloadFunction());
    btq = new SpanBoostQuery(btq, DOMUtils.getAttribute(e, "boost", 1.0f));
    return btq;
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanBoostQuery(org.apache.lucene.search.spans.SpanBoostQuery) Term(org.apache.lucene.index.Term) AveragePayloadFunction(org.apache.lucene.queries.payloads.AveragePayloadFunction) SpanQuery(org.apache.lucene.search.spans.SpanQuery) PayloadScoreQuery(org.apache.lucene.queries.payloads.PayloadScoreQuery)

Aggregations

SpanBoostQuery (org.apache.lucene.search.spans.SpanBoostQuery)18 SpanQuery (org.apache.lucene.search.spans.SpanQuery)14 BoostQuery (org.apache.lucene.search.BoostQuery)6 Query (org.apache.lucene.search.Query)6 Term (org.apache.lucene.index.Term)5 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)5 ArrayList (java.util.ArrayList)4 TermQuery (org.apache.lucene.search.TermQuery)4 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)4 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)3 SpanMultiTermQueryWrapper (org.apache.lucene.search.spans.SpanMultiTermQueryWrapper)3 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)3 SpanNotQuery (org.apache.lucene.search.spans.SpanNotQuery)3 Element (org.w3c.dom.Element)3 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)2 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)2 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)2 PrefixQuery (org.apache.lucene.search.PrefixQuery)2 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)2