Search in sources :

Example 1 with BoostQuery

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

the class MultiMatchQuery method parseAndApply.

private Query parseAndApply(Type type, String fieldName, Object value, String minimumShouldMatch, Float boostValue) throws IOException {
    Query query = parse(type, fieldName, value);
    query = Queries.maybeApplyMinimumShouldMatch(query, minimumShouldMatch);
    if (query != null && boostValue != null && boostValue != AbstractQueryBuilder.DEFAULT_BOOST) {
        query = new BoostQuery(query, boostValue);
    }
    return query;
}
Also used : Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) 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 2 with BoostQuery

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

the class QueryAnalyzerTests method testExtractQueryMetadata_boostQuery.

public void testExtractQueryMetadata_boostQuery() {
    TermQuery termQuery1 = new TermQuery(new Term("_field", "_term"));
    BoostQuery constantScoreQuery = new BoostQuery(termQuery1, 1f);
    Result result = analyze(constantScoreQuery);
    assertThat(result.verified, is(true));
    List<Term> terms = new ArrayList<>(result.terms);
    assertThat(terms.size(), equalTo(1));
    assertThat(terms.get(0).field(), equalTo(termQuery1.getTerm().field()));
    assertThat(terms.get(0).bytes(), equalTo(termQuery1.getTerm().bytes()));
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) TermQuery(org.apache.lucene.search.TermQuery) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) QueryAnalyzer.selectTermListWithTheLongestShortestTerm(org.elasticsearch.percolator.QueryAnalyzer.selectTermListWithTheLongestShortestTerm) BoostQuery(org.apache.lucene.search.BoostQuery) Result(org.elasticsearch.percolator.QueryAnalyzer.Result)

Example 3 with BoostQuery

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

the class DisMaxQueryBuilderTests method testToQueryInnerPrefixQuery.

public void testToQueryInnerPrefixQuery() throws Exception {
    assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
    String queryAsString = "{\n" + "    \"dis_max\":{\n" + "        \"queries\":[\n" + "            {\n" + "                \"prefix\":{\n" + "                    \"" + STRING_FIELD_NAME + "\":{\n" + "                        \"value\":\"sh\",\n" + "                        \"boost\":1.2\n" + "                    }\n" + "                }\n" + "            }\n" + "        ]\n" + "    }\n" + "}";
    Query query = parseQuery(queryAsString).toQuery(createShardContext());
    assertThat(query, instanceOf(DisjunctionMaxQuery.class));
    DisjunctionMaxQuery disjunctionMaxQuery = (DisjunctionMaxQuery) query;
    List<Query> disjuncts = disjunctionMaxQuery.getDisjuncts();
    assertThat(disjuncts.size(), equalTo(1));
    assertThat(disjuncts.get(0), instanceOf(BoostQuery.class));
    BoostQuery boostQuery = (BoostQuery) disjuncts.get(0);
    assertThat((double) boostQuery.getBoost(), closeTo(1.2, 0.00001));
    assertThat(boostQuery.getQuery(), instanceOf(PrefixQuery.class));
    PrefixQuery firstQ = (PrefixQuery) boostQuery.getQuery();
    // since age is automatically registered in data, we encode it as numeric
    assertThat(firstQ.getPrefix(), equalTo(new Term(STRING_FIELD_NAME, "sh")));
}
Also used : Query(org.apache.lucene.search.Query) PrefixQuery(org.apache.lucene.search.PrefixQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) BoostQuery(org.apache.lucene.search.BoostQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 4 with BoostQuery

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

the class FuzzyQueryBuilderTests method testToQueryWithStringField.

public void testToQueryWithStringField() throws IOException {
    assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
    String query = "{\n" + "    \"fuzzy\":{\n" + "        \"" + STRING_FIELD_NAME + "\":{\n" + "            \"value\":\"sh\",\n" + "            \"fuzziness\": \"AUTO\",\n" + "            \"prefix_length\":1,\n" + "            \"boost\":2.0\n" + "        }\n" + "    }\n" + "}";
    Query parsedQuery = parseQuery(query).toQuery(createShardContext());
    assertThat(parsedQuery, instanceOf(BoostQuery.class));
    BoostQuery boostQuery = (BoostQuery) parsedQuery;
    assertThat(boostQuery.getBoost(), equalTo(2.0f));
    assertThat(boostQuery.getQuery(), instanceOf(FuzzyQuery.class));
    FuzzyQuery fuzzyQuery = (FuzzyQuery) boostQuery.getQuery();
    assertThat(fuzzyQuery.getTerm(), equalTo(new Term(STRING_FIELD_NAME, "sh")));
    assertThat(fuzzyQuery.getMaxEdits(), equalTo(Fuzziness.AUTO.asDistance("sh")));
    assertThat(fuzzyQuery.getPrefixLength(), equalTo(1));
}
Also used : Query(org.apache.lucene.search.Query) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) BoostQuery(org.apache.lucene.search.BoostQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) Matchers.containsString(org.hamcrest.Matchers.containsString) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 5 with BoostQuery

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

the class MultiMatchQueryBuilderTests method testToQueryBoost.

public void testToQueryBoost() throws IOException {
    assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
    QueryShardContext shardContext = createShardContext();
    MultiMatchQueryBuilder multiMatchQueryBuilder = new MultiMatchQueryBuilder("test");
    multiMatchQueryBuilder.field(STRING_FIELD_NAME, 5f);
    Query query = multiMatchQueryBuilder.toQuery(shardContext);
    assertTermOrBoostQuery(query, STRING_FIELD_NAME, "test", 5f);
    multiMatchQueryBuilder = new MultiMatchQueryBuilder("test");
    multiMatchQueryBuilder.field(STRING_FIELD_NAME, 5f);
    multiMatchQueryBuilder.boost(2f);
    query = multiMatchQueryBuilder.toQuery(shardContext);
    assertThat(query, instanceOf(BoostQuery.class));
    BoostQuery boostQuery = (BoostQuery) query;
    assertThat(boostQuery.getBoost(), equalTo(2f));
    assertTermOrBoostQuery(boostQuery.getQuery(), STRING_FIELD_NAME, "test", 5f);
}
Also used : Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MatchQuery(org.elasticsearch.index.search.MatchQuery) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) AllTermQuery(org.elasticsearch.common.lucene.all.AllTermQuery) MultiPhrasePrefixQuery(org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery) ExtendedCommonTermsQuery(org.apache.lucene.queries.ExtendedCommonTermsQuery) LegacyNumericRangeQuery(org.apache.lucene.search.LegacyNumericRangeQuery) PointRangeQuery(org.apache.lucene.search.PointRangeQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) ElasticsearchAssertions.assertBooleanSubQuery(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertBooleanSubQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) QueryBuilders.multiMatchQuery(org.elasticsearch.index.query.QueryBuilders.multiMatchQuery) BoostQuery(org.apache.lucene.search.BoostQuery)

Aggregations

BoostQuery (org.apache.lucene.search.BoostQuery)130 Query (org.apache.lucene.search.Query)109 BooleanQuery (org.apache.lucene.search.BooleanQuery)98 TermQuery (org.apache.lucene.search.TermQuery)85 Term (org.apache.lucene.index.Term)54 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)47 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)28 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 ArrayList (java.util.ArrayList)19 SynonymQuery (org.apache.lucene.search.SynonymQuery)19 WildcardQuery (org.apache.lucene.search.WildcardQuery)19 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)18 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)17 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)16