Upsert should return fields Fixes #2362

This commit is contained in:
Igor Motov 2012-10-29 17:46:03 -04:00 committed by Shay Banon
parent ec64c8c907
commit a2628b5eb2
2 changed files with 35 additions and 3 deletions

View File

@ -179,7 +179,7 @@ public class TransportUpdateAction extends TransportInstanceSingleOperationActio
listener.onFailure(new DocumentMissingException(new ShardId(request.index(), request.shardId()), request.type(), request.id())); listener.onFailure(new DocumentMissingException(new ShardId(request.index(), request.shardId()), request.type(), request.id()));
return; return;
} }
IndexRequest indexRequest = request.upsertRequest(); final IndexRequest indexRequest = request.upsertRequest();
indexRequest.index(request.index()).type(request.type()).id(request.id()) indexRequest.index(request.index()).type(request.type()).id(request.id())
// it has to be a "create!" // it has to be a "create!"
.create(true) .create(true)
@ -195,8 +195,12 @@ public class TransportUpdateAction extends TransportInstanceSingleOperationActio
public void onResponse(IndexResponse response) { public void onResponse(IndexResponse response) {
UpdateResponse update = new UpdateResponse(response.index(), response.type(), response.id(), response.version()); UpdateResponse update = new UpdateResponse(response.index(), response.type(), response.id(), response.version());
update.matches(response.matches()); update.matches(response.matches());
// TODO: we can parse the index _source and extractGetResult if applicable if (request.fields() != null && request.fields().length > 0) {
Tuple<XContentType, Map<String, Object>> sourceAndContent = XContentHelper.convertToMap(updateSourceBytes, true);
update.getResult(extractGetResult(request, response.version(), sourceAndContent.v2(), sourceAndContent.v1(), updateSourceBytes));
} else {
update.getResult(null); update.getResult(null);
}
listener.onResponse(update); listener.onResponse(update);
} }

View File

@ -190,6 +190,34 @@ public class UpdateTests extends AbstractNodesTests {
} }
} }
@Test
public void testUpsertFields() throws Exception {
createIndex();
ClusterHealthResponse clusterHealth = client.admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet();
assertThat(clusterHealth.timedOut(), equalTo(false));
assertThat(clusterHealth.status(), equalTo(ClusterHealthStatus.GREEN));
UpdateResponse updateResponse = client.prepareUpdate("test", "type1", "1")
.setUpsert(XContentFactory.jsonBuilder().startObject().field("bar", "baz").endObject())
.setScript("ctx._source.extra = \"foo\"")
.setFields("_source")
.execute().actionGet();
assertThat(updateResponse.getGetResult(), notNullValue());
assertThat(updateResponse.getGetResult().sourceAsMap().get("bar").toString(), equalTo("baz"));
assertThat(updateResponse.getGetResult().sourceAsMap().get("extra"), nullValue());
updateResponse = client.prepareUpdate("test", "type1", "1")
.setUpsert(XContentFactory.jsonBuilder().startObject().field("bar", "baz").endObject())
.setScript("ctx._source.extra = \"foo\"")
.setFields("_source")
.execute().actionGet();
assertThat(updateResponse.getGetResult(), notNullValue());
assertThat(updateResponse.getGetResult().sourceAsMap().get("bar").toString(), equalTo("baz"));
assertThat(updateResponse.getGetResult().sourceAsMap().get("extra").toString(), equalTo("foo"));
}
@Test @Test
public void testUpdate() throws Exception { public void testUpdate() throws Exception {
createIndex(); createIndex();