use of org.elasticsearch.action.update.UpdateResponse in project elasticsearch by elastic.
the class UpdateIT method testContextVariables.
public void testContextVariables() throws Exception {
assertAcked(prepareCreate("test").addAlias(new Alias("alias")).addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").endObject().endObject()).addMapping("subtype1", XContentFactory.jsonBuilder().startObject().startObject("subtype1").startObject("_parent").field("type", "type1").endObject().endObject().endObject()));
ensureGreen();
// Index some documents
client().prepareIndex().setIndex("test").setType("type1").setId("parentId1").setSource("field1", 0, "content", "bar").execute().actionGet();
client().prepareIndex().setIndex("test").setType("subtype1").setId("id1").setParent("parentId1").setRouting("routing1").setSource("field1", 1, "content", "foo").execute().actionGet();
// Update the first object and note context variables values
UpdateResponse updateResponse = client().prepareUpdate("test", "subtype1", "id1").setRouting("routing1").setScript(new Script(ScriptType.INLINE, "extract_ctx", "", Collections.emptyMap())).execute().actionGet();
assertEquals(2, updateResponse.getVersion());
GetResponse getResponse = client().prepareGet("test", "subtype1", "id1").setRouting("routing1").execute().actionGet();
Map<String, Object> updateContext = (Map<String, Object>) getResponse.getSourceAsMap().get("update_context");
assertEquals("test", updateContext.get("_index"));
assertEquals("subtype1", updateContext.get("_type"));
assertEquals("id1", updateContext.get("_id"));
assertEquals(1, updateContext.get("_version"));
assertEquals("parentId1", updateContext.get("_parent"));
assertEquals("routing1", updateContext.get("_routing"));
// Idem with the second object
updateResponse = client().prepareUpdate("test", "type1", "parentId1").setScript(new Script(ScriptType.INLINE, "extract_ctx", "", Collections.emptyMap())).execute().actionGet();
assertEquals(2, updateResponse.getVersion());
getResponse = client().prepareGet("test", "type1", "parentId1").execute().actionGet();
updateContext = (Map<String, Object>) getResponse.getSourceAsMap().get("update_context");
assertEquals("test", updateContext.get("_index"));
assertEquals("type1", updateContext.get("_type"));
assertEquals("parentId1", updateContext.get("_id"));
assertEquals(1, updateContext.get("_version"));
assertNull(updateContext.get("_parent"));
assertNull(updateContext.get("_routing"));
assertNull(updateContext.get("_ttl"));
}
use of org.elasticsearch.action.update.UpdateResponse in project elasticsearch by elastic.
the class UpdateIT method testUpsertDoc.
public void testUpsertDoc() throws Exception {
createTestIndex();
ensureGreen();
UpdateResponse updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1").setDoc(XContentFactory.jsonBuilder().startObject().field("bar", "baz").endObject()).setDocAsUpsert(true).setFields("_source").execute().actionGet();
assertThat(updateResponse.getIndex(), equalTo("test"));
assertThat(updateResponse.getGetResult(), notNullValue());
assertThat(updateResponse.getGetResult().getIndex(), equalTo("test"));
assertThat(updateResponse.getGetResult().sourceAsMap().get("bar").toString(), equalTo("baz"));
}
use of org.elasticsearch.action.update.UpdateResponse in project elasticsearch by elastic.
the class UpdateIT method testIndexAutoCreation.
public void testIndexAutoCreation() throws Exception {
UpdateResponse updateResponse = client().prepareUpdate("test", "type1", "1").setUpsert(XContentFactory.jsonBuilder().startObject().field("bar", "baz").endObject()).setScript(new Script(ScriptType.INLINE, "put_values", "", Collections.singletonMap("extra", "foo"))).setFetchSource(true).execute().actionGet();
assertThat(updateResponse.getIndex(), equalTo("test"));
assertThat(updateResponse.getGetResult(), notNullValue());
assertThat(updateResponse.getGetResult().getIndex(), equalTo("test"));
assertThat(updateResponse.getGetResult().sourceAsMap().get("bar").toString(), equalTo("baz"));
assertThat(updateResponse.getGetResult().sourceAsMap().get("extra"), nullValue());
}
use of org.elasticsearch.action.update.UpdateResponse in project elasticsearch by elastic.
the class UpdateNoopIT method update.
private UpdateResponse update(Boolean detectNoop, long expectedVersion, XContentBuilder xContentBuilder) {
UpdateRequestBuilder updateRequest = client().prepareUpdate("test", "type1", "1").setDoc(xContentBuilder).setDocAsUpsert(true).setFields("_source");
if (detectNoop != null) {
updateRequest.setDetectNoop(detectNoop);
}
UpdateResponse updateResponse = updateRequest.get();
assertThat(updateResponse.getGetResult(), notNullValue());
assertEquals(expectedVersion, updateResponse.getVersion());
return updateResponse;
}
use of org.elasticsearch.action.update.UpdateResponse in project elasticsearch by elastic.
the class IndicesRequestIT method testUpdateUpsert.
public void testUpdateUpsert() {
//update action goes to the primary, index op gets executed locally, then replicated
String[] updateShardActions = new String[] { UpdateAction.NAME + "[s]", BulkAction.NAME + "[s][p]", BulkAction.NAME + "[s][r]" };
interceptTransportActions(updateShardActions);
String indexOrAlias = randomIndexOrAlias();
UpdateRequest updateRequest = new UpdateRequest(indexOrAlias, "type", "id").upsert(Requests.INDEX_CONTENT_TYPE, "field", "value").doc(Requests.INDEX_CONTENT_TYPE, "field1", "value1");
UpdateResponse updateResponse = internalCluster().coordOnlyNodeClient().update(updateRequest).actionGet();
assertEquals(DocWriteResponse.Result.CREATED, updateResponse.getResult());
clearInterceptedActions();
assertSameIndices(updateRequest, updateShardActions);
}
Aggregations