From 5e9106ae5e7ed7ad0648fab640471aa48a4bd135 Mon Sep 17 00:00:00 2001 From: Isabel Drost-Fromm Date: Wed, 17 Jun 2015 11:18:32 +0200 Subject: [PATCH] Fixing review comments. --- .../index/query/SimpleQueryStringBuilder.java | 4 ++-- .../index/query/SimpleQueryStringParser.java | 7 +++++-- .../search/query/SimpleQueryStringTests.java | 8 +++++--- 3 files changed, 12 insertions(+), 7 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 633f38dc5b2..700ad419e9f 100644 --- a/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringBuilder.java @@ -39,7 +39,7 @@ public class SimpleQueryStringBuilder extends QueryBuilder implements BoostableQ private String queryName; private String minimumShouldMatch; private int flags = -1; - private float boost = 1.0f; + private float boost = -1.0f; private Boolean lowercaseExpandedTerms; private Boolean lenient; private Boolean analyzeWildcard; @@ -210,7 +210,7 @@ public class SimpleQueryStringBuilder extends QueryBuilder implements BoostableQ builder.field("minimum_should_match", minimumShouldMatch); } - if (boost != 1.0f) { + if (boost != -1.0f) { builder.field("boost", boost); } 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 097556902fb..c696a7522f4 100644 --- a/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringParser.java +++ b/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringParser.java @@ -234,8 +234,11 @@ public class SimpleQueryStringParser implements QueryParser { if (minimumShouldMatch != null && query instanceof BooleanQuery) { Queries.applyMinimumShouldMatch((BooleanQuery) query, minimumShouldMatch); } - - query.setBoost(boost); + + if (query != null) { + 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 7009d8fbfa7..eaa016411ad 100644 --- a/core/src/test/java/org/elasticsearch/search/query/SimpleQueryStringTests.java +++ b/core/src/test/java/org/elasticsearch/search/query/SimpleQueryStringTests.java @@ -60,12 +60,14 @@ 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").boost(-10.0f)) + .should(simpleQueryStringQuery("\"foo bar\"").boost(10.0f)) .should(termQuery("body", "eggplant"))).get(); - assertHitCount(searchResponse, 3l); - assertFirstHit(searchResponse, hasId("4")); + assertHitCount(searchResponse, 2l); + assertFirstHit(searchResponse, hasId("3")); searchResponse = client().prepareSearch().setQuery( simpleQueryStringQuery("foo bar").defaultOperator(SimpleQueryStringBuilder.Operator.AND)).get();