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;
}
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()));
}
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")));
}
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));
}
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);
}
Aggregations