From 907cdc61f7e023de50c7c0ad934b9e58e31b0182 Mon Sep 17 00:00:00 2001 From: Isabel Drost-Fromm Date: Tue, 16 Jun 2015 11:35:31 +0200 Subject: [PATCH] Add boost to SimpleQueryStringBuilder. As per discussion in #11274 this adds support for query boosting to the SimpleQueryStringQuery. --- .../index/query/SimpleQueryStringBuilder.java | 21 ++++++++++++++++++- .../index/query/SimpleQueryStringParser.java | 7 ++++++- .../search/query/SimpleQueryStringTests.java | 8 +++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringBuilder.java b/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringBuilder.java index 9e8e51a5d90..633f38dc5b2 100644 --- a/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringBuilder.java @@ -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 { private Map 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(); } + } diff --git a/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringParser.java b/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringParser.java index cd47565669b..097556902fb 100644 --- a/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringParser.java +++ b/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringParser.java @@ -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; } } diff --git a/core/src/test/java/org/elasticsearch/search/query/SimpleQueryStringTests.java b/core/src/test/java/org/elasticsearch/search/query/SimpleQueryStringTests.java index fc14abf27da..7009d8fbfa7 100644 --- a/core/src/test/java/org/elasticsearch/search/query/SimpleQueryStringTests.java +++ b/core/src/test/java/org/elasticsearch/search/query/SimpleQueryStringTests.java @@ -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);