From 4c8f3de34b0f85abd8cf263cfbbf796dc6b4f132 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Wed, 3 Jul 2013 21:37:16 +0200 Subject: [PATCH] Fixes class cast exception when`top_children`, `has_child` and `has_parent` queries are cached via `fquery` filter. The error only occurs for `has_child` and `has_parent` if `score_mode` is used. Closes #3290 --- .../index/search/child/ChildrenQuery.java | 8 +--- .../index/search/child/HasChildFilter.java | 4 -- .../index/search/child/ParentQuery.java | 4 +- .../index/search/child/TopChildrenQuery.java | 8 ++-- .../child/SimpleChildQuerySearchTests.java | 44 ++++++++++++++++++- 5 files changed, 50 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/elasticsearch/index/search/child/ChildrenQuery.java b/src/main/java/org/elasticsearch/index/search/child/ChildrenQuery.java index 7e53b9e9ab8..0f7082f152e 100644 --- a/src/main/java/org/elasticsearch/index/search/child/ChildrenQuery.java +++ b/src/main/java/org/elasticsearch/index/search/child/ChildrenQuery.java @@ -93,23 +93,19 @@ public class ChildrenQuery extends Query implements SearchContext.Rewrite { return false; } - HasChildFilter that = (HasChildFilter) obj; - if (!originalChildQuery.equals(that.childQuery)) { + ChildrenQuery that = (ChildrenQuery) obj; + if (!originalChildQuery.equals(that.originalChildQuery)) { return false; } if (!childType.equals(that.childType)) { return false; } - if (!parentType.equals(that.parentType)) { - return false; - } return true; } @Override public int hashCode() { int result = originalChildQuery.hashCode(); - result = 31 * result + parentType.hashCode(); result = 31 * result + childType.hashCode(); return result; } diff --git a/src/main/java/org/elasticsearch/index/search/child/HasChildFilter.java b/src/main/java/org/elasticsearch/index/search/child/HasChildFilter.java index c819b441649..d2882fc4e3f 100644 --- a/src/main/java/org/elasticsearch/index/search/child/HasChildFilter.java +++ b/src/main/java/org/elasticsearch/index/search/child/HasChildFilter.java @@ -73,16 +73,12 @@ public class HasChildFilter extends Filter implements SearchContext.Rewrite { if (!childType.equals(that.childType)) { return false; } - if (!parentType.equals(that.parentType)) { - return false; - } return true; } @Override public int hashCode() { int result = childQuery.hashCode(); - result = 31 * result + parentType.hashCode(); result = 31 * result + childType.hashCode(); return result; } diff --git a/src/main/java/org/elasticsearch/index/search/child/ParentQuery.java b/src/main/java/org/elasticsearch/index/search/child/ParentQuery.java index 5c605eda85a..d22255f4a71 100644 --- a/src/main/java/org/elasticsearch/index/search/child/ParentQuery.java +++ b/src/main/java/org/elasticsearch/index/search/child/ParentQuery.java @@ -102,8 +102,8 @@ public class ParentQuery extends Query implements SearchContext.Rewrite { return false; } - HasParentFilter that = (HasParentFilter) obj; - if (!originalParentQuery.equals(that.parentQuery)) { + ParentQuery that = (ParentQuery) obj; + if (!originalParentQuery.equals(that.originalParentQuery)) { return false; } if (!parentType.equals(that.parentType)) { diff --git a/src/main/java/org/elasticsearch/index/search/child/TopChildrenQuery.java b/src/main/java/org/elasticsearch/index/search/child/TopChildrenQuery.java index 8880f7cd52a..24c8425a12c 100644 --- a/src/main/java/org/elasticsearch/index/search/child/TopChildrenQuery.java +++ b/src/main/java/org/elasticsearch/index/search/child/TopChildrenQuery.java @@ -261,14 +261,14 @@ public class TopChildrenQuery extends Query implements SearchContext.Rewrite { return false; } - HasChildFilter that = (HasChildFilter) obj; - if (!originalChildQuery.equals(that.childQuery)) { + TopChildrenQuery that = (TopChildrenQuery) obj; + if (!originalChildQuery.equals(that.originalChildQuery)) { return false; } if (!childType.equals(that.childType)) { return false; } - if (!parentType.equals(that.parentType)) { + if (incrementalFactor != that.incrementalFactor) { return false; } return true; @@ -278,7 +278,7 @@ public class TopChildrenQuery extends Query implements SearchContext.Rewrite { public int hashCode() { int result = originalChildQuery.hashCode(); result = 31 * result + parentType.hashCode(); - result = 31 * result + childType.hashCode(); + result = 31 * result + incrementalFactor; return result; } 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 d72985056ec..7d808ea0450 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 @@ -169,8 +169,6 @@ public class SimpleChildQuerySearchTests extends AbstractSharedClusterTest { @Test public void simpleChildQuery() throws Exception { - client().admin().indices().prepareDelete().execute().actionGet(); - client().admin().indices().prepareCreate("test") .setSettings( ImmutableSettings.settingsBuilder() @@ -350,6 +348,48 @@ public class SimpleChildQuerySearchTests extends AbstractSharedClusterTest { assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); assertThat(searchResponse.getHits().getAt(0).id(), equalTo("c1")); assertThat(searchResponse.getHits().getAt(1).id(), equalTo("c2")); + + + } + + @Test + // See: https://github.com/elasticsearch/elasticsearch/issues/3290 + public void testCachingBug_withFqueryFilter() throws Exception { + 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 + for (int i = 0; i < 10; i++) { + client().prepareIndex("test", "parent", Integer.toString(i)).setSource("p_field", i).execute().actionGet(); + } + for (int i = 0; i < 10; i++) { + client().prepareIndex("test", "child", Integer.toString(i)).setSource("c_field", i).setParent("" + 0).execute().actionGet(); + } + for (int i = 0; i < 10; i++) { + client().prepareIndex("test", "child", Integer.toString(i + 10)).setSource("c_field", i + 10).setParent(Integer.toString(i)).execute().actionGet(); + } + client().admin().indices().prepareFlush().execute().actionGet(); + client().admin().indices().prepareRefresh().execute().actionGet(); + + for (int i = 0; i < 10; i++) { + SearchResponse searchResponse = client().prepareSearch("test").setQuery(constantScoreQuery(queryFilter(topChildrenQuery("child", matchAllQuery())).cache(true))) + .execute().actionGet(); + assertThat("Failures " + Arrays.toString(searchResponse.getShardFailures()), searchResponse.getShardFailures().length, equalTo(0)); + searchResponse = client().prepareSearch("test").setQuery(constantScoreQuery(queryFilter(hasChildQuery("child", matchAllQuery()).scoreType("max")).cache(true))) + .execute().actionGet(); + assertThat("Failures " + Arrays.toString(searchResponse.getShardFailures()), searchResponse.getShardFailures().length, equalTo(0)); + searchResponse = client().prepareSearch("test").setQuery(constantScoreQuery(queryFilter(hasParentQuery("parent", matchAllQuery()).scoreType("score")).cache(true))) + .execute().actionGet(); + assertThat("Failures " + Arrays.toString(searchResponse.getShardFailures()), searchResponse.getShardFailures().length, equalTo(0)); + } } @Test