Upsert should return fields Fixes #2362
This commit is contained in:
parent
ec64c8c907
commit
a2628b5eb2
|
@ -179,7 +179,7 @@ public class TransportUpdateAction extends TransportInstanceSingleOperationActio
|
|||
listener.onFailure(new DocumentMissingException(new ShardId(request.index(), request.shardId()), request.type(), request.id()));
|
||||
return;
|
||||
}
|
||||
IndexRequest indexRequest = request.upsertRequest();
|
||||
final IndexRequest indexRequest = request.upsertRequest();
|
||||
indexRequest.index(request.index()).type(request.type()).id(request.id())
|
||||
// it has to be a "create!"
|
||||
.create(true)
|
||||
|
@ -195,8 +195,12 @@ public class TransportUpdateAction extends TransportInstanceSingleOperationActio
|
|||
public void onResponse(IndexResponse response) {
|
||||
UpdateResponse update = new UpdateResponse(response.index(), response.type(), response.id(), response.version());
|
||||
update.matches(response.matches());
|
||||
// TODO: we can parse the index _source and extractGetResult if applicable
|
||||
update.getResult(null);
|
||||
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);
|
||||
}
|
||||
listener.onResponse(update);
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
public void testUpdate() throws Exception {
|
||||
createIndex();
|
||||
|
|
Loading…
Reference in New Issue