From 822ffe9e701f89e0beea6c5961c8769409d3250f Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Mon, 23 Feb 2015 13:57:33 -0800 Subject: [PATCH] Mappings: Lock down _uid field Also, cleanup writePre20Settings so it is shared across all field mappers. see #8143 --- .../index/mapper/core/AbstractFieldMapper.java | 6 +++--- .../index/mapper/internal/IdFieldMapper.java | 4 +--- .../index/mapper/internal/RoutingFieldMapper.java | 4 +--- .../index/mapper/internal/UidFieldMapper.java | 11 ++++++++--- .../elasticsearch/search/sort/SimpleSortTests.java | 5 ++--- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/elasticsearch/index/mapper/core/AbstractFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/core/AbstractFieldMapper.java index 3bf5785572bc6..ee0c66e9ccb37 100644 --- a/src/main/java/org/elasticsearch/index/mapper/core/AbstractFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/core/AbstractFieldMapper.java @@ -302,7 +302,7 @@ protected String buildFullName(BuilderContext context) { protected FieldDataType fieldDataType; protected final MultiFields multiFields; protected CopyTo copyTo; - protected final boolean writePre20Metadata; + protected final boolean writePre2xSettings; protected AbstractFieldMapper(Names names, float boost, FieldType fieldType, Boolean docValues, NamedAnalyzer indexAnalyzer, NamedAnalyzer searchAnalyzer, SimilarityProvider similarity, @@ -348,7 +348,7 @@ protected AbstractFieldMapper(Names names, float boost, FieldType fieldType, Boo } this.multiFields = multiFields; this.copyTo = copyTo; - this.writePre20Metadata = Version.indexCreated(indexSettings).before(Version.V_2_0_0); + this.writePre2xSettings = Version.indexCreated(indexSettings).before(Version.V_2_0_0); } @Nullable @@ -683,7 +683,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException { builder.field("type", contentType()); - if (writePre20Metadata && (includeDefaults || !names.name().equals(names.indexNameClean()))) { + if (writePre2xSettings && (includeDefaults || !names.name().equals(names.indexNameClean()))) { builder.field("index_name", names.indexNameClean()); } diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/IdFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/IdFieldMapper.java index 4fd93289d950e..06eb4419a7b72 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/IdFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/IdFieldMapper.java @@ -137,7 +137,6 @@ public Mapper.Builder parse(String name, Map node, ParserContext } private final String path; - private final boolean writePre20Settings; public IdFieldMapper(Settings indexSettings) { this(Defaults.NAME, Defaults.INDEX_NAME, Defaults.BOOST, idFieldType(indexSettings), null, Defaults.PATH, null, indexSettings); @@ -148,7 +147,6 @@ protected IdFieldMapper(String name, String indexName, float boost, FieldType fi super(new Names(name, indexName, indexName, name), boost, fieldType, docValues, Lucene.KEYWORD_ANALYZER, Lucene.KEYWORD_ANALYZER, null, null, fieldDataSettings, indexSettings); this.path = path; - this.writePre20Settings = Version.indexCreated(indexSettings).before(Version.V_2_0_0); } private static FieldType idFieldType(Settings indexSettings) { @@ -363,7 +361,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws if (includeDefaults || fieldType.indexOptions() != Defaults.FIELD_TYPE.indexOptions()) { builder.field("index", indexTokenizeOptionToString(fieldType.indexOptions() != IndexOptions.NONE, fieldType.tokenized())); } - if (writePre20Settings && (includeDefaults || path != Defaults.PATH)) { + if (writePre2xSettings && (includeDefaults || path != Defaults.PATH)) { builder.field("path", path); } diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/RoutingFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/RoutingFieldMapper.java index 2e8a00a237f4c..039a0ad63bf4e 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/RoutingFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/RoutingFieldMapper.java @@ -123,7 +123,6 @@ public Mapper.Builder parse(String name, Map node, ParserContext private boolean required; private final String path; - private final boolean writePre20Settings; public RoutingFieldMapper(Settings indexSettings) { this(Defaults.FIELD_TYPE, Defaults.REQUIRED, Defaults.PATH, null, indexSettings); @@ -134,7 +133,6 @@ protected RoutingFieldMapper(FieldType fieldType, boolean required, String path, Lucene.KEYWORD_ANALYZER, null, null, fieldDataSettings, indexSettings); this.required = required; this.path = path; - this.writePre20Settings = Version.indexCreated(indexSettings).before(Version.V_2_0_0); } @Override @@ -238,7 +236,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws if (includeDefaults || required != Defaults.REQUIRED) { builder.field("required", required); } - if (writePre20Settings && (includeDefaults || path != Defaults.PATH)) { + if (writePre2xSettings && (includeDefaults || path != Defaults.PATH)) { builder.field("path", path); } builder.endObject(); diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/UidFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/UidFieldMapper.java index a09aba22bece8..479572d38ad0a 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/UidFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/UidFieldMapper.java @@ -26,9 +26,9 @@ import org.apache.lucene.index.IndexableField; import org.apache.lucene.index.Term; import org.apache.lucene.util.BytesRef; +import org.elasticsearch.Version; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.lucene.Lucene; -import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.fielddata.FieldDataType; @@ -61,7 +61,6 @@ public class UidFieldMapper extends AbstractFieldMapper implements Internal public static class Defaults extends AbstractFieldMapper.Defaults { public static final String NAME = UidFieldMapper.NAME; - public static final String INDEX_NAME = UidFieldMapper.NAME; public static final FieldType FIELD_TYPE = new FieldType(AbstractFieldMapper.Defaults.FIELD_TYPE); public static final FieldType NESTED_FIELD_TYPE; @@ -83,7 +82,7 @@ public static class Builder extends AbstractFieldMapper.Builder parse(String name, Map node, ParserContext parserContext) throws MapperParsingException { Builder builder = uid(); + if (parserContext.indexVersionCreated().onOrAfter(Version.V_2_0_0)) { + throw new MapperParsingException(NAME + " is not configurable"); + } parseField(builder, builder.name, node, parserContext); return builder; } @@ -200,6 +202,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 defaults, don't output diff --git a/src/test/java/org/elasticsearch/search/sort/SimpleSortTests.java b/src/test/java/org/elasticsearch/search/sort/SimpleSortTests.java index 900c44e2dc0a9..1af9f623bef7b 100644 --- a/src/test/java/org/elasticsearch/search/sort/SimpleSortTests.java +++ b/src/test/java/org/elasticsearch/search/sort/SimpleSortTests.java @@ -1561,8 +1561,7 @@ public void testSortMetaField() throws Exception { final boolean idDocValues = maybeDocValues(); final boolean timestampDocValues = maybeDocValues(); assertAcked(prepareCreate("test") - .addMapping("typ", XContentFactory.jsonBuilder().startObject().startObject("typ") - .startObject("_uid").startObject("fielddata").field("format", maybeDocValues() ? "doc_values" : null).endObject().endObject() + .addMapping("type", XContentFactory.jsonBuilder().startObject().startObject("type") .startObject("_id").field("index", !idDocValues || randomBoolean() ? "not_analyzed" : "no").startObject("fielddata").field("format", idDocValues ? "doc_values" : null).endObject().endObject() .startObject("_timestamp").field("enabled", true).field("store", true).field("index", !timestampDocValues || randomBoolean() ? "not_analyzed" : "no").startObject("fielddata").field("format", timestampDocValues ? "doc_values" : null).endObject().endObject() .endObject().endObject())); @@ -1570,7 +1569,7 @@ public void testSortMetaField() throws Exception { final int numDocs = randomIntBetween(10, 20); IndexRequestBuilder[] indexReqs = new IndexRequestBuilder[numDocs]; for (int i = 0; i < numDocs; ++i) { - indexReqs[i] = client().prepareIndex("test", "typ", Integer.toString(i)).setTimestamp(Integer.toString(randomInt(1000))).setSource(); + indexReqs[i] = client().prepareIndex("test", "type", Integer.toString(i)).setTimestamp(Integer.toString(randomInt(1000))).setSource(); } indexRandom(true, indexReqs);