Skip to content

Commit

Permalink
Fixes class cast exception whentop_children, has_child and `has_p…
Browse files Browse the repository at this point in the history
…arent` queries are cached via `fquery` filter.

The error only occurs for `has_child` and `has_parent` if `score_mode` is used.
Closes elastic#3290
  • Loading branch information
martijnvg committed Jul 3, 2013
1 parent e46c6de commit cf69e5f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 18 deletions.
Expand Up @@ -93,23 +93,19 @@ public boolean equals(Object obj) {
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;
}
Expand Down
Expand Up @@ -73,16 +73,12 @@ public boolean equals(Object obj) {
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;
}
Expand Down
Expand Up @@ -102,8 +102,8 @@ public boolean equals(Object obj) {
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)) {
Expand Down
Expand Up @@ -261,14 +261,14 @@ public boolean equals(Object obj) {
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;
Expand All @@ -278,7 +278,7 @@ public boolean equals(Object obj) {
public int hashCode() {
int result = originalChildQuery.hashCode();
result = 31 * result + parentType.hashCode();
result = 31 * result + childType.hashCode();
result = 31 * result + incrementalFactor;
return result;
}

Expand Down
Expand Up @@ -169,8 +169,6 @@ public void test2744() throws ElasticSearchException, IOException {

@Test
public void simpleChildQuery() throws Exception {
client().admin().indices().prepareDelete().execute().actionGet();

client().admin().indices().prepareCreate("test")
.setSettings(
ImmutableSettings.settingsBuilder()
Expand Down Expand Up @@ -350,6 +348,48 @@ public void simpleChildQuery() throws Exception {
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
Expand Down

0 comments on commit cf69e5f

Please sign in to comment.