From be0cef0c43052ed54919d04571b38464a19ed29f Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Tue, 24 Feb 2015 15:56:46 -0800 Subject: [PATCH] Mappings: Lock down _type field see #8143 closes #9869 --- docs/reference/migration/migrate_2_0.asciidoc | 1 + .../index/mapper/internal/TypeFieldMapper.java | 7 +++++++ .../org/elasticsearch/count/query/CountQueryTests.java | 3 ++- .../index/mapper/all/SimpleAllMapperTests.java | 1 - .../elasticsearch/index/mapper/simple/test-mapping.json | 2 -- .../org/elasticsearch/search/query/SearchQueryTests.java | 3 ++- .../search/suggest/ContextSuggestSearchTests.java | 1 - .../elasticsearch/test/ElasticsearchIntegrationTest.java | 5 ----- 8 files changed, 12 insertions(+), 11 deletions(-) diff --git a/docs/reference/migration/migrate_2_0.asciidoc b/docs/reference/migration/migrate_2_0.asciidoc index d540b72b7e40a..7e95f10758e71 100644 --- a/docs/reference/migration/migrate_2_0.asciidoc +++ b/docs/reference/migration/migrate_2_0.asciidoc @@ -242,6 +242,7 @@ Meta fields (those beginning with underscore) are fields used by elasticsearch to provide special features. They now have limited configuration options. * `_id` configuration can no longer be changed. If you need to sort, use `_uid` instead. +* `_type` configuration can no longer be changed. === Codecs diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/TypeFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/TypeFieldMapper.java index f408c734c6244..2de0bbfaeaca6 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/TypeFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/TypeFieldMapper.java @@ -30,6 +30,7 @@ import org.apache.lucene.search.PrefixFilter; import org.apache.lucene.search.Query; import org.apache.lucene.util.BytesRef; +import org.elasticsearch.Version; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.lucene.BytesRefs; import org.elasticsearch.common.lucene.Lucene; @@ -94,6 +95,9 @@ public TypeFieldMapper build(BuilderContext context) { public static class TypeParser implements Mapper.TypeParser { @Override public Mapper.Builder parse(String name, Map node, ParserContext parserContext) throws MapperParsingException { + if (parserContext.indexVersionCreated().onOrAfter(Version.V_2_0_0)) { + throw new MapperParsingException(NAME + " is not configurable"); + } TypeFieldMapper.Builder builder = type(); parseField(builder, builder.name, node, parserContext); return builder; @@ -187,6 +191,9 @@ protected String contentType() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + if (writePre2xSettings == false) { + return builder; + } boolean includeDefaults = params.paramAsBoolean("include_defaults", false); // if all are defaults, no sense to write it at all diff --git a/src/test/java/org/elasticsearch/count/query/CountQueryTests.java b/src/test/java/org/elasticsearch/count/query/CountQueryTests.java index 8d5646b8368bd..ae1a600a31a85 100644 --- a/src/test/java/org/elasticsearch/count/query/CountQueryTests.java +++ b/src/test/java/org/elasticsearch/count/query/CountQueryTests.java @@ -227,7 +227,8 @@ public void typeFilterTypeNotIndexedTests() throws Exception { } private void typeFilterTests(String index) throws Exception { - assertAcked(prepareCreate("test") + Settings indexSettings = ImmutableSettings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_4_2.id).build(); + assertAcked(prepareCreate("test").setSettings(indexSettings) .addMapping("type1", jsonBuilder().startObject().startObject("type1") .startObject("_type").field("index", index).endObject() .endObject().endObject()) 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 6efd811763829..526201075f2fa 100644 --- a/src/test/java/org/elasticsearch/index/mapper/all/SimpleAllMapperTests.java +++ b/src/test/java/org/elasticsearch/index/mapper/all/SimpleAllMapperTests.java @@ -417,7 +417,6 @@ public void testRootMappersStillWorking() { rootTypes.put(SizeFieldMapper.NAME, "{\"enabled\" : true}"); rootTypes.put(IndexFieldMapper.NAME, "{\"enabled\" : true}"); rootTypes.put(SourceFieldMapper.NAME, "{\"enabled\" : true}"); - rootTypes.put(TypeFieldMapper.NAME, "{\"store\" : true}"); rootTypes.put("include_in_all", "true"); rootTypes.put("dynamic_date_formats", "[\"yyyy-MM-dd\", \"dd-MM-yyyy\"]"); rootTypes.put("numeric_detection", "true"); diff --git a/src/test/java/org/elasticsearch/index/mapper/simple/test-mapping.json b/src/test/java/org/elasticsearch/index/mapper/simple/test-mapping.json index f0e97d4bf3f7d..ce2805cabc41f 100644 --- a/src/test/java/org/elasticsearch/index/mapper/simple/test-mapping.json +++ b/src/test/java/org/elasticsearch/index/mapper/simple/test-mapping.json @@ -8,8 +8,6 @@ enabled:true, _source:{ }, - _type:{ - }, _boost:{ null_value:2.0 }, diff --git a/src/test/java/org/elasticsearch/search/query/SearchQueryTests.java b/src/test/java/org/elasticsearch/search/query/SearchQueryTests.java index b27679e6d9134..e0a17c024198e 100644 --- a/src/test/java/org/elasticsearch/search/query/SearchQueryTests.java +++ b/src/test/java/org/elasticsearch/search/query/SearchQueryTests.java @@ -597,7 +597,8 @@ public void typeFilterTypeNotIndexedTests() throws Exception { } private void typeFilterTests(String index) throws Exception { - assertAcked(prepareCreate("test") + Settings indexSettings = ImmutableSettings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_4_2.id).build(); + assertAcked(prepareCreate("test").setSettings(indexSettings) .addMapping("type1", jsonBuilder().startObject().startObject("type1") .startObject("_type").field("index", index).endObject() .endObject().endObject()) diff --git a/src/test/java/org/elasticsearch/search/suggest/ContextSuggestSearchTests.java b/src/test/java/org/elasticsearch/search/suggest/ContextSuggestSearchTests.java index c8cdfeb55818d..c3d33e55dae2a 100644 --- a/src/test/java/org/elasticsearch/search/suggest/ContextSuggestSearchTests.java +++ b/src/test/java/org/elasticsearch/search/suggest/ContextSuggestSearchTests.java @@ -967,7 +967,6 @@ private XContentBuilder createMapping(String type, String indexAnalyzer, String XContentBuilder mapping = jsonBuilder(); mapping.startObject(); mapping.startObject(type); - mapping.startObject("_type").field("index", "not_analyzed").endObject(); // Forcefully configure the _type field, since it can be randomized and if used as context it needs to be enabled mapping.startObject("properties"); mapping.startObject(FIELD); mapping.field("type", "completion"); diff --git a/src/test/java/org/elasticsearch/test/ElasticsearchIntegrationTest.java b/src/test/java/org/elasticsearch/test/ElasticsearchIntegrationTest.java index 68a68f47e39e0..b251f6fbc3d14 100644 --- a/src/test/java/org/elasticsearch/test/ElasticsearchIntegrationTest.java +++ b/src/test/java/org/elasticsearch/test/ElasticsearchIntegrationTest.java @@ -312,11 +312,6 @@ private void randomIndexTemplate() throws IOException { XContentBuilder mappings = null; if (frequently() && randomDynamicTemplates()) { mappings = XContentFactory.jsonBuilder().startObject().startObject("_default_"); - if (randomBoolean()) { - mappings.startObject(TypeFieldMapper.NAME) - .field("index", randomFrom("no", "not_analyzed")) - .endObject(); - } if (randomBoolean()) { mappings.startObject(TimestampFieldMapper.NAME) .field("enabled", randomBoolean())