From f766b260ba56bfc2077cedd240485dcdae47622d Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Mon, 11 May 2015 13:07:24 -0700 Subject: [PATCH] Add tests for includeInObject backcompat --- docs/reference/migration/migrate_2_0.asciidoc | 4 +- .../mapper/internal/TimestampFieldMapper.java | 2 +- .../mapper/all/SimpleAllMapperTests.java | 35 ++++++------- .../index/mapper/id/IdMappingTests.java | 17 +++++++ .../routing/RoutingTypeMapperTests.java | 49 ++++++++++++++----- .../timestamp/TimestampMappingTests.java | 33 ++++++++++++- .../index/mapper/ttl/TTLMappingTests.java | 20 ++++++++ 7 files changed, 124 insertions(+), 36 deletions(-) diff --git a/docs/reference/migration/migrate_2_0.asciidoc b/docs/reference/migration/migrate_2_0.asciidoc index ebd1ffa92d4..e9c647e406b 100644 --- a/docs/reference/migration/migrate_2_0.asciidoc +++ b/docs/reference/migration/migrate_2_0.asciidoc @@ -281,8 +281,8 @@ to provide special features. They now have limited configuration options. ==== Meta fields in documents 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, -use the `routing` url parameter when indexing that document. +via the API. For example, instead of adding a field `_parent` within a document, +use the `parent` url parameter when indexing that document. ==== Source field limitations The `_source` field could previously be disabled dynamically. Since this field diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/TimestampFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/TimestampFieldMapper.java index 33acd36ba3a..9437ee3d056 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/TimestampFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/TimestampFieldMapper.java @@ -214,7 +214,7 @@ public class TimestampFieldMapper extends DateFieldMapper implements RootMapper super(new Names(Defaults.NAME, Defaults.NAME, Defaults.NAME, Defaults.NAME), dateTimeFormatter, Defaults.PRECISION_STEP_64_BIT, Defaults.BOOST, fieldType, docValues, Defaults.NULL_VALUE, TimeUnit.MILLISECONDS /*always milliseconds*/, - ignoreMalformed, coerce, null, normsLoading, fieldDataSettings, + ignoreMalformed, coerce, null, normsLoading, fieldDataSettings, indexSettings, MultiFields.empty(), null); this.enabledState = enabledState; this.path = path; diff --git a/src/test/java/org/elasticsearch/index/mapper/all/SimpleAllMapperTests.java b/src/test/java/org/elasticsearch/index/mapper/all/SimpleAllMapperTests.java index e4f92367998..89f57c67034 100644 --- a/src/test/java/org/elasticsearch/index/mapper/all/SimpleAllMapperTests.java +++ b/src/test/java/org/elasticsearch/index/mapper/all/SimpleAllMapperTests.java @@ -41,13 +41,12 @@ import org.elasticsearch.index.IndexService; import org.elasticsearch.index.engine.Engine.Searcher; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.DocumentMapperParser; -import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.MapperParsingException; 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.IndexFieldMapper; import org.elasticsearch.index.mapper.internal.SizeFieldMapper; -import org.elasticsearch.index.mapper.internal.SourceFieldMapper; import org.elasticsearch.test.ElasticsearchSingleNodeTest; import org.hamcrest.Matchers; 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.copyToStringFromClasspath; 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.empty; 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.nullValue; -/** - * - */ public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest { - @Test public void testSimpleAllMappers() throws Exception { String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/mapping.json"); 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)); } - @Test public void testAllMappersNoBoost() throws Exception { String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/noboost-mapping.json"); IndexService index = createIndex("test"); @@ -111,7 +104,6 @@ public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest { assertThat(field.fieldType().omitNorms(), equalTo(false)); } - @Test public void testAllMappersTermQuery() throws Exception { String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/mapping_omit_positions_on_all.json"); 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: - @Test public void testAllMappersWithOffsetsTermQuery() throws Exception { String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/mapping_offsets_on_all.json"); 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 - @Test public void testBoostWithOmitPositions() throws Exception { String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/mapping_boost_omit_positions_on_all.json"); 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 - @Test public void testNoBoost() throws Exception { String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/noboost-mapping.json"); 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))); } - - @Test public void testSimpleAllMappersWithReparse() throws Exception { DocumentMapperParser parser = createIndex("test").mapperService().documentMapperParser(); 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)); } - @Test public void testSimpleAllMappersWithStore() throws Exception { String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/store-mapping.json"); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); @@ -211,7 +197,6 @@ public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest { assertThat(field.fieldType().omitNorms(), equalTo(false)); } - @Test public void testSimpleAllMappersWithReparseWithStore() throws Exception { DocumentMapperParser parser = createIndex("test").mapperService().documentMapperParser(); 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)); } - @Test public void testRandom() throws Exception { boolean omitNorms = false; boolean stored = false; @@ -338,7 +322,6 @@ public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest { } - @Test public void testMultiField_includeInAllSetToFalse() throws IOException { 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); @@ -357,7 +340,6 @@ public class SimpleAllMapperTests extends ElasticsearchSingleNodeTest { assertThat(allEntries.fields(), empty()); } - @Test public void testMultiField_defaults() throws IOException { String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/multifield-mapping_default.json"); 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 // test that RootObjectMapping still works - @Test public void testRootObjectMapperPropertiesDoNotCauseException() throws IOException { DocumentMapperParser parser = createIndex("test").mapperService().documentMapperParser(); 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 - @Test public void testRootMappersStillWorking() { String mapping = "{"; Map 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()); + } } diff --git a/src/test/java/org/elasticsearch/index/mapper/id/IdMappingTests.java b/src/test/java/org/elasticsearch/index/mapper/id/IdMappingTests.java index 2d9f6cf3a15..6c46ba2bacb 100644 --- a/src/test/java/org/elasticsearch/index/mapper/id/IdMappingTests.java +++ b/src/test/java/org/elasticsearch/index/mapper/id/IdMappingTests.java @@ -29,6 +29,8 @@ import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.MapperParsingException; 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.UidFieldMapper; import org.elasticsearch.test.ElasticsearchSingleNodeTest; @@ -98,4 +100,19 @@ public class IdMappingTests extends ElasticsearchSingleNodeTest { 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)); + } } diff --git a/src/test/java/org/elasticsearch/index/mapper/routing/RoutingTypeMapperTests.java b/src/test/java/org/elasticsearch/index/mapper/routing/RoutingTypeMapperTests.java index 302f3b3381b..8fd9a8ed3bd 100644 --- a/src/test/java/org/elasticsearch/index/mapper/routing/RoutingTypeMapperTests.java +++ b/src/test/java/org/elasticsearch/index/mapper/routing/RoutingTypeMapperTests.java @@ -21,7 +21,10 @@ package org.elasticsearch.index.mapper.routing; import org.apache.lucene.index.IndexOptions; import org.elasticsearch.Version; +import org.elasticsearch.action.index.IndexRequest; 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.Settings; import org.elasticsearch.common.xcontent.ToXContent; @@ -38,28 +41,23 @@ import java.util.Map; import static org.hamcrest.Matchers.*; -/** - * - */ public class RoutingTypeMapperTests extends ElasticsearchSingleNodeTest { - @Test - public void simpleRoutingMapperTests() throws Exception { + public void testRoutingMapper() throws Exception { String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") .endObject().endObject().string(); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); ParsedDocument doc = docMapper.parse(SourceToParse.source(XContentFactory.jsonBuilder() - .startObject() - .field("field", "value") - .endObject() - .bytes()).type("type").id("1").routing("routing_value")); + .startObject() + .field("field", "value") + .endObject() + .bytes()).type("type").id("1").routing("routing_value")); assertThat(doc.rootDoc().get("_routing"), equalTo("routing_value")); assertThat(doc.rootDoc().get("field"), equalTo("value")); } - @Test public void testFieldTypeSettingsBackcompat() throws Exception { String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") .startObject("_routing") @@ -73,7 +71,6 @@ public class RoutingTypeMapperTests extends ElasticsearchSingleNodeTest { assertEquals(IndexOptions.NONE, docMapper.routingFieldMapper().fieldType().indexOptions()); } - @Test public void testFieldTypeSettingsSerializationBackcompat() throws Exception { String enabledMapping = XContentFactory.jsonBuilder().startObject().startObject("type") .startObject("_routing").field("store", "no").field("index", "no").endObject() @@ -93,4 +90,34 @@ public class RoutingTypeMapperTests extends ElasticsearchSingleNodeTest { assertThat(routingConfiguration, hasKey("index")); 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")); + } } diff --git a/src/test/java/org/elasticsearch/index/mapper/timestamp/TimestampMappingTests.java b/src/test/java/org/elasticsearch/index/mapper/timestamp/TimestampMappingTests.java index 9b0db0ac676..876bdf3ffed 100644 --- a/src/test/java/org/elasticsearch/index/mapper/timestamp/TimestampMappingTests.java +++ b/src/test/java/org/elasticsearch/index/mapper/timestamp/TimestampMappingTests.java @@ -689,7 +689,7 @@ public class TimestampMappingTests extends ElasticsearchSingleNodeTest { docMapper.refreshSource(); docMapper = parser.parse(docMapper.mappingSource().string()); 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) { assertThat(mergeResult.buildConflicts()[0], containsString(conflict)); } @@ -749,4 +749,35 @@ public class TimestampMappingTests extends ElasticsearchSingleNodeTest { assertThat(docMapper.timestampFieldMapper().hasDocValues(), equalTo(docValues)); 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")); + } } diff --git a/src/test/java/org/elasticsearch/index/mapper/ttl/TTLMappingTests.java b/src/test/java/org/elasticsearch/index/mapper/ttl/TTLMappingTests.java index 92b9ba83859..cbd2003bbd7 100644 --- a/src/test/java/org/elasticsearch/index/mapper/ttl/TTLMappingTests.java +++ b/src/test/java/org/elasticsearch/index/mapper/ttl/TTLMappingTests.java @@ -22,7 +22,10 @@ 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.action.index.IndexRequest; 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.compress.CompressedString; 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 { return getMappingWithTtlEnabled(null); }