diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/TTLFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/TTLFieldMapper.java index aa179f1da2f..d9f672e4c72 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/TTLFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/TTLFieldMapper.java @@ -182,6 +182,7 @@ public class TTLFieldMapper extends LongFieldMapper implements InternalMapper, R long ttl = context.sourceToParse().ttl(); if (ttl <= 0 && defaultTTL > 0) { // no ttl provided so we use the default value ttl = defaultTTL; + context.sourceToParse().ttl(ttl); } if (ttl > 0) { // a ttl has been provided either externally or in the _source long timestamp = context.sourceToParse().timestamp(); diff --git a/src/test/java/org/elasticsearch/test/integration/ttl/SimpleTTLTests.java b/src/test/java/org/elasticsearch/test/integration/ttl/SimpleTTLTests.java index 3b8f00c041b..ed8736660ed 100644 --- a/src/test/java/org/elasticsearch/test/integration/ttl/SimpleTTLTests.java +++ b/src/test/java/org/elasticsearch/test/integration/ttl/SimpleTTLTests.java @@ -72,13 +72,22 @@ public class SimpleTTLTests extends AbstractNodesTests { .startObject("_ttl").field("enabled", true).field("store", "yes").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("store", "yes").field("default", "1d").endObject() + .endObject() + .endObject()) .execute().actionGet(); client.admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet(); long providedTTLValue = 3000; logger.info("--> checking ttl"); - // Index one doc without routing and one doc with routing + // Index one doc without routing, one doc with routing, one doc with not TTL and no default and one doc with default TTL client.prepareIndex("test", "type1", "1").setSource("field1", "value1").setTTL(providedTTLValue).setRefresh(true).execute().actionGet(); client.prepareIndex("test", "type1", "with_routing").setSource("field1", "value1").setTTL(providedTTLValue).setRouting("routing").setRefresh(true).execute().actionGet(); + client.prepareIndex("test", "type1", "no_ttl").setSource("field1", "value1").execute().actionGet(); + client.prepareIndex("test", "type2", "default_ttl").setSource("field1", "value1").execute().actionGet(); long now = System.currentTimeMillis(); // realtime get check @@ -106,6 +115,14 @@ public class SimpleTTLTests extends AbstractNodesTests { assertThat(ttl0, greaterThan(0L)); assertThat(ttl0, lessThan(providedTTLValue - (now1 - now))); + // no TTL provided so no TTL fetched + getResponse = client.prepareGet("test", "type1", "no_ttl").setFields("_ttl").setRealtime(true).execute().actionGet(); + assertThat(getResponse.field("_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.field("_ttl").value()).longValue(); + assertThat(ttl0, greaterThan(0L)); + logger.info("--> checking purger"); // make sure the purger has done its job for all indexed docs that are expired long shouldBeExpiredDate = now + providedTTLValue + purgeInterval + 2000;