Skip to content

Commit

Permalink
Mappings: Lock down _ttl field
Browse files Browse the repository at this point in the history
While the parser allowed changing field type settings, these would never
have been serialized.  So this change simply removes parsing using
parseField. Backcompat will still work if a user uploads old settings
(they just would never have worked anyways, so we continue ignoring
them with 1.x, and 2.x will now error).

see elastic#8143
closes elastic#9914
  • Loading branch information
rjernst committed Feb 27, 2015
1 parent 9d708e2 commit dd0084d
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 15 deletions.
1 change: 0 additions & 1 deletion rest-api-spec/test/index/75_ttl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
test:
_ttl:
enabled: 1
store: yes
default: 10s
- do:
cluster.health:
Expand Down
1 change: 0 additions & 1 deletion rest-api-spec/test/update/75_ttl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
test:
_ttl:
enabled: 1
store: yes
default: 10s
- do:
cluster.health:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeBooleanValue;
import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeTimeValue;
import static org.elasticsearch.index.mapper.MapperBuilders.ttl;
import static org.elasticsearch.index.mapper.core.TypeParsers.parseField;

public class TTLFieldMapper extends LongFieldMapper implements InternalMapper, RootMapper {

Expand Down Expand Up @@ -103,7 +102,6 @@ public static class TypeParser implements Mapper.TypeParser {
@Override
public Mapper.Builder parse(String name, Map<String, Object> node, ParserContext parserContext) throws MapperParsingException {
TTLFieldMapper.Builder builder = ttl();
parseField(builder, builder.name, node, parserContext);
for (Iterator<Map.Entry<String, Object>> iterator = node.entrySet().iterator(); iterator.hasNext();) {
Map.Entry<String, Object> entry = iterator.next();
String fieldName = Strings.toUnderscoreCase(entry.getKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
package org.elasticsearch.index.mapper.ttl;

import org.apache.lucene.index.IndexOptions;
import org.elasticsearch.Version;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.compress.CompressedString;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.mapper.*;
Expand Down Expand Up @@ -83,16 +86,17 @@ public void testDefaultValues() throws Exception {


@Test
public void testSetValues() throws Exception {
public void testSetValuesBackcompat() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_ttl")
.field("enabled", "yes").field("store", "no").field("index", "no")
.field("enabled", "yes").field("store", "no")
.endObject()
.endObject().endObject().string();
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping);
Settings indexSettings = ImmutableSettings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_4_2.id).build();
DocumentMapper docMapper = createIndex("test", indexSettings).mapperService().documentMapperParser().parse(mapping);
assertThat(docMapper.TTLFieldMapper().enabled(), equalTo(true));
assertThat(docMapper.TTLFieldMapper().fieldType().stored(), equalTo(false));
assertEquals(IndexOptions.NONE, docMapper.TTLFieldMapper().fieldType().indexOptions());
assertThat(docMapper.TTLFieldMapper().fieldType().stored(), equalTo(true)); // store was never serialized, so it was always lost

}

@Test
Expand All @@ -103,7 +107,7 @@ public void testThatEnablingTTLFieldOnMergeWorks() throws Exception {

String mappingWithTtl = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_ttl")
.field("enabled", "yes").field("store", "no").field("index", "no")
.field("enabled", "yes")
.endObject()
.startObject("properties").field("field").startObject().field("type", "string").endObject().endObject()
.endObject().endObject().string();
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/elasticsearch/ttl/SimpleTTLTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ public void testSimpleTTL() throws Exception {
.startObject()
.startObject("type1")
.startObject("_timestamp").field("enabled", true).field("store", "yes").endObject()
.startObject("_ttl").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("store", "yes").field("default", "1d").endObject()
.startObject("_ttl").field("enabled", true).field("default", "1d").endObject()
.endObject()
.endObject()));
ensureYellow("test");
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/org/elasticsearch/update/UpdateTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private void createTestIndex() throws Exception {
.startObject()
.startObject("type1")
.startObject("_timestamp").field("enabled", true).field("store", "yes").endObject()
.startObject("_ttl").field("enabled", true).field("store", "yes").endObject()
.startObject("_ttl").field("enabled", true).endObject()
.endObject()
.endObject()));
}
Expand Down Expand Up @@ -474,7 +474,7 @@ public void testContextVariables() throws Exception {
.startObject("subtype1")
.startObject("_parent").field("type", "type1").endObject()
.startObject("_timestamp").field("enabled", true).field("store", "yes").endObject()
.startObject("_ttl").field("enabled", true).field("store", "yes").endObject()
.startObject("_ttl").field("enabled", true).endObject()
.endObject()
.endObject())
.execute().actionGet();
Expand Down Expand Up @@ -624,7 +624,7 @@ public void stressUpdateDeleteConcurrency() throws Exception {
.startObject()
.startObject("type1")
.startObject("_timestamp").field("enabled", true).field("store", "yes").endObject()
.startObject("_ttl").field("enabled", true).field("store", "yes").endObject()
.startObject("_ttl").field("enabled", true).endObject()
.endObject()
.endObject())
.setSettings(ImmutableSettings.builder().put(MergePolicyModule.MERGE_POLICY_TYPE_KEY, NoMergePolicyProvider.class)));
Expand Down

0 comments on commit dd0084d

Please sign in to comment.