Search in sources :

Example 71 with BoostQuery

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

the class TermBasedFieldType method termQuery.

@Override
public Query termQuery(Object value, QueryShardContext context) {
    failIfNotIndexed();
    TermQuery query = new TermQuery(new Term(name(), indexedValueForSearch(value)));
    if (boost() == 1f || (context != null && context.indexVersionCreated().before(Version.V_5_0_0_alpha1))) {
        return query;
    }
    return new BoostQuery(query, boost());
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 72 with BoostQuery

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

the class QueryStringQueryBuilderTests method testToQueryBoosts.

public void testToQueryBoosts() throws Exception {
    assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
    QueryShardContext shardContext = createShardContext();
    QueryStringQueryBuilder queryStringQuery = queryStringQuery(STRING_FIELD_NAME + ":boosted^2");
    Query query = queryStringQuery.toQuery(shardContext);
    assertThat(query, instanceOf(BoostQuery.class));
    BoostQuery boostQuery = (BoostQuery) query;
    assertThat(boostQuery.getBoost(), equalTo(2.0f));
    assertThat(boostQuery.getQuery(), instanceOf(TermQuery.class));
    assertThat(((TermQuery) boostQuery.getQuery()).getTerm(), equalTo(new Term(STRING_FIELD_NAME, "boosted")));
    queryStringQuery.boost(2.0f);
    query = queryStringQuery.toQuery(shardContext);
    assertThat(query, instanceOf(BoostQuery.class));
    boostQuery = (BoostQuery) query;
    assertThat(boostQuery.getBoost(), equalTo(2.0f));
    assertThat(boostQuery.getQuery(), instanceOf(BoostQuery.class));
    boostQuery = (BoostQuery) boostQuery.getQuery();
    assertThat(boostQuery.getBoost(), equalTo(2.0f));
    queryStringQuery = queryStringQuery("((" + STRING_FIELD_NAME + ":boosted^2) AND (" + STRING_FIELD_NAME + ":foo^1.5))^3");
    query = queryStringQuery.toQuery(shardContext);
    assertThat(query, instanceOf(BoostQuery.class));
    boostQuery = (BoostQuery) query;
    assertThat(boostQuery.getBoost(), equalTo(3.0f));
    BoostQuery boostQuery1 = assertBooleanSubQuery(boostQuery.getQuery(), BoostQuery.class, 0);
    assertThat(boostQuery1.getBoost(), equalTo(2.0f));
    assertThat(boostQuery1.getQuery(), instanceOf(TermQuery.class));
    assertThat(((TermQuery) boostQuery1.getQuery()).getTerm(), equalTo(new Term(STRING_FIELD_NAME, "boosted")));
    BoostQuery boostQuery2 = assertBooleanSubQuery(boostQuery.getQuery(), BoostQuery.class, 1);
    assertThat(boostQuery2.getBoost(), equalTo(1.5f));
    assertThat(boostQuery2.getQuery(), instanceOf(TermQuery.class));
    assertThat(((TermQuery) boostQuery2.getQuery()).getTerm(), equalTo(new Term(STRING_FIELD_NAME, "foo")));
    queryStringQuery.boost(2.0f);
    query = queryStringQuery.toQuery(shardContext);
    assertThat(query, instanceOf(BoostQuery.class));
    boostQuery = (BoostQuery) query;
    assertThat(boostQuery.getBoost(), equalTo(2.0f));
}
Also used : MultiTermQuery(org.apache.lucene.search.MultiTermQuery) AllTermQuery(org.elasticsearch.common.lucene.all.AllTermQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) RegexpQuery(org.apache.lucene.search.RegexpQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) AllTermQuery(org.elasticsearch.common.lucene.all.AllTermQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) ElasticsearchAssertions.assertBooleanSubQuery(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertBooleanSubQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) QueryBuilders.queryStringQuery(org.elasticsearch.index.query.QueryBuilders.queryStringQuery) BoostQuery(org.apache.lucene.search.BoostQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 73 with BoostQuery

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

the class PercolatorHighlightSubFetchPhaseTests method testLocatePercolatorQuery.

public void testLocatePercolatorQuery() {
    PercolateQuery percolateQuery = new PercolateQuery("", ctx -> null, new BytesArray("{}"), new MatchAllDocsQuery(), Mockito.mock(IndexSearcher.class), new MatchAllDocsQuery());
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(new MatchAllDocsQuery()), nullValue());
    BooleanQuery.Builder bq = new BooleanQuery.Builder();
    bq.add(new MatchAllDocsQuery(), BooleanClause.Occur.FILTER);
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(bq.build()), nullValue());
    bq.add(percolateQuery, BooleanClause.Occur.FILTER);
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(bq.build()), sameInstance(percolateQuery));
    ConstantScoreQuery constantScoreQuery = new ConstantScoreQuery(new MatchAllDocsQuery());
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(constantScoreQuery), nullValue());
    constantScoreQuery = new ConstantScoreQuery(percolateQuery);
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(constantScoreQuery), sameInstance(percolateQuery));
    BoostQuery boostQuery = new BoostQuery(new MatchAllDocsQuery(), 1f);
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(boostQuery), nullValue());
    boostQuery = new BoostQuery(percolateQuery, 1f);
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(boostQuery), sameInstance(percolateQuery));
    FunctionScoreQuery functionScoreQuery = new FunctionScoreQuery(new MatchAllDocsQuery(), new RandomScoreFunction());
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(functionScoreQuery), nullValue());
    functionScoreQuery = new FunctionScoreQuery(percolateQuery, new RandomScoreFunction());
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(functionScoreQuery), sameInstance(percolateQuery));
    DisjunctionMaxQuery disjunctionMaxQuery = new DisjunctionMaxQuery(Arrays.asList(new MatchAllDocsQuery()), 1f);
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(disjunctionMaxQuery), nullValue());
    disjunctionMaxQuery = new DisjunctionMaxQuery(Arrays.asList(percolateQuery, new MatchAllDocsQuery()), 1f);
    assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(disjunctionMaxQuery), sameInstance(percolateQuery));
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) BooleanQuery(org.apache.lucene.search.BooleanQuery) BytesArray(org.elasticsearch.common.bytes.BytesArray) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) BoostQuery(org.apache.lucene.search.BoostQuery) RandomScoreFunction(org.elasticsearch.common.lucene.search.function.RandomScoreFunction)

