Support for parent in multi get request
When specifying the docs to be returned in a multi get request, a parent field could not be specified, so that some docs seemingly did not exist, even though they did. This fix behaves like the normal GetRequest and simply overwrites the routing value if it has not yet been set. Also a test for routing with mget has been added. Closes #3274
This commit is contained in:
parent
3a0ce0bde8
commit
2dcc664310
|
@ -94,6 +94,13 @@ public class MultiGetRequest extends ActionRequest<MultiGetRequest> {
|
|||
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<MultiGetRequest> {
|
|||
String type = defaultType;
|
||||
String id = null;
|
||||
String routing = null;
|
||||
String parent = null;
|
||||
List<String> 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<MultiGetRequest> {
|
|||
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<String>();
|
||||
fields.add(parser.text());
|
||||
|
@ -278,7 +288,7 @@ public class MultiGetRequest extends ActionRequest<MultiGetRequest> {
|
|||
} 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) {
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue