diff --git a/src/main/java/org/elasticsearch/action/get/MultiGetRequest.java b/src/main/java/org/elasticsearch/action/get/MultiGetRequest.java index ddacffb82ed..c6ca9dd2ff4 100644 --- a/src/main/java/org/elasticsearch/action/get/MultiGetRequest.java +++ b/src/main/java/org/elasticsearch/action/get/MultiGetRequest.java @@ -94,6 +94,13 @@ public class MultiGetRequest extends ActionRequest { return this.routing; } + public Item parent(String parent) { + if (routing == null) { + this.routing = parent; + } + return this; + } + public Item fields(String... fields) { this.fields = fields; return this; @@ -246,6 +253,7 @@ public class MultiGetRequest extends ActionRequest { String type = defaultType; String id = null; String routing = null; + String parent = null; List fields = null; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { @@ -259,6 +267,8 @@ public class MultiGetRequest extends ActionRequest { id = parser.text(); } else if ("_routing".equals(currentFieldName) || "routing".equals(currentFieldName)) { routing = parser.text(); + } else if ("_parent".equals(currentFieldName) || "parent".equals(currentFieldName)) { + parent = parser.text(); } else if ("fields".equals(currentFieldName)) { fields = new ArrayList(); fields.add(parser.text()); @@ -278,7 +288,7 @@ public class MultiGetRequest extends ActionRequest { } else { aFields = defaultFields; } - add(new Item(index, type, id).routing(routing).fields(aFields)); + add(new Item(index, type, id).routing(routing).fields(aFields).parent(parent)); } } else if ("ids".equals(currentFieldName)) { while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { diff --git a/src/test/java/org/elasticsearch/test/integration/mget/SimpleMgetTests.java b/src/test/java/org/elasticsearch/test/integration/mget/SimpleMgetTests.java index e8ca4fd88b8..ff2ae7b6527 100644 --- a/src/test/java/org/elasticsearch/test/integration/mget/SimpleMgetTests.java +++ b/src/test/java/org/elasticsearch/test/integration/mget/SimpleMgetTests.java @@ -51,4 +51,57 @@ public class SimpleMgetTests extends AbstractSharedClusterTest { assertThat(mgetResponse.getResponses()[1].isFailed(), is(true)); assertThat(mgetResponse.getResponses()[1].getFailure().getMessage(), is("[nonExistingIndex] missing")); } + + @Test + public void testThatParentPerDocumentIsSupported() throws Exception { + createIndex("test"); + ensureYellow(); + client().admin().indices().preparePutMapping("test").setType("test").setSource(jsonBuilder() + .startObject() + .startObject("test") + .startObject("_parent") + .field("type", "foo") + .endObject() + .endObject(). + endObject() + ).execute().actionGet(); + + client().prepareIndex("test", "test", "1").setParent("4").setRefresh(true) + .setSource(jsonBuilder().startObject().field("foo", "bar").endObject()) + .execute().actionGet(); + + MultiGetResponse mgetResponse = client().prepareMultiGet() + .add(new MultiGetRequest.Item("test", "test", "1").parent("4")) + .add(new MultiGetRequest.Item("test", "test", "1")) + .execute().actionGet(); + + assertThat(mgetResponse.getResponses().length, is(2)); + assertThat(mgetResponse.getResponses()[0].isFailed(), is(false)); + assertThat(mgetResponse.getResponses()[0].getResponse().isExists(), is(true)); + + assertThat(mgetResponse.getResponses()[1].isFailed(), is(false)); + assertThat(mgetResponse.getResponses()[1].getResponse().isExists(), is(false)); + } + + @Test + public void testThatRoutingPerDocumentIsSupported() throws Exception { + createIndex("test"); + ensureYellow(); + + client().prepareIndex("test", "test", "1").setRefresh(true).setRouting("bar") + .setSource(jsonBuilder().startObject().field("foo", "bar").endObject()) + .execute().actionGet(); + + MultiGetResponse mgetResponse = client().prepareMultiGet() + .add(new MultiGetRequest.Item("test", "test", "1").routing("bar")) + .add(new MultiGetRequest.Item("test", "test", "1")) + .execute().actionGet(); + + assertThat(mgetResponse.getResponses().length, is(2)); + assertThat(mgetResponse.getResponses()[0].isFailed(), is(false)); + assertThat(mgetResponse.getResponses()[0].getResponse().isExists(), is(true)); + + assertThat(mgetResponse.getResponses()[1].isFailed(), is(false)); + assertThat(mgetResponse.getResponses()[1].getResponse().isExists(), is(false)); + } }