diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/ParentFieldMapper.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/ParentFieldMapper.java index 3600b775518..91ea808de11 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/ParentFieldMapper.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/ParentFieldMapper.java @@ -116,6 +116,18 @@ public class ParentFieldMapper extends AbstractFieldMapper implements org.e return field.stringValue(); } + @Override public Object valueForSearch(Fieldable field) { + String fieldValue = field.stringValue(); + if (fieldValue == null) { + return null; + } + int index = fieldValue.indexOf(Uid.DELIMITER); + if (index == -1) { + return fieldValue; + } + return fieldValue.substring(index + 1); + } + @Override public String indexedValue(String value) { if (value.indexOf(Uid.DELIMITER) == -1) { return Uid.createUid(type, value); diff --git a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/child/SimpleChildQuerySearchTests.java b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/child/SimpleChildQuerySearchTests.java index 6261d395af7..3453eda93da 100644 --- a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/child/SimpleChildQuerySearchTests.java +++ b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/child/SimpleChildQuerySearchTests.java @@ -80,9 +80,46 @@ public class SimpleChildQuerySearchTests extends AbstractNodesTests { client.admin().indices().prepareRefresh().execute().actionGet(); + // TEST FETCHING _parent from child + SearchResponse searchResponse = client.prepareSearch("test") + .setQuery(termQuery("child._id", "c1")) + .addFields("_parent") + .execute().actionGet(); + if (searchResponse.failedShards() > 0) { + logger.warn("Failed shards:"); + for (ShardSearchFailure shardSearchFailure : searchResponse.shardFailures()) { + logger.warn("-> {}", shardSearchFailure); + } + } + assertThat(searchResponse.failedShards(), equalTo(0)); + assertThat(searchResponse.hits().totalHits(), equalTo(1l)); + assertThat(searchResponse.hits().getAt(0).id(), equalTo("c1")); + assertThat(searchResponse.hits().getAt(0).field("_parent").value().toString(), equalTo("p1")); + + // TEST matching on parent + searchResponse = client.prepareSearch("test") + .setQuery(termQuery("child._parent", "p1")) + .addFields("_parent") + .execute().actionGet(); + if (searchResponse.failedShards() > 0) { + logger.warn("Failed shards:"); + for (ShardSearchFailure shardSearchFailure : searchResponse.shardFailures()) { + logger.warn("-> {}", shardSearchFailure); + } + } + assertThat(searchResponse.failedShards(), equalTo(0)); + assertThat(searchResponse.hits().totalHits(), equalTo(2l)); + assertThat(searchResponse.hits().getAt(0).id(), anyOf(equalTo("c1"), equalTo("c2"))); + assertThat(searchResponse.hits().getAt(0).field("_parent").value().toString(), equalTo("p1")); + assertThat(searchResponse.hits().getAt(1).id(), anyOf(equalTo("c1"), equalTo("c2"))); + assertThat(searchResponse.hits().getAt(1).field("_parent").value().toString(), equalTo("p1")); + + // TOP CHILDREN QUERY - SearchResponse searchResponse = client.prepareSearch("test").setQuery(topChildrenQuery("child", termQuery("c_field", "yellow"))).execute().actionGet(); + searchResponse = client.prepareSearch("test") + .setQuery(topChildrenQuery("child", termQuery("c_field", "yellow"))) + .execute().actionGet(); if (searchResponse.failedShards() > 0) { logger.warn("Failed shards:"); for (ShardSearchFailure shardSearchFailure : searchResponse.shardFailures()) {