Add boost to SimpleQueryStringBuilder.
As per discussion in #11274 this adds support for query boosting to the SimpleQueryStringQuery.
This commit is contained in:
parent
f4a143d138
commit
907cdc61f7
|
@ -19,6 +19,7 @@
|
||||||
|
|
||||||
package org.elasticsearch.index.query;
|
package org.elasticsearch.index.query;
|
||||||
|
|
||||||
|
import org.elasticsearch.common.xcontent.ToXContent.Params;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -30,7 +31,7 @@ import java.util.Map;
|
||||||
* SimpleQuery is a query parser that acts similar to a query_string
|
* SimpleQuery is a query parser that acts similar to a query_string
|
||||||
* query, but won't throw exceptions for any weird string syntax.
|
* 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 Map<String, Float> fields = new HashMap<>();
|
||||||
private String analyzer;
|
private String analyzer;
|
||||||
private Operator operator;
|
private Operator operator;
|
||||||
|
@ -38,6 +39,7 @@ public class SimpleQueryStringBuilder extends QueryBuilder {
|
||||||
private String queryName;
|
private String queryName;
|
||||||
private String minimumShouldMatch;
|
private String minimumShouldMatch;
|
||||||
private int flags = -1;
|
private int flags = -1;
|
||||||
|
private float boost = 1.0f;
|
||||||
private Boolean lowercaseExpandedTerms;
|
private Boolean lowercaseExpandedTerms;
|
||||||
private Boolean lenient;
|
private Boolean lenient;
|
||||||
private Boolean analyzeWildcard;
|
private Boolean analyzeWildcard;
|
||||||
|
@ -58,6 +60,18 @@ public class SimpleQueryStringBuilder extends QueryBuilder {
|
||||||
this.queryText = text;
|
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
|
* Add a field to run the query against
|
||||||
*/
|
*/
|
||||||
|
@ -196,6 +210,11 @@ public class SimpleQueryStringBuilder extends QueryBuilder {
|
||||||
builder.field("minimum_should_match", minimumShouldMatch);
|
builder.field("minimum_should_match", minimumShouldMatch);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (boost != 1.0f) {
|
||||||
|
builder.field("boost", boost);
|
||||||
|
}
|
||||||
|
|
||||||
builder.endObject();
|
builder.endObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -90,6 +90,7 @@ public class SimpleQueryStringParser implements QueryParser {
|
||||||
|
|
||||||
String currentFieldName = null;
|
String currentFieldName = null;
|
||||||
String queryBody = null;
|
String queryBody = null;
|
||||||
|
float boost = 1.0f;
|
||||||
String queryName = null;
|
String queryName = null;
|
||||||
String field = null;
|
String field = null;
|
||||||
String minimumShouldMatch = null;
|
String minimumShouldMatch = null;
|
||||||
|
@ -147,6 +148,8 @@ public class SimpleQueryStringParser implements QueryParser {
|
||||||
} else if (token.isValue()) {
|
} else if (token.isValue()) {
|
||||||
if ("query".equals(currentFieldName)) {
|
if ("query".equals(currentFieldName)) {
|
||||||
queryBody = parser.text();
|
queryBody = parser.text();
|
||||||
|
} else if ("boost".equals(currentFieldName)) {
|
||||||
|
boost = parser.floatValue();
|
||||||
} else if ("analyzer".equals(currentFieldName)) {
|
} else if ("analyzer".equals(currentFieldName)) {
|
||||||
analyzer = parseContext.analysisService().analyzer(parser.text());
|
analyzer = parseContext.analysisService().analyzer(parser.text());
|
||||||
if (analyzer == null) {
|
if (analyzer == null) {
|
||||||
|
@ -231,6 +234,8 @@ public class SimpleQueryStringParser implements QueryParser {
|
||||||
if (minimumShouldMatch != null && query instanceof BooleanQuery) {
|
if (minimumShouldMatch != null && query instanceof BooleanQuery) {
|
||||||
Queries.applyMinimumShouldMatch((BooleanQuery) query, minimumShouldMatch);
|
Queries.applyMinimumShouldMatch((BooleanQuery) query, minimumShouldMatch);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
query.setBoost(boost);
|
||||||
return query;
|
return query;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.boolQuery;
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.queryStringQuery;
|
import static org.elasticsearch.index.query.QueryBuilders.queryStringQuery;
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.simpleQueryStringQuery;
|
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.elasticsearch.test.hamcrest.ElasticsearchAssertions.*;
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
|
||||||
|
@ -59,6 +60,13 @@ public class SimpleQueryStringTests extends ElasticsearchIntegrationTest {
|
||||||
assertHitCount(searchResponse, 3l);
|
assertHitCount(searchResponse, 3l);
|
||||||
assertSearchHits(searchResponse, "1", "2", "3");
|
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(
|
searchResponse = client().prepareSearch().setQuery(
|
||||||
simpleQueryStringQuery("foo bar").defaultOperator(SimpleQueryStringBuilder.Operator.AND)).get();
|
simpleQueryStringQuery("foo bar").defaultOperator(SimpleQueryStringBuilder.Operator.AND)).get();
|
||||||
assertHitCount(searchResponse, 1l);
|
assertHitCount(searchResponse, 1l);
|
||||||
|
|
Loading…
Reference in New Issue