Merge pull request #11696 from MaineC/feature/simple-query-string-addboost

Add support for query boost to SimpleQueryStringBuilder.

Relates to #10217
This commit is contained in:
Isabel Drost-Fromm 2015-06-18 10:10:57 +02:00
commit 2fa56899d6
3 changed files with 39 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,11 @@ public class SimpleQueryStringParser implements QueryParser {
if (minimumShouldMatch != null && query instanceof BooleanQuery) {
Queries.applyMinimumShouldMatch((BooleanQuery) query, minimumShouldMatch);
}
if (query != null) {
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,15 @@ public class SimpleQueryStringTests extends ElasticsearchIntegrationTest {
assertHitCount(searchResponse, 3l);
assertSearchHits(searchResponse, "1", "2", "3");
// Tests boost value setting. In this case doc 1 should always be ranked above the other
// two matches.
searchResponse = client().prepareSearch().setQuery(
boolQuery()
.should(simpleQueryStringQuery("\"foo bar\"").boost(10.0f))
.should(termQuery("body", "eggplant"))).get();
assertHitCount(searchResponse, 2l);
assertFirstHit(searchResponse, hasId("3"));
searchResponse = client().prepareSearch().setQuery(
simpleQueryStringQuery("foo bar").defaultOperator(SimpleQueryStringBuilder.Operator.AND)).get();
assertHitCount(searchResponse, 1l);