Beispiel #1
0
 @CheckForNull
 public QProfileRule findByActiveRuleId(int activeRuleId) {
   GetResponse activeRuleResponse =
       index
           .client()
           .prepareGet(INDEX_RULES, ESActiveRule.TYPE_ACTIVE_RULE, Integer.toString(activeRuleId))
           .setFields(FIELD_SOURCE, FIELD_PARENT)
           .execute()
           .actionGet();
   Map<String, Object> activeRuleSource = activeRuleResponse.getSourceAsMap();
   if (activeRuleSource != null) {
     Map<String, Object> ruleSource =
         index
             .client()
             .prepareGet(
                 INDEX_RULES,
                 TYPE_RULE,
                 (String) activeRuleResponse.getField(FIELD_PARENT).getValue())
             .execute()
             .actionGet()
             .getSourceAsMap();
     if (ruleSource != null) {
       return new QProfileRule(ruleSource, activeRuleSource);
     }
   }
   return null;
 }
  @Test
  public void testNullShape() throws Exception {
    String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type1")
            .startObject("properties")
            .startObject("location")
            .field("type", "geo_shape")
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .string();
    prepareCreate("test").addMapping("type1", mapping).execute().actionGet();
    ensureGreen();

    client()
        .prepareIndex("test", "type1", "aNullshape")
        .setSource("{\"location\": null}")
        .execute()
        .actionGet();
    GetResponse result = client().prepareGet("test", "type1", "aNullshape").execute().actionGet();
    assertThat(result.getField("location"), nullValue());
  }
 protected ESShape getShape(Iterator<MultiGetItemResponse> iterator, int size) {
   ESShapeBuilder shapeBuilder = new ESShapeBuilder(size);
   for (int i = 0; i < size; i++) {
     GetResponse response = iterator.next().getResponse();
     if (!response.isExists()) continue;
     @SuppressWarnings("unchecked")
     Map<String, Object> shape = (Map<String, Object>) response.getField("shape").getValue();
     @SuppressWarnings("unchecked")
     List<Double> coordinates = (List<Double>) shape.get("coordinates");
     shapeBuilder.addLocation(coordinates.get(1), coordinates.get(0));
   }
   return shapeBuilder.build();
 }
 private String getLastUpdate() {
   String res;
   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
   try {
     GetResponse response =
         client.prepareGet(indexName, "stats", "1").setFields("last_update").execute().actionGet();
     if (!response.getFields().isEmpty()) {
       res = (String) response.getField("last_update").getValue();
     } else {
       res = sdf.format(new Date(0));
     }
   } catch (ElasticsearchIllegalStateException ex) {
     logger.error("Could not get last_update, use Date(0)", ex.getLocalizedMessage());
     res = sdf.format(new Date(0));
   } catch (NullPointerException ex) {
     logger.error("Could not get last_update, use Date(0)", ex.getLocalizedMessage());
     res = sdf.format(new Date(0));
   }
   return res;
 }
 @Override
 public void run() {
   while (run) {
     int childIdLimit = PARENT_COUNT * NUM_CHILDREN_PER_PARENT;
     for (int childId = 1; run && childId < childIdLimit; ) {
       try {
         for (int j = 0; j < 8; j++) {
           GetResponse getResponse =
               client
                   .prepareGet(indexName, "child", String.valueOf(++childId))
                   .setFields("_source", "_parent")
                   .setRouting("1") // Doesn't matter what value, since there is only one shard
                   .get();
           client
               .prepareIndex(indexName, "child", Integer.toString(childId) + "_" + j)
               .setParent(getResponse.getField("_parent").getValue().toString())
               .setSource(getResponse.getSource())
               .get();
         }
         client.admin().indices().prepareRefresh(indexName).execute().actionGet();
         Thread.sleep(1000);
         if (childId % 500 == 0) {
           NodesStatsResponse statsResponse =
               client
                   .admin()
                   .cluster()
                   .prepareNodesStats()
                   .clear()
                   .setIndices(true)
                   .execute()
                   .actionGet();
           System.out.println(
               "Deleted docs: " + statsResponse.getAt(0).getIndices().getDocs().getDeleted());
         }
       } catch (Throwable e) {
         e.printStackTrace();
       }
     }
   }
 }
  @Test
  public void testSimpleTTL() throws Exception {
    assertAcked(
        prepareCreate("test")
            .addMapping(
                "type1",
                XContentFactory.jsonBuilder()
                    .startObject()
                    .startObject("type1")
                    .startObject("_timestamp")
                    .field("enabled", true)
                    .field("store", "yes")
                    .endObject()
                    .startObject("_ttl")
                    .field("enabled", true)
                    .endObject()
                    .endObject()
                    .endObject())
            .addMapping(
                "type2",
                XContentFactory.jsonBuilder()
                    .startObject()
                    .startObject("type2")
                    .startObject("_timestamp")
                    .field("enabled", true)
                    .field("store", "yes")
                    .endObject()
                    .startObject("_ttl")
                    .field("enabled", true)
                    .field("default", "1d")
                    .endObject()
                    .endObject()
                    .endObject()));
    ensureYellow("test");

    final NumShards test = getNumShards("test");

    long providedTTLValue = 3000;
    logger.info("--> checking ttl");
    // Index one doc without routing, one doc with routing, one doc with not TTL and no default and
    // one doc with default TTL
    long now = System.currentTimeMillis();
    IndexResponse indexResponse =
        client()
            .prepareIndex("test", "type1", "1")
            .setSource("field1", "value1")
            .setTimestamp(String.valueOf(now))
            .setTTL(providedTTLValue)
            .setRefresh(true)
            .get();
    assertThat(indexResponse.isCreated(), is(true));
    indexResponse =
        client()
            .prepareIndex("test", "type1", "with_routing")
            .setSource("field1", "value1")
            .setTimestamp(String.valueOf(now))
            .setTTL(providedTTLValue)
            .setRouting("routing")
            .setRefresh(true)
            .get();
    assertThat(indexResponse.isCreated(), is(true));
    indexResponse =
        client().prepareIndex("test", "type1", "no_ttl").setSource("field1", "value1").get();
    assertThat(indexResponse.isCreated(), is(true));
    indexResponse =
        client().prepareIndex("test", "type2", "default_ttl").setSource("field1", "value1").get();
    assertThat(indexResponse.isCreated(), is(true));

    // realtime get check
    long currentTime = System.currentTimeMillis();
    GetResponse getResponse = client().prepareGet("test", "type1", "1").setFields("_ttl").get();
    long ttl0;
    if (getResponse.isExists()) {
      ttl0 = ((Number) getResponse.getField("_ttl").getValue()).longValue();
      assertThat(ttl0, lessThanOrEqualTo(providedTTLValue - (currentTime - now)));
    } else {
      assertThat(providedTTLValue - (currentTime - now), lessThanOrEqualTo(0l));
    }
    // verify the ttl is still decreasing when going to the replica
    currentTime = System.currentTimeMillis();
    getResponse = client().prepareGet("test", "type1", "1").setFields("_ttl").get();
    if (getResponse.isExists()) {
      ttl0 = ((Number) getResponse.getField("_ttl").getValue()).longValue();
      assertThat(ttl0, lessThanOrEqualTo(providedTTLValue - (currentTime - now)));
    } else {
      assertThat(providedTTLValue - (currentTime - now), lessThanOrEqualTo(0l));
    }
    // non realtime get (stored)
    currentTime = System.currentTimeMillis();
    getResponse =
        client().prepareGet("test", "type1", "1").setFields("_ttl").setRealtime(false).get();
    if (getResponse.isExists()) {
      ttl0 = ((Number) getResponse.getField("_ttl").getValue()).longValue();
      assertThat(ttl0, lessThanOrEqualTo(providedTTLValue - (currentTime - now)));
    } else {
      assertThat(providedTTLValue - (currentTime - now), lessThanOrEqualTo(0l));
    }
    // non realtime get going the replica
    currentTime = System.currentTimeMillis();
    getResponse =
        client().prepareGet("test", "type1", "1").setFields("_ttl").setRealtime(false).get();
    if (getResponse.isExists()) {
      ttl0 = ((Number) getResponse.getField("_ttl").getValue()).longValue();
      assertThat(ttl0, lessThanOrEqualTo(providedTTLValue - (currentTime - now)));
    } else {
      assertThat(providedTTLValue - (currentTime - now), lessThanOrEqualTo(0l));
    }

    // no TTL provided so no TTL fetched
    getResponse =
        client()
            .prepareGet("test", "type1", "no_ttl")
            .setFields("_ttl")
            .setRealtime(true)
            .execute()
            .actionGet();
    assertThat(getResponse.getField("_ttl"), nullValue());
    // no TTL provided make sure it has default TTL
    getResponse =
        client()
            .prepareGet("test", "type2", "default_ttl")
            .setFields("_ttl")
            .setRealtime(true)
            .execute()
            .actionGet();
    ttl0 = ((Number) getResponse.getField("_ttl").getValue()).longValue();
    assertThat(ttl0, greaterThan(0L));

    IndicesStatsResponse response =
        client().admin().indices().prepareStats("test").clear().setIndexing(true).get();
    assertThat(
        response.getIndices().get("test").getTotal().getIndexing().getTotal().getDeleteCount(),
        equalTo(0L));

    // make sure the purger has done its job for all indexed docs that are expired
    long shouldBeExpiredDate = now + providedTTLValue + PURGE_INTERVAL + 2000;
    currentTime = System.currentTimeMillis();
    if (shouldBeExpiredDate - currentTime > 0) {
      Thread.sleep(shouldBeExpiredDate - currentTime);
    }

    // We can't assume that after waiting for ttl + purgeInterval (waitTime) that the document have
    // actually been deleted.
    // The ttl purging happens in the background in a different thread, and might not have been
    // completed after waiting for waitTime.
    // But we can use index statistics' delete count to be sure that deletes have been executed,
    // that must be incremented before
    // ttl purging has finished.
    logger.info("--> checking purger");
    assertThat(
        awaitBusy(
            new Predicate<Object>() {
              @Override
              public boolean apply(Object input) {
                if (rarely()) {
                  client().admin().indices().prepareFlush("test").get();
                } else if (rarely()) {
                  client().admin().indices().prepareOptimize("test").setMaxNumSegments(1).get();
                }
                IndicesStatsResponse response =
                    client().admin().indices().prepareStats("test").clear().setIndexing(true).get();
                // TTL deletes two docs, but it is indexed in the primary shard and replica shard.
                return response
                        .getIndices()
                        .get("test")
                        .getTotal()
                        .getIndexing()
                        .getTotal()
                        .getDeleteCount()
                    == 2L * test.dataCopies;
              }
            },
            5,
            TimeUnit.SECONDS),
        equalTo(true));

    // realtime get check
    getResponse =
        client()
            .prepareGet("test", "type1", "1")
            .setFields("_ttl")
            .setRealtime(true)
            .execute()
            .actionGet();
    assertThat(getResponse.isExists(), equalTo(false));
    getResponse =
        client()
            .prepareGet("test", "type1", "with_routing")
            .setRouting("routing")
            .setFields("_ttl")
            .setRealtime(true)
            .execute()
            .actionGet();
    assertThat(getResponse.isExists(), equalTo(false));
    // replica realtime get check
    getResponse =
        client()
            .prepareGet("test", "type1", "1")
            .setFields("_ttl")
            .setRealtime(true)
            .execute()
            .actionGet();
    assertThat(getResponse.isExists(), equalTo(false));
    getResponse =
        client()
            .prepareGet("test", "type1", "with_routing")
            .setRouting("routing")
            .setFields("_ttl")
            .setRealtime(true)
            .execute()
            .actionGet();
    assertThat(getResponse.isExists(), equalTo(false));

    // Need to run a refresh, in order for the non realtime get to work.
    client().admin().indices().prepareRefresh("test").execute().actionGet();

    // non realtime get (stored) check
    getResponse =
        client()
            .prepareGet("test", "type1", "1")
            .setFields("_ttl")
            .setRealtime(false)
            .execute()
            .actionGet();
    assertThat(getResponse.isExists(), equalTo(false));
    getResponse =
        client()
            .prepareGet("test", "type1", "with_routing")
            .setRouting("routing")
            .setFields("_ttl")
            .setRealtime(false)
            .execute()
            .actionGet();
    assertThat(getResponse.isExists(), equalTo(false));
    // non realtime get going the replica check
    getResponse =
        client()
            .prepareGet("test", "type1", "1")
            .setFields("_ttl")
            .setRealtime(false)
            .execute()
            .actionGet();
    assertThat(getResponse.isExists(), equalTo(false));
    getResponse =
        client()
            .prepareGet("test", "type1", "with_routing")
            .setRouting("routing")
            .setFields("_ttl")
            .setRealtime(false)
            .execute()
            .actionGet();
    assertThat(getResponse.isExists(), equalTo(false));
  }
  @Test
  public void simpleGetTests() {
    client.admin().indices().prepareDelete().execute().actionGet();

    client
        .admin()
        .indices()
        .prepareCreate("test")
        .setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1))
        .execute()
        .actionGet();

    ClusterHealthResponse clusterHealth =
        client.admin().cluster().health(clusterHealthRequest().waitForGreenStatus()).actionGet();
    assertThat(clusterHealth.isTimedOut(), equalTo(false));
    assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN));

    GetResponse response = client.prepareGet("test", "type1", "1").execute().actionGet();
    assertThat(response.isExists(), equalTo(false));

    logger.info("--> index doc 1");
    client
        .prepareIndex("test", "type1", "1")
        .setSource("field1", "value1", "field2", "value2")
        .execute()
        .actionGet();

    logger.info("--> realtime get 1");
    response = client.prepareGet("test", "type1", "1").execute().actionGet();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getSourceAsMap().get("field1").toString(), equalTo("value1"));
    assertThat(response.getSourceAsMap().get("field2").toString(), equalTo("value2"));

    logger.info("--> realtime get 1 (no source)");
    response =
        client
            .prepareGet("test", "type1", "1")
            .setFields(Strings.EMPTY_ARRAY)
            .execute()
            .actionGet();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getSourceAsBytes(), nullValue());

    logger.info("--> realtime get 1 (no type)");
    response = client.prepareGet("test", null, "1").execute().actionGet();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getSourceAsMap().get("field1").toString(), equalTo("value1"));
    assertThat(response.getSourceAsMap().get("field2").toString(), equalTo("value2"));

    logger.info("--> non realtime get 1");
    response = client.prepareGet("test", "type1", "1").setRealtime(false).execute().actionGet();
    assertThat(response.isExists(), equalTo(false));

    logger.info("--> realtime fetch of field (requires fetching parsing source)");
    response = client.prepareGet("test", "type1", "1").setFields("field1").execute().actionGet();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getSourceAsBytes(), nullValue());
    assertThat(response.getField("field1").getValues().get(0).toString(), equalTo("value1"));
    assertThat(response.getField("field2"), nullValue());

    logger.info("--> flush the index, so we load it from it");
    client.admin().indices().prepareFlush().execute().actionGet();

    logger.info("--> realtime get 1 (loaded from index)");
    response = client.prepareGet("test", "type1", "1").execute().actionGet();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getSourceAsMap().get("field1").toString(), equalTo("value1"));
    assertThat(response.getSourceAsMap().get("field2").toString(), equalTo("value2"));

    logger.info("--> non realtime get 1 (loaded from index)");
    response = client.prepareGet("test", "type1", "1").setRealtime(false).execute().actionGet();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getSourceAsMap().get("field1").toString(), equalTo("value1"));
    assertThat(response.getSourceAsMap().get("field2").toString(), equalTo("value2"));

    logger.info("--> realtime fetch of field (loaded from index)");
    response = client.prepareGet("test", "type1", "1").setFields("field1").execute().actionGet();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getSourceAsBytes(), nullValue());
    assertThat(response.getField("field1").getValues().get(0).toString(), equalTo("value1"));
    assertThat(response.getField("field2"), nullValue());

    logger.info("--> update doc 1");
    client
        .prepareIndex("test", "type1", "1")
        .setSource("field1", "value1_1", "field2", "value2_1")
        .execute()
        .actionGet();

    logger.info("--> realtime get 1");
    response = client.prepareGet("test", "type1", "1").execute().actionGet();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getSourceAsMap().get("field1").toString(), equalTo("value1_1"));
    assertThat(response.getSourceAsMap().get("field2").toString(), equalTo("value2_1"));

    logger.info("--> update doc 1 again");
    client
        .prepareIndex("test", "type1", "1")
        .setSource("field1", "value1_2", "field2", "value2_2")
        .execute()
        .actionGet();

    response = client.prepareGet("test", "type1", "1").execute().actionGet();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getSourceAsMap().get("field1").toString(), equalTo("value1_2"));
    assertThat(response.getSourceAsMap().get("field2").toString(), equalTo("value2_2"));

    DeleteResponse deleteResponse =
        client.prepareDelete("test", "type1", "1").execute().actionGet();
    assertThat(deleteResponse.isNotFound(), equalTo(false));

    response = client.prepareGet("test", "type1", "1").execute().actionGet();
    assertThat(response.isExists(), equalTo(false));
  }
  @Test
  public void getFieldsWithDifferentTypes() throws Exception {
    client.admin().indices().prepareDelete().execute().actionGet();

    client
        .admin()
        .indices()
        .prepareCreate("test")
        .setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1))
        .addMapping(
            "type1",
            jsonBuilder()
                .startObject()
                .startObject("type")
                .startObject("_source")
                .field("enabled", true)
                .endObject()
                .endObject()
                .endObject())
        .addMapping(
            "type2",
            jsonBuilder()
                .startObject()
                .startObject("type")
                .startObject("_source")
                .field("enabled", false)
                .endObject()
                .startObject("properties")
                .startObject("str")
                .field("type", "string")
                .field("store", "yes")
                .endObject()
                .startObject("int")
                .field("type", "integer")
                .field("store", "yes")
                .endObject()
                .startObject("date")
                .field("type", "date")
                .field("store", "yes")
                .endObject()
                .startObject("binary")
                .field("type", "binary")
                .field("store", "yes")
                .endObject()
                .endObject()
                .endObject()
                .endObject())
        .execute()
        .actionGet();

    ClusterHealthResponse clusterHealth =
        client.admin().cluster().health(clusterHealthRequest().waitForGreenStatus()).actionGet();
    assertThat(clusterHealth.isTimedOut(), equalTo(false));
    assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN));

    client
        .prepareIndex("test", "type1", "1")
        .setSource(
            "str",
            "test",
            "int",
            42,
            "date",
            "2012-11-13T15:26:14.000Z",
            "binary",
            Base64.encodeBytes(new byte[] {1, 2, 3}))
        .execute()
        .actionGet();
    client
        .prepareIndex("test", "type2", "1")
        .setSource(
            "str",
            "test",
            "int",
            42,
            "date",
            "2012-11-13T15:26:14.000Z",
            "binary",
            Base64.encodeBytes(new byte[] {1, 2, 3}))
        .execute()
        .actionGet();

    // realtime get with stored source
    logger.info("--> realtime get (from source)");
    GetResponse getResponse =
        client
            .prepareGet("test", "type1", "1")
            .setFields("str", "int", "date", "binary")
            .execute()
            .actionGet();
    assertThat(getResponse.isExists(), equalTo(true));
    assertThat((String) getResponse.getField("str").getValue(), equalTo("test"));
    assertThat((Long) getResponse.getField("int").getValue(), equalTo(42l));
    assertThat(
        (String) getResponse.getField("date").getValue(), equalTo("2012-11-13T15:26:14.000Z"));
    assertThat(
        getResponse.getField("binary").getValue(),
        instanceOf(String.class)); // its a String..., not binary mapped

    logger.info("--> realtime get (from stored fields)");
    getResponse =
        client
            .prepareGet("test", "type2", "1")
            .setFields("str", "int", "date", "binary")
            .execute()
            .actionGet();
    assertThat(getResponse.isExists(), equalTo(true));
    assertThat((String) getResponse.getField("str").getValue(), equalTo("test"));
    assertThat((Integer) getResponse.getField("int").getValue(), equalTo(42));
    assertThat(
        (String) getResponse.getField("date").getValue(), equalTo("2012-11-13T15:26:14.000Z"));
    assertThat(
        (BytesReference) getResponse.getField("binary").getValue(),
        equalTo((BytesReference) new BytesArray(new byte[] {1, 2, 3})));

    logger.info("--> flush the index, so we load it from it");
    client.admin().indices().prepareFlush().execute().actionGet();

    logger.info("--> non realtime get (from source)");
    getResponse =
        client
            .prepareGet("test", "type1", "1")
            .setFields("str", "int", "date", "binary")
            .execute()
            .actionGet();
    assertThat(getResponse.isExists(), equalTo(true));
    assertThat((String) getResponse.getField("str").getValue(), equalTo("test"));
    assertThat((Long) getResponse.getField("int").getValue(), equalTo(42l));
    assertThat(
        (String) getResponse.getField("date").getValue(), equalTo("2012-11-13T15:26:14.000Z"));
    assertThat(
        getResponse.getField("binary").getValue(),
        instanceOf(String.class)); // its a String..., not binary mapped

    logger.info("--> non realtime get (from stored fields)");
    getResponse =
        client
            .prepareGet("test", "type2", "1")
            .setFields("str", "int", "date", "binary")
            .execute()
            .actionGet();
    assertThat(getResponse.isExists(), equalTo(true));
    assertThat((String) getResponse.getField("str").getValue(), equalTo("test"));
    assertThat((Integer) getResponse.getField("int").getValue(), equalTo(42));
    assertThat(
        (String) getResponse.getField("date").getValue(), equalTo("2012-11-13T15:26:14.000Z"));
    assertThat(
        (BytesReference) getResponse.getField("binary").getValue(),
        equalTo((BytesReference) new BytesArray(new byte[] {1, 2, 3})));
  }
  @Test
  public void testUpdate() throws Exception {
    createIndex();
    ensureGreen();

    try {
      client()
          .prepareUpdate("test", "type1", "1")
          .setScript("ctx._source.field++", ScriptService.ScriptType.INLINE)
          .execute()
          .actionGet();
      fail();
    } catch (DocumentMissingException e) {
      // all is well
    }

    client().prepareIndex("test", "type1", "1").setSource("field", 1).execute().actionGet();

    UpdateResponse updateResponse =
        client()
            .prepareUpdate("test", "type1", "1")
            .setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE)
            .execute()
            .actionGet();
    assertThat(updateResponse.getVersion(), equalTo(2L));
    assertFalse(updateResponse.isCreated());

    for (int i = 0; i < 5; i++) {
      GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
      assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("2"));
    }

    updateResponse =
        client()
            .prepareUpdate("test", "type1", "1")
            .setScript("ctx._source.field += count", ScriptService.ScriptType.INLINE)
            .addScriptParam("count", 3)
            .execute()
            .actionGet();
    assertThat(updateResponse.getVersion(), equalTo(3L));
    assertFalse(updateResponse.isCreated());

    for (int i = 0; i < 5; i++) {
      GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
      assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("5"));
    }

    // check noop
    updateResponse =
        client()
            .prepareUpdate("test", "type1", "1")
            .setScript("ctx.op = 'none'", ScriptService.ScriptType.INLINE)
            .execute()
            .actionGet();
    assertThat(updateResponse.getVersion(), equalTo(3L));
    assertFalse(updateResponse.isCreated());

    for (int i = 0; i < 5; i++) {
      GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
      assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("5"));
    }

    // check delete
    updateResponse =
        client()
            .prepareUpdate("test", "type1", "1")
            .setScript("ctx.op = 'delete'", ScriptService.ScriptType.INLINE)
            .execute()
            .actionGet();
    assertThat(updateResponse.getVersion(), equalTo(4L));
    assertFalse(updateResponse.isCreated());

    for (int i = 0; i < 5; i++) {
      GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
      assertThat(getResponse.isExists(), equalTo(false));
    }

    // check TTL is kept after an update without TTL
    client()
        .prepareIndex("test", "type1", "2")
        .setSource("field", 1)
        .setTTL(86400000L)
        .setRefresh(true)
        .execute()
        .actionGet();
    GetResponse getResponse =
        client().prepareGet("test", "type1", "2").setFields("_ttl").execute().actionGet();
    long ttl = ((Number) getResponse.getField("_ttl").getValue()).longValue();
    assertThat(ttl, greaterThan(0L));
    client()
        .prepareUpdate("test", "type1", "2")
        .setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE)
        .execute()
        .actionGet();
    getResponse = client().prepareGet("test", "type1", "2").setFields("_ttl").execute().actionGet();
    ttl = ((Number) getResponse.getField("_ttl").getValue()).longValue();
    assertThat(ttl, greaterThan(0L));

    // check TTL update
    client()
        .prepareUpdate("test", "type1", "2")
        .setScript("ctx._ttl = 3600000", ScriptService.ScriptType.INLINE)
        .execute()
        .actionGet();
    getResponse = client().prepareGet("test", "type1", "2").setFields("_ttl").execute().actionGet();
    ttl = ((Number) getResponse.getField("_ttl").getValue()).longValue();
    assertThat(ttl, greaterThan(0L));
    assertThat(ttl, lessThanOrEqualTo(3600000L));

    // check timestamp update
    client()
        .prepareIndex("test", "type1", "3")
        .setSource("field", 1)
        .setRefresh(true)
        .execute()
        .actionGet();
    client()
        .prepareUpdate("test", "type1", "3")
        .setScript("ctx._timestamp = \"2009-11-15T14:12:12\"", ScriptService.ScriptType.INLINE)
        .execute()
        .actionGet();
    getResponse =
        client().prepareGet("test", "type1", "3").setFields("_timestamp").execute().actionGet();
    long timestamp = ((Number) getResponse.getField("_timestamp").getValue()).longValue();
    assertThat(timestamp, equalTo(1258294332000L));

    // check fields parameter
    client().prepareIndex("test", "type1", "1").setSource("field", 1).execute().actionGet();
    updateResponse =
        client()
            .prepareUpdate("test", "type1", "1")
            .setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE)
            .setFields("_source", "field")
            .execute()
            .actionGet();
    assertThat(updateResponse.getGetResult(), notNullValue());
    assertThat(updateResponse.getGetResult().sourceRef(), notNullValue());
    assertThat(updateResponse.getGetResult().field("field").getValue(), notNullValue());

    // check updates without script
    // add new field
    client().prepareIndex("test", "type1", "1").setSource("field", 1).execute().actionGet();
    updateResponse =
        client()
            .prepareUpdate("test", "type1", "1")
            .setDoc(XContentFactory.jsonBuilder().startObject().field("field2", 2).endObject())
            .execute()
            .actionGet();
    for (int i = 0; i < 5; i++) {
      getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
      assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("1"));
      assertThat(getResponse.getSourceAsMap().get("field2").toString(), equalTo("2"));
    }

    // change existing field
    updateResponse =
        client()
            .prepareUpdate("test", "type1", "1")
            .setDoc(XContentFactory.jsonBuilder().startObject().field("field", 3).endObject())
            .execute()
            .actionGet();
    for (int i = 0; i < 5; i++) {
      getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
      assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("3"));
      assertThat(getResponse.getSourceAsMap().get("field2").toString(), equalTo("2"));
    }

    // recursive map
    Map<String, Object> testMap = new HashMap<>();
    Map<String, Object> testMap2 = new HashMap<>();
    Map<String, Object> testMap3 = new HashMap<>();
    testMap3.put("commonkey", testMap);
    testMap3.put("map3", 5);
    testMap2.put("map2", 6);
    testMap.put("commonkey", testMap2);
    testMap.put("map1", 8);

    client().prepareIndex("test", "type1", "1").setSource("map", testMap).execute().actionGet();
    updateResponse =
        client()
            .prepareUpdate("test", "type1", "1")
            .setDoc(XContentFactory.jsonBuilder().startObject().field("map", testMap3).endObject())
            .execute()
            .actionGet();
    for (int i = 0; i < 5; i++) {
      getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
      Map map1 = (Map) getResponse.getSourceAsMap().get("map");
      assertThat(map1.size(), equalTo(3));
      assertThat(map1.containsKey("map1"), equalTo(true));
      assertThat(map1.containsKey("map3"), equalTo(true));
      assertThat(map1.containsKey("commonkey"), equalTo(true));
      Map map2 = (Map) map1.get("commonkey");
      assertThat(map2.size(), equalTo(3));
      assertThat(map2.containsKey("map1"), equalTo(true));
      assertThat(map2.containsKey("map2"), equalTo(true));
      assertThat(map2.containsKey("commonkey"), equalTo(true));
    }
  }