Example 74 with BoostQuery

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

the class AbstractQueryTestCase method assertTermOrBoostQuery.

protected static void assertTermOrBoostQuery(Query query, String field, String value, float fieldBoost) {
    if (fieldBoost != AbstractQueryBuilder.DEFAULT_BOOST) {
        assertThat(query, instanceOf(BoostQuery.class));
        BoostQuery boostQuery = (BoostQuery) query;
        assertThat(boostQuery.getBoost(), equalTo(fieldBoost));
        query = boostQuery.getQuery();
    }
    assertTermQuery(query, field, value);
}
Also used : SpanBoostQuery(org.apache.lucene.search.spans.SpanBoostQuery) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 75 with BoostQuery

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

the class MultiMatchQueryTests method testBlendNoTermQuery.

public void testBlendNoTermQuery() {
    FakeFieldType ft1 = new FakeFieldType();
    ft1.setName("foo");
    FakeFieldType ft2 = new FakeFieldType() {

        @Override
        public Query termQuery(Object value, QueryShardContext context) {
            return new MatchAllDocsQuery();
        }
    };
    ft2.setName("bar");
    Term[] terms = new Term[] { new Term("foo", "baz") };
    float[] boosts = new float[] { 2 };
    Query expectedClause1 = BlendedTermQuery.booleanBlendedQuery(terms, boosts, false);
    Query expectedClause2 = new BoostQuery(new MatchAllDocsQuery(), 3);
    Query expected = new BooleanQuery.Builder().setDisableCoord(true).add(expectedClause1, Occur.SHOULD).add(expectedClause2, Occur.SHOULD).build();
    Query actual = MultiMatchQuery.blendTerm(indexService.newQueryShardContext(randomInt(20), null, () -> {
        throw new UnsupportedOperationException();
    }), new BytesRef("baz"), null, 1f, new FieldAndFieldType(ft1, 2), new FieldAndFieldType(ft2, 3));
    assertEquals(expected, actual);
}
Also used : 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) MultiMatchQueryBuilder(org.elasticsearch.index.query.MultiMatchQueryBuilder) Term(org.apache.lucene.index.Term) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) BoostQuery(org.apache.lucene.search.BoostQuery) FakeFieldType(org.elasticsearch.index.mapper.MockFieldMapper.FakeFieldType) FieldAndFieldType(org.elasticsearch.index.search.MultiMatchQuery.FieldAndFieldType) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

BoostQuery (org.apache.lucene.search.BoostQuery)128 Query (org.apache.lucene.search.Query)107 BooleanQuery (org.apache.lucene.search.BooleanQuery)96 TermQuery (org.apache.lucene.search.TermQuery)84 Term (org.apache.lucene.index.Term)54 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)45 PhraseQuery (org.apache.lucene.search.PhraseQuery)35 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)32 PrefixQuery (org.apache.lucene.search.PrefixQuery)29 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)27 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)24 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)23 BooleanClause (org.apache.lucene.search.BooleanClause)20 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)20 SynonymQuery (org.apache.lucene.search.SynonymQuery)19 WildcardQuery (org.apache.lucene.search.WildcardQuery)19 ArrayList (java.util.ArrayList)18 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)18 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)17 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)16