Search in sources :

Example 6 with BoostQuery

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

the class QueryStringQueryBuilderTests method testToQueryPhraseQueryBoostAndSlop.

public void testToQueryPhraseQueryBoostAndSlop() throws IOException {
    assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
    QueryStringQueryBuilder queryStringQueryBuilder = new QueryStringQueryBuilder("\"test phrase\"~2").field(STRING_FIELD_NAME, 5f);
    Query query = queryStringQueryBuilder.toQuery(createShardContext());
    assertThat(query, instanceOf(DisjunctionMaxQuery.class));
    DisjunctionMaxQuery disjunctionMaxQuery = (DisjunctionMaxQuery) query;
    assertThat(disjunctionMaxQuery.getDisjuncts().size(), equalTo(1));
    assertThat(disjunctionMaxQuery.getDisjuncts().get(0), instanceOf(BoostQuery.class));
    BoostQuery boostQuery = (BoostQuery) disjunctionMaxQuery.getDisjuncts().get(0);
    assertThat(boostQuery.getBoost(), equalTo(5f));
    assertThat(boostQuery.getQuery(), instanceOf(PhraseQuery.class));
    PhraseQuery phraseQuery = (PhraseQuery) boostQuery.getQuery();
    assertThat(phraseQuery.getSlop(), Matchers.equalTo(2));
    assertThat(phraseQuery.getTerms().length, equalTo(2));
}
Also used : 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) PhraseQuery(org.apache.lucene.search.PhraseQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 7 with BoostQuery

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

the class QueryStringQueryBuilderTests method testToQueryBooleanQueryMultipleBoosts.

public void testToQueryBooleanQueryMultipleBoosts() throws Exception {
    assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
    int numBoosts = randomIntBetween(2, 10);
    float[] boosts = new float[numBoosts + 1];
    String queryStringPrefix = "";
    String queryStringSuffix = "";
    for (int i = 0; i < boosts.length - 1; i++) {
        float boost = 2.0f / randomIntBetween(3, 20);
        boosts[i] = boost;
        queryStringPrefix += "(";
        queryStringSuffix += ")^" + boost;
    }
    String queryString = queryStringPrefix + "foo bar" + queryStringSuffix;
    float mainBoost = 2.0f / randomIntBetween(3, 20);
    boosts[boosts.length - 1] = mainBoost;
    QueryStringQueryBuilder queryStringQueryBuilder = new QueryStringQueryBuilder(queryString).field(STRING_FIELD_NAME).minimumShouldMatch("2").boost(mainBoost);
    Query query = queryStringQueryBuilder.toQuery(createShardContext());
    for (int i = boosts.length - 1; i >= 0; i--) {
        assertThat(query, instanceOf(BoostQuery.class));
        BoostQuery boostQuery = (BoostQuery) query;
        assertThat(boostQuery.getBoost(), equalTo(boosts[i]));
        query = boostQuery.getQuery();
    }
    assertThat(query, instanceOf(BooleanQuery.class));
    BooleanQuery booleanQuery = (BooleanQuery) query;
    assertThat(booleanQuery.getMinimumNumberShouldMatch(), equalTo(2));
    assertThat(booleanQuery.clauses().get(0).getOccur(), equalTo(BooleanClause.Occur.SHOULD));
    assertThat(booleanQuery.clauses().get(0).getQuery(), equalTo(new TermQuery(new Term(STRING_FIELD_NAME, "foo"))));
    assertThat(booleanQuery.clauses().get(1).getOccur(), equalTo(BooleanClause.Occur.SHOULD));
    assertThat(booleanQuery.clauses().get(1).getQuery(), equalTo(new TermQuery(new Term(STRING_FIELD_NAME, "bar"))));
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) 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) Matchers.containsString(org.hamcrest.Matchers.containsString) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 8 with BoostQuery

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

the class SimpleQueryStringBuilderTests method testToQueryBoost.

public void testToQueryBoost() throws IOException {
    assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
    QueryShardContext shardContext = createShardContext();
    SimpleQueryStringBuilder simpleQueryStringBuilder = new SimpleQueryStringBuilder("test");
    simpleQueryStringBuilder.field(STRING_FIELD_NAME, 5);
    Query query = simpleQueryStringBuilder.toQuery(shardContext);
    assertThat(query, instanceOf(BoostQuery.class));
    BoostQuery boostQuery = (BoostQuery) query;
    assertThat(boostQuery.getBoost(), equalTo(5f));
    assertThat(boostQuery.getQuery(), instanceOf(TermQuery.class));
    simpleQueryStringBuilder = new SimpleQueryStringBuilder("test");
    simpleQueryStringBuilder.field(STRING_FIELD_NAME, 5);
    simpleQueryStringBuilder.boost(2);
    query = simpleQueryStringBuilder.toQuery(shardContext);
    boostQuery = (BoostQuery) query;
    assertThat(boostQuery.getBoost(), equalTo(2f));
    assertThat(boostQuery.getQuery(), instanceOf(BoostQuery.class));
    boostQuery = (BoostQuery) boostQuery.getQuery();
    assertThat(boostQuery.getBoost(), equalTo(5f));
    assertThat(boostQuery.getQuery(), instanceOf(TermQuery.class));
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) 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 9 with BoostQuery

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

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

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