From 4c0b10aec73e46bfb87d05c4c12957cda2ce9ad4 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Tue, 25 Jun 2013 13:37:32 +0200 Subject: [PATCH] Made the minimum score only active when executing the main query and not during the context rewrite phase. This fixes parent/child queries when using minimum_score. Closes #3203 --- .../search/internal/ContextIndexSearcher.java | 9 ++--- .../child/SimpleChildQuerySearchTests.java | 36 +++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/elasticsearch/search/internal/ContextIndexSearcher.java b/src/main/java/org/elasticsearch/search/internal/ContextIndexSearcher.java index 4c9d9b9ecb2..f969e1fd19e 100644 --- a/src/main/java/org/elasticsearch/search/internal/ContextIndexSearcher.java +++ b/src/main/java/org/elasticsearch/search/internal/ContextIndexSearcher.java @@ -144,10 +144,11 @@ public class ContextIndexSearcher extends IndexSearcher { if (queryCollectors != null && !queryCollectors.isEmpty()) { collector = new MultiCollector(collector, queryCollectors.toArray(new Collector[queryCollectors.size()])); } - } - // apply the minimum score after multi collector so we filter facets as well - if (searchContext.minimumScore() != null) { - collector = new MinimumScoreCollector(collector, searchContext.minimumScore()); + + // apply the minimum score after multi collector so we filter facets as well + if (searchContext.minimumScore() != null) { + collector = new MinimumScoreCollector(collector, searchContext.minimumScore()); + } } // we only compute the doc id set once since within a context, we execute the same query always... diff --git a/src/test/java/org/elasticsearch/test/integration/search/child/SimpleChildQuerySearchTests.java b/src/test/java/org/elasticsearch/test/integration/search/child/SimpleChildQuerySearchTests.java index 186c94476e7..d72985056ec 100644 --- a/src/test/java/org/elasticsearch/test/integration/search/child/SimpleChildQuerySearchTests.java +++ b/src/test/java/org/elasticsearch/test/integration/search/child/SimpleChildQuerySearchTests.java @@ -1399,4 +1399,40 @@ public class SimpleChildQuerySearchTests extends AbstractSharedClusterTest { assertThat(searchResponse.getHits().getAt(1).id(), Matchers.anyOf(equalTo("c3"), equalTo("c4"))); } + @Test + // See also issue: https://github.com/elasticsearch/elasticsearch/issues/3203 + public void testHasChildQueryWithMinimumScore() throws Exception { + client().admin().indices().prepareDelete().execute().actionGet(); + + client().admin().indices().prepareCreate("test") + .setSettings( + ImmutableSettings.settingsBuilder() + .put("index.number_of_shards", 1) + .put("index.number_of_replicas", 0) + ).execute().actionGet(); + client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet(); + client().admin().indices().preparePutMapping("test").setType("child").setSource(jsonBuilder().startObject().startObject("type") + .startObject("_parent").field("type", "parent").endObject() + .endObject().endObject()).execute().actionGet(); + + // index simple data + client().prepareIndex("test", "parent", "p1").setSource("p_field", "p_value1").execute().actionGet(); + client().prepareIndex("test", "child", "c1").setSource("c_field", "x").setParent("p1").execute().actionGet(); + client().prepareIndex("test", "parent", "p2").setSource("p_field", "p_value2").execute().actionGet(); + client().prepareIndex("test", "child", "c3").setSource("c_field", "x").setParent("p2").execute().actionGet(); + client().prepareIndex("test", "child", "c4").setSource("c_field", "x").setParent("p2").execute().actionGet(); + client().prepareIndex("test", "child", "c5").setSource("c_field", "x").setParent("p2").execute().actionGet(); + client().admin().indices().prepareRefresh("test").execute().actionGet(); + + SearchResponse searchResponse = client().prepareSearch("test") + .setQuery(hasChildQuery("child", matchAllQuery()).scoreType("sum")) + .setMinScore(2) // Score needs to be above 2.0! + .execute().actionGet(); + assertThat("Failures " + Arrays.toString(searchResponse.getShardFailures()), searchResponse.getShardFailures().length, equalTo(0)); + assertThat(searchResponse.getFailedShards(), equalTo(0)); + assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); + assertThat(searchResponse.getHits().getAt(0).id(), equalTo("p2")); + assertThat(searchResponse.getHits().getAt(0).score(), equalTo(3.0f)); + } + }