Add boost to SimpleQueryStringBuilder.

As per discussion in #11274 this adds support for query boosting
to the SimpleQueryStringQuery.
This commit is contained in:
Isabel Drost-Fromm 2015-06-16 11:35:31 +02:00
parent f4a143d138
commit 907cdc61f7
3 changed files with 34 additions and 2 deletions

View File

@ -19,6 +19,7 @@
package org.elasticsearch.index.query;
import org.elasticsearch.common.xcontent.ToXContent.Params;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
@ -30,7 +31,7 @@ import java.util.Map;
* SimpleQuery is a query parser that acts similar to a query_string
* query, but won't throw exceptions for any weird string syntax.
*/
public class SimpleQueryStringBuilder extends QueryBuilder {
public class SimpleQueryStringBuilder extends QueryBuilder implements BoostableQueryBuilder<SimpleQueryStringBuilder> {
private Map<String, Float> fields = new HashMap<>();
private String analyzer;
private Operator operator;
@ -38,6 +39,7 @@ public class SimpleQueryStringBuilder extends QueryBuilder {
private String queryName;
private String minimumShouldMatch;
private int flags = -1;
private float boost = 1.0f;
private Boolean lowercaseExpandedTerms;
private Boolean lenient;
private Boolean analyzeWildcard;
@ -58,6 +60,18 @@ public class SimpleQueryStringBuilder extends QueryBuilder {
this.queryText = text;
}
/** Set the boost of this query. */
@Override
public SimpleQueryStringBuilder boost(float boost) {
this.boost = boost;
return this;
}
/** Returns the boost of this query. */
public float boost() {
return this.boost;
}
/**
* Add a field to run the query against
*/
@ -195,7 +209,12 @@ public class SimpleQueryStringBuilder extends QueryBuilder {
if (minimumShouldMatch != null) {
builder.field("minimum_should_match", minimumShouldMatch);
}
if (boost != 1.0f) {
builder.field("boost", boost);
}
builder.endObject();
}
}

View File

@ -90,6 +90,7 @@ public class SimpleQueryStringParser implements QueryParser {
String currentFieldName = null;
String queryBody = null;
float boost = 1.0f;
String queryName = null;
String field = null;
String minimumShouldMatch = null;
@ -147,6 +148,8 @@ public class SimpleQueryStringParser implements QueryParser {
} else if (token.isValue()) {
if ("query".equals(currentFieldName)) {
queryBody = parser.text();
} else if ("boost".equals(currentFieldName)) {
boost = parser.floatValue();
} else if ("analyzer".equals(currentFieldName)) {
analyzer = parseContext.analysisService().analyzer(parser.text());
if (analyzer == null) {
@ -198,7 +201,7 @@ public class SimpleQueryStringParser implements QueryParser {
if (queryBody == null) {
throw new QueryParsingException(parseContext, "[" + NAME + "] query text missing");
}
// Support specifying only a field instead of a map
if (field == null) {
field = currentFieldName;
@ -231,6 +234,8 @@ public class SimpleQueryStringParser implements QueryParser {
if (minimumShouldMatch != null && query instanceof BooleanQuery) {
Queries.applyMinimumShouldMatch((BooleanQuery) query, minimumShouldMatch);
}
query.setBoost(boost);
return query;
}
}

View File

@ -36,6 +36,7 @@ import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.queryStringQuery;
import static org.elasticsearch.index.query.QueryBuilders.simpleQueryStringQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*;
import static org.hamcrest.Matchers.equalTo;
@ -59,6 +60,13 @@ public class SimpleQueryStringTests extends ElasticsearchIntegrationTest {
assertHitCount(searchResponse, 3l);
assertSearchHits(searchResponse, "1", "2", "3");
searchResponse = client().prepareSearch().setQuery(
boolQuery()
.should(simpleQueryStringQuery("foo").boost(-10.0f))
.should(termQuery("body", "eggplant"))).get();
assertHitCount(searchResponse, 3l);
assertFirstHit(searchResponse, hasId("4"));
searchResponse = client().prepareSearch().setQuery(
simpleQueryStringQuery("foo bar").defaultOperator(SimpleQueryStringBuilder.Operator.AND)).get();
assertHitCount(searchResponse, 1l);