Add tests for includeInObject backcompat

This commit is contained in:
Ryan Ernst 2015-05-11 13:07:24 -07:00
parent 565ffb16f1
commit f766b260ba
7 changed files with 124 additions and 36 deletions

View File

@ -281,8 +281,8 @@ to provide special features. They now have limited configuration options.
==== Meta fields in documents ==== Meta fields in documents
Meta fields can no longer be specified within a document. They should be specified Meta fields can no longer be specified within a document. They should be specified
via the API. For example, instead of adding a field `_routing` within a document, via the API. For example, instead of adding a field `_parent` within a document,
use the `routing` url parameter when indexing that document. use the `parent` url parameter when indexing that document.
==== Source field limitations ==== Source field limitations
The `_source` field could previously be disabled dynamically. Since this field The `_source` field could previously be disabled dynamically. Since this field

View File

@ -214,7 +214,7 @@ public class TimestampFieldMapper extends DateFieldMapper implements RootMapper
super(new Names(Defaults.NAME, Defaults.NAME, Defaults.NAME, Defaults.NAME), dateTimeFormatter, super(new Names(Defaults.NAME, Defaults.NAME, Defaults.NAME, Defaults.NAME), dateTimeFormatter,
Defaults.PRECISION_STEP_64_BIT, Defaults.BOOST, fieldType, docValues, Defaults.PRECISION_STEP_64_BIT, Defaults.BOOST, fieldType, docValues,
Defaults.NULL_VALUE, TimeUnit.MILLISECONDS /*always milliseconds*/, Defaults.NULL_VALUE, TimeUnit.MILLISECONDS /*always milliseconds*/,
ignoreMalformed, coerce, null, normsLoading, fieldDataSettings, ignoreMalformed, coerce, null, normsLoading, fieldDataSettings,
indexSettings, MultiFields.empty(), null); indexSettings, MultiFields.empty(), null);
this.enabledState = enabledState; this.enabledState = enabledState;
this.path = path; this.path = path;

View File

@ -41,13 +41,12 @@ import org.elasticsearch.index.IndexService;
import org.elasticsearch.index.engine.Engine.Searcher; import org.elasticsearch.index.engine.Engine.Searcher;
import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.DocumentMapperParser; import org.elasticsearch.index.mapper.DocumentMapperParser;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.MapperParsingException;
import org.elasticsearch.index.mapper.ParseContext.Document; import org.elasticsearch.index.mapper.ParseContext.Document;
import org.elasticsearch.index.mapper.ParsedDocument;
import org.elasticsearch.index.mapper.internal.AllFieldMapper; import org.elasticsearch.index.mapper.internal.AllFieldMapper;
import org.elasticsearch.index.mapper.internal.IndexFieldMapper; import org.elasticsearch.index.mapper.internal.IndexFieldMapper;
import org.elasticsearch.index.mapper.internal.SizeFieldMapper; import org.elasticsearch.index.mapper.internal.SizeFieldMapper;
import org.elasticsearch.index.mapper.internal.SourceFieldMapper;
import org.elasticsearch.test.ElasticsearchSingleNodeTest; import org.elasticsearch.test.ElasticsearchSingleNodeTest;
import org.hamcrest.Matchers; import org.hamcrest.Matchers;
import org.junit.Test; import org.junit.Test;
@ -62,7 +61,6 @@ import java.util.Map;
import static org.elasticsearch.common.io.Streams.copyToBytesFromClasspath; import static org.elasticsearch.common.io.Streams.copyToBytesFromClasspath;
import static org.elasticsearch.common.io.Streams.copyToStringFromClasspath; import static org.elasticsearch.common.io.Streams.copyToStringFromClasspath;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
@ -71,12 +69,8 @@ import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.nullValue;
/**
*
*/
public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest { public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest {
@Test
public void testSimpleAllMappers() throws Exception { public void testSimpleAllMappers() throws Exception {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/mapping.json"); String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/mapping.json");
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping);
@ -95,7 +89,6 @@ public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest {
assertThat(mapper.queryStringTermQuery(new Term("_all", "foobar")), Matchers.instanceOf(AllTermQuery.class)); assertThat(mapper.queryStringTermQuery(new Term("_all", "foobar")), Matchers.instanceOf(AllTermQuery.class));
} }
@Test
public void testAllMappersNoBoost() throws Exception { public void testAllMappersNoBoost() throws Exception {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/noboost-mapping.json"); String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/noboost-mapping.json");
IndexService index = createIndex("test"); IndexService index = createIndex("test");
@ -111,7 +104,6 @@ public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest {
assertThat(field.fieldType().omitNorms(), equalTo(false)); assertThat(field.fieldType().omitNorms(), equalTo(false));
} }
@Test
public void testAllMappersTermQuery() throws Exception { public void testAllMappersTermQuery() throws Exception {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/mapping_omit_positions_on_all.json"); String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/mapping_omit_positions_on_all.json");
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping);
@ -130,7 +122,6 @@ public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest {
} }
// #6187: make sure we see AllTermQuery even when offsets are indexed in the _all field: // #6187: make sure we see AllTermQuery even when offsets are indexed in the _all field:
@Test
public void testAllMappersWithOffsetsTermQuery() throws Exception { public void testAllMappersWithOffsetsTermQuery() throws Exception {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/mapping_offsets_on_all.json"); String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/mapping_offsets_on_all.json");
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping);
@ -150,7 +141,6 @@ public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest {
} }
// #6187: if _all doesn't index positions then we never use AllTokenStream, even if some fields have boost // #6187: if _all doesn't index positions then we never use AllTokenStream, even if some fields have boost
@Test
public void testBoostWithOmitPositions() throws Exception { public void testBoostWithOmitPositions() throws Exception {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/mapping_boost_omit_positions_on_all.json"); String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/mapping_boost_omit_positions_on_all.json");
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping);
@ -162,7 +152,6 @@ public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest {
} }
// #6187: if no fields were boosted, we shouldn't use AllTokenStream // #6187: if no fields were boosted, we shouldn't use AllTokenStream
@Test
public void testNoBoost() throws Exception { public void testNoBoost() throws Exception {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/noboost-mapping.json"); String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/noboost-mapping.json");
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping);
@ -173,8 +162,6 @@ public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest {
assertThat(field.tokenStream(docMapper.mappers().indexAnalyzer(), null), Matchers.not(Matchers.instanceOf(AllTokenStream.class))); assertThat(field.tokenStream(docMapper.mappers().indexAnalyzer(), null), Matchers.not(Matchers.instanceOf(AllTokenStream.class)));
} }
@Test
public void testSimpleAllMappersWithReparse() throws Exception { public void testSimpleAllMappersWithReparse() throws Exception {
DocumentMapperParser parser = createIndex("test").mapperService().documentMapperParser(); DocumentMapperParser parser = createIndex("test").mapperService().documentMapperParser();
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/mapping.json"); String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/mapping.json");
@ -194,7 +181,6 @@ public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest {
assertThat(field.fieldType().omitNorms(), equalTo(true)); assertThat(field.fieldType().omitNorms(), equalTo(true));
} }
@Test
public void testSimpleAllMappersWithStore() throws Exception { public void testSimpleAllMappersWithStore() throws Exception {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/store-mapping.json"); String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/store-mapping.json");
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping);
@ -211,7 +197,6 @@ public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest {
assertThat(field.fieldType().omitNorms(), equalTo(false)); assertThat(field.fieldType().omitNorms(), equalTo(false));
} }
@Test
public void testSimpleAllMappersWithReparseWithStore() throws Exception { public void testSimpleAllMappersWithReparseWithStore() throws Exception {
DocumentMapperParser parser = createIndex("test").mapperService().documentMapperParser(); DocumentMapperParser parser = createIndex("test").mapperService().documentMapperParser();
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/store-mapping.json"); String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/store-mapping.json");
@ -233,7 +218,6 @@ public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest {
assertThat(field.fieldType().omitNorms(), equalTo(false)); assertThat(field.fieldType().omitNorms(), equalTo(false));
} }
@Test
public void testRandom() throws Exception { public void testRandom() throws Exception {
boolean omitNorms = false; boolean omitNorms = false;
boolean stored = false; boolean stored = false;
@ -338,7 +322,6 @@ public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest {
} }
@Test
public void testMultiField_includeInAllSetToFalse() throws IOException { public void testMultiField_includeInAllSetToFalse() throws IOException {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/multifield-mapping_include_in_all_set_to_false.json"); String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/multifield-mapping_include_in_all_set_to_false.json");
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping);
@ -357,7 +340,6 @@ public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest {
assertThat(allEntries.fields(), empty()); assertThat(allEntries.fields(), empty());
} }
@Test
public void testMultiField_defaults() throws IOException { public void testMultiField_defaults() throws IOException {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/multifield-mapping_default.json"); String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/multifield-mapping_default.json");
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping);
@ -399,7 +381,6 @@ public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest {
// issue https://github.com/elasticsearch/elasticsearch/issues/5864 // issue https://github.com/elasticsearch/elasticsearch/issues/5864
// test that RootObjectMapping still works // test that RootObjectMapping still works
@Test
public void testRootObjectMapperPropertiesDoNotCauseException() throws IOException { public void testRootObjectMapperPropertiesDoNotCauseException() throws IOException {
DocumentMapperParser parser = createIndex("test").mapperService().documentMapperParser(); DocumentMapperParser parser = createIndex("test").mapperService().documentMapperParser();
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/type_dynamic_template_mapping.json"); String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/type_dynamic_template_mapping.json");
@ -413,7 +394,6 @@ public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest {
} }
// issue https://github.com/elasticsearch/elasticsearch/issues/5864 // issue https://github.com/elasticsearch/elasticsearch/issues/5864
@Test
public void testRootMappersStillWorking() { public void testRootMappersStillWorking() {
String mapping = "{"; String mapping = "{";
Map<String, String> rootTypes = new HashMap<>(); Map<String, String> rootTypes = new HashMap<>();
@ -472,4 +452,17 @@ public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest {
} }
} }
} }
public void testIncludeInObjectBackcompat() throws Exception {
String mapping = jsonBuilder().startObject().startObject("type").endObject().endObject().string();
Settings settings = ImmutableSettings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_4_2.id).build();
DocumentMapper docMapper = createIndex("test", settings).mapperService().documentMapperParser().parse(mapping);
ParsedDocument doc = docMapper.parse("type", "1", XContentFactory.jsonBuilder()
.startObject().field("_all", "foo").endObject().bytes());
assertNull(doc.rootDoc().get("_all"));
AllField field = (AllField) doc.rootDoc().getField("_all");
// the backcompat behavior is actually ignoring directly specifying _all
assertFalse(field.getAllEntries().fields().iterator().hasNext());
}
} }

View File

@ -29,6 +29,8 @@ import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.MapperParsingException;
import org.elasticsearch.index.mapper.ParsedDocument; import org.elasticsearch.index.mapper.ParsedDocument;
import org.elasticsearch.index.mapper.SourceToParse;
import org.elasticsearch.index.mapper.Uid;
import org.elasticsearch.index.mapper.internal.IdFieldMapper; import org.elasticsearch.index.mapper.internal.IdFieldMapper;
import org.elasticsearch.index.mapper.internal.UidFieldMapper; import org.elasticsearch.index.mapper.internal.UidFieldMapper;
import org.elasticsearch.test.ElasticsearchSingleNodeTest; import org.elasticsearch.test.ElasticsearchSingleNodeTest;
@ -98,4 +100,19 @@ public class IdMappingTests extends ElasticsearchSingleNodeTest {
assertThat(serialized_id_mapping, equalTo(expected_id_mapping)); assertThat(serialized_id_mapping, equalTo(expected_id_mapping));
} }
public void testIncludeInObjectBackcompat() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").endObject().endObject().string();
Settings settings = ImmutableSettings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_4_2.id).build();
DocumentMapper docMapper = createIndex("test", settings).mapperService().documentMapperParser().parse(mapping);
ParsedDocument doc = docMapper.parse(SourceToParse.source(XContentFactory.jsonBuilder()
.startObject()
.field("_id", "1")
.endObject()
.bytes()).type("type"));
// _id is not indexed so we need to check _uid
assertEquals(Uid.createUid("type", "1"), doc.rootDoc().get(UidFieldMapper.NAME));
}
} }

View File

@ -21,7 +21,10 @@ package org.elasticsearch.index.mapper.routing;
import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexOptions;
import org.elasticsearch.Version; import org.elasticsearch.Version;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContent;
@ -38,28 +41,23 @@ import java.util.Map;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
/**
*
*/
public class RoutingTypeMapperTests extends ElasticsearchSingleNodeTest { public class RoutingTypeMapperTests extends ElasticsearchSingleNodeTest {
@Test public void testRoutingMapper() throws Exception {
public void simpleRoutingMapperTests() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.endObject().endObject().string(); .endObject().endObject().string();
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping);
ParsedDocument doc = docMapper.parse(SourceToParse.source(XContentFactory.jsonBuilder() ParsedDocument doc = docMapper.parse(SourceToParse.source(XContentFactory.jsonBuilder()
.startObject() .startObject()
.field("field", "value") .field("field", "value")
.endObject() .endObject()
.bytes()).type("type").id("1").routing("routing_value")); .bytes()).type("type").id("1").routing("routing_value"));
assertThat(doc.rootDoc().get("_routing"), equalTo("routing_value")); assertThat(doc.rootDoc().get("_routing"), equalTo("routing_value"));
assertThat(doc.rootDoc().get("field"), equalTo("value")); assertThat(doc.rootDoc().get("field"), equalTo("value"));
} }
@Test
public void testFieldTypeSettingsBackcompat() throws Exception { public void testFieldTypeSettingsBackcompat() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_routing") .startObject("_routing")
@ -73,7 +71,6 @@ public class RoutingTypeMapperTests extends ElasticsearchSingleNodeTest {
assertEquals(IndexOptions.NONE, docMapper.routingFieldMapper().fieldType().indexOptions()); assertEquals(IndexOptions.NONE, docMapper.routingFieldMapper().fieldType().indexOptions());
} }
@Test
public void testFieldTypeSettingsSerializationBackcompat() throws Exception { public void testFieldTypeSettingsSerializationBackcompat() throws Exception {
String enabledMapping = XContentFactory.jsonBuilder().startObject().startObject("type") String enabledMapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_routing").field("store", "no").field("index", "no").endObject() .startObject("_routing").field("store", "no").field("index", "no").endObject()
@ -93,4 +90,34 @@ public class RoutingTypeMapperTests extends ElasticsearchSingleNodeTest {
assertThat(routingConfiguration, hasKey("index")); assertThat(routingConfiguration, hasKey("index"));
assertThat(routingConfiguration.get("index").toString(), is("no")); assertThat(routingConfiguration.get("index").toString(), is("no"));
} }
public void testPathBackcompat() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_routing").field("path", "custom_routing").endObject()
.endObject().endObject().string();
Settings settings = ImmutableSettings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_4_2.id).build();
DocumentMapper docMapper = createIndex("test", settings).mapperService().documentMapperParser().parse(mapping);
XContentBuilder doc = XContentFactory.jsonBuilder().startObject().field("custom_routing", "routing_value").endObject();
MappingMetaData mappingMetaData = new MappingMetaData(docMapper);
IndexRequest request = new IndexRequest("test", "type", "1").source(doc);
request.process(MetaData.builder().build(), mappingMetaData, true, "test");
assertEquals(request.routing(), "routing_value");
}
public void testIncludeInObjectBackcompat() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").endObject().endObject().string();
Settings settings = ImmutableSettings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_4_2.id).build();
DocumentMapper docMapper = createIndex("test", settings).mapperService().documentMapperParser().parse(mapping);
XContentBuilder doc = XContentFactory.jsonBuilder().startObject().field("_timestamp", 2000000).endObject();
MappingMetaData mappingMetaData = new MappingMetaData(docMapper);
IndexRequest request = new IndexRequest("test", "type", "1").source(doc);
request.process(MetaData.builder().build(), mappingMetaData, true, "test");
// _routing in a document never worked, so backcompat is ignoring the field
assertNull(request.routing());
assertNull(docMapper.parse("type", "1", doc.bytes()).rootDoc().get("_routing"));
}
} }

View File

@ -689,7 +689,7 @@ public class TimestampMappingTests extends ElasticsearchSingleNodeTest {
docMapper.refreshSource(); docMapper.refreshSource();
docMapper = parser.parse(docMapper.mappingSource().string()); docMapper = parser.parse(docMapper.mappingSource().string());
MergeResult mergeResult = docMapper.merge(parser.parse(mapping2).mapping(), true); MergeResult mergeResult = docMapper.merge(parser.parse(mapping2).mapping(), true);
assertThat(mergeResult.buildConflicts().length, equalTo(conflict == null ? 0:1)); assertThat(mergeResult.buildConflicts().length, equalTo(conflict == null ? 0 : 1));
if (conflict != null) { if (conflict != null) {
assertThat(mergeResult.buildConflicts()[0], containsString(conflict)); assertThat(mergeResult.buildConflicts()[0], containsString(conflict));
} }
@ -749,4 +749,35 @@ public class TimestampMappingTests extends ElasticsearchSingleNodeTest {
assertThat(docMapper.timestampFieldMapper().hasDocValues(), equalTo(docValues)); assertThat(docMapper.timestampFieldMapper().hasDocValues(), equalTo(docValues));
assertAcked(client().admin().indices().prepareDelete("test_doc_values")); assertAcked(client().admin().indices().prepareDelete("test_doc_values"));
} }
public void testPath() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_timestamp").field("enabled", true).field("path", "custom_timestamp").endObject()
.endObject().endObject().string();
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping);
XContentBuilder doc = XContentFactory.jsonBuilder().startObject().field("custom_timestamp", 1).endObject();
MappingMetaData mappingMetaData = new MappingMetaData(docMapper);
IndexRequest request = new IndexRequest("test", "type", "1").source(doc);
request.process(MetaData.builder().build(), mappingMetaData, true, "test");
assertEquals(request.timestamp(), "1");
}
public void testIncludeInObjectBackcompat() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_timestamp").field("enabled", true).field("default", "1970").field("format", "YYYY").endObject()
.endObject().endObject().string();
Settings settings = ImmutableSettings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_4_2.id).build();
DocumentMapper docMapper = createIndex("test", settings).mapperService().documentMapperParser().parse(mapping);
XContentBuilder doc = XContentFactory.jsonBuilder().startObject().field("_timestamp", 2000000).endObject();
MappingMetaData mappingMetaData = new MappingMetaData(docMapper);
IndexRequest request = new IndexRequest("test", "type", "1").source(doc);
request.process(MetaData.builder().build(), mappingMetaData, true, "test");
// _timestamp in a document never worked, so backcompat is ignoring the field
assertEquals(MappingMetaData.Timestamp.parseStringTimestamp("1970", Joda.forPattern("YYYY")), request.timestamp());
assertNull(docMapper.parse("type", "1", doc.bytes()).rootDoc().get("_timestamp"));
}
} }

View File

@ -22,7 +22,10 @@ package org.elasticsearch.index.mapper.ttl;
import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexOptions;
import org.elasticsearch.Version; import org.elasticsearch.Version;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse; import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.compress.CompressedString; import org.elasticsearch.common.compress.CompressedString;
import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.ImmutableSettings;
@ -291,6 +294,23 @@ public class TTLMappingTests extends ElasticsearchSingleNodeTest {
} }
public void testIncludeInObjectBackcompat() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_ttl").field("enabled", true).endObject()
.endObject().endObject().string();
Settings settings = ImmutableSettings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_4_2.id).build();
DocumentMapper docMapper = createIndex("test", settings).mapperService().documentMapperParser().parse(mapping);
XContentBuilder doc = XContentFactory.jsonBuilder().startObject().field("_ttl", "2d").endObject();
MappingMetaData mappingMetaData = new MappingMetaData(docMapper);
IndexRequest request = new IndexRequest("test", "type", "1").source(doc);
request.process(MetaData.builder().build(), mappingMetaData, true, "test");
// _ttl in a document never worked, so backcompat is ignoring the field
assertEquals(-1, request.ttl());
assertNull(docMapper.parse("type", "1", doc.bytes()).rootDoc().get("_ttl"));
}
private org.elasticsearch.common.xcontent.XContentBuilder getMappingWithTtlEnabled() throws IOException { private org.elasticsearch.common.xcontent.XContentBuilder getMappingWithTtlEnabled() throws IOException {
return getMappingWithTtlEnabled(null); return getMappingWithTtlEnabled(null);
} }