diff --git a/src/main/java/org/elasticsearch/action/index/IndexRequest.java b/src/main/java/org/elasticsearch/action/index/IndexRequest.java index a63e646267a..2c6d9174bee 100644 --- a/src/main/java/org/elasticsearch/action/index/IndexRequest.java +++ b/src/main/java/org/elasticsearch/action/index/IndexRequest.java @@ -36,6 +36,7 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.lucene.uid.Versions; import org.elasticsearch.common.xcontent.*; import org.elasticsearch.index.VersionType; +import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.internal.TimestampFieldMapper; import java.io.IOException; @@ -567,12 +568,17 @@ public class IndexRequest extends ShardReplicationOperationRequest id = parseContext.id(); } if (parseContext.shouldParseRouting()) { + if (routing != null && !routing.equals(parseContext.routing())) { + throw new MapperParsingException("The provided routing value [" + routing + "] doesn't match the routing key stored in the document: [" + parseContext.routing() + "]"); + } routing = parseContext.routing(); } if (parseContext.shouldParseTimestamp()) { timestamp = parseContext.timestamp(); timestamp = MappingMetaData.Timestamp.parseStringTimestamp(timestamp, mappingMd.timestamp().dateTimeFormatter()); } + } catch (MapperParsingException e) { + throw e; } catch (Exception e) { throw new ElasticsearchParseException("failed to parse doc to extract routing/timestamp/id", e); } finally { diff --git a/src/main/java/org/elasticsearch/cluster/metadata/MappingMetaData.java b/src/main/java/org/elasticsearch/cluster/metadata/MappingMetaData.java index b31bd52b983..67791f5ca09 100644 --- a/src/main/java/org/elasticsearch/cluster/metadata/MappingMetaData.java +++ b/src/main/java/org/elasticsearch/cluster/metadata/MappingMetaData.java @@ -418,9 +418,11 @@ public class MappingMetaData { } public ParseContext createParseContext(@Nullable String id, @Nullable String routing, @Nullable String timestamp) { + // We parse the routing even if there is already a routing key in the request in order to make sure that + // they are the same return new ParseContext( id == null && id().hasPath(), - routing == null && routing().hasPath(), + routing().hasPath(), timestamp == null && timestamp().hasPath() ); } diff --git a/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java b/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java index 2c8186fd58f..b0340311d9f 100644 --- a/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java @@ -522,10 +522,6 @@ public class DocumentMapper implements ToXContent { for (RootMapper rootMapper : rootMappersOrdered) { rootMapper.postParse(context); } - - for (RootMapper rootMapper : rootMappersOrdered) { - rootMapper.validate(context); - } } catch (Throwable e) { // if its already a mapper parsing exception, no need to wrap it... if (e instanceof MapperParsingException) { diff --git a/src/main/java/org/elasticsearch/index/mapper/RootMapper.java b/src/main/java/org/elasticsearch/index/mapper/RootMapper.java index fde3566052e..678c7e86ea1 100644 --- a/src/main/java/org/elasticsearch/index/mapper/RootMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/RootMapper.java @@ -31,8 +31,6 @@ public interface RootMapper extends Mapper { void postParse(ParseContext context) throws IOException; - void validate(ParseContext context) throws MapperParsingException; - /** * Should the mapper be included in the root {@link org.elasticsearch.index.mapper.object.ObjectMapper}. */ diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/AllFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/AllFieldMapper.java index 82055f7ed97..5b1bd7c344e 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/AllFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/AllFieldMapper.java @@ -199,10 +199,6 @@ public class AllFieldMapper extends AbstractFieldMapper implements Interna // we parse in post parse } - @Override - public void validate(ParseContext context) throws MapperParsingException { - } - @Override public boolean includeInObject() { return true; diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/AnalyzerMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/AnalyzerMapper.java index 25f0a339880..264caba4d83 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/AnalyzerMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/AnalyzerMapper.java @@ -124,10 +124,6 @@ public class AnalyzerMapper implements Mapper, InternalMapper, RootMapper { context.analyzer(analyzer); } - @Override - public void validate(ParseContext context) throws MapperParsingException { - } - @Override public boolean includeInObject() { return false; diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/BoostFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/BoostFieldMapper.java index 56ac66010ce..f61c6f17a20 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/BoostFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/BoostFieldMapper.java @@ -236,10 +236,6 @@ public class BoostFieldMapper extends NumberFieldMapper implements Intern public void postParse(ParseContext context) throws IOException { } - @Override - public void validate(ParseContext context) throws MapperParsingException { - } - @Override public boolean includeInObject() { return true; 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 63e10bcf983..e0eeadead04 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/IdFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/IdFieldMapper.java @@ -292,10 +292,6 @@ public class IdFieldMapper extends AbstractFieldMapper implements Intern super.parse(context); } - @Override - public void validate(ParseContext context) throws MapperParsingException { - } - @Override public boolean includeInObject() { return true; diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/IndexFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/IndexFieldMapper.java index b2d9d6f4499..dc89140bf10 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/IndexFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/IndexFieldMapper.java @@ -172,10 +172,6 @@ public class IndexFieldMapper extends AbstractFieldMapper implements Int } - @Override - public void validate(ParseContext context) throws MapperParsingException { - } - @Override public boolean includeInObject() { return false; diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/ParentFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/ParentFieldMapper.java index 886e4e0afe0..547f8398d9f 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/ParentFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/ParentFieldMapper.java @@ -170,10 +170,6 @@ public class ParentFieldMapper extends AbstractFieldMapper implements Inter parse(context); } - @Override - public void validate(ParseContext context) throws MapperParsingException { - } - @Override public boolean includeInObject() { return true; 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 ce8bda06145..4a9244fd642 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/RoutingFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/RoutingFieldMapper.java @@ -35,7 +35,6 @@ import org.elasticsearch.index.codec.postingsformat.PostingsFormatProvider; import org.elasticsearch.index.fielddata.FieldDataType; import org.elasticsearch.index.mapper.*; import org.elasticsearch.index.mapper.core.AbstractFieldMapper; -import org.elasticsearch.index.mapper.core.NumberFieldMapper; import java.io.IOException; import java.util.List; @@ -172,31 +171,6 @@ public class RoutingFieldMapper extends AbstractFieldMapper implements I return value.toString(); } - @Override - public void validate(ParseContext context) throws MapperParsingException { - String routing = context.sourceToParse().routing(); - if (path != null && routing != null) { - // we have a path, check if we can validate we have the same routing value as the one in the doc... - String value = null; - Field field = (Field) context.doc().getField(path); - if (field != null) { - value = field.stringValue(); - if (value == null) { - // maybe its a numeric field... - if (field instanceof NumberFieldMapper.CustomNumericField) { - value = ((NumberFieldMapper.CustomNumericField) field).numericAsString(); - } - } - } - if (value == null) { - value = context.ignoredValue(path); - } - if (!routing.equals(value)) { - throw new MapperParsingException("External routing [" + routing + "] and document path routing [" + value + "] mismatch"); - } - } - } - @Override public void preParse(ParseContext context) throws IOException { super.parse(context); diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/SizeFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/SizeFieldMapper.java index bda7df5889d..abf977bf54c 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/SizeFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/SizeFieldMapper.java @@ -121,10 +121,6 @@ public class SizeFieldMapper extends IntegerFieldMapper implements RootMapper { return this.enabledState.enabled; } - @Override - public void validate(ParseContext context) throws MapperParsingException { - } - @Override public void preParse(ParseContext context) throws IOException { } diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/SourceFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/SourceFieldMapper.java index 7f1f54a9525..b609d8528cb 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/SourceFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/SourceFieldMapper.java @@ -248,10 +248,6 @@ public class SourceFieldMapper extends AbstractFieldMapper implements In // nothing to do here, we will call it in pre parse } - @Override - public void validate(ParseContext context) throws MapperParsingException { - } - @Override public boolean includeInObject() { return false; diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/TTLFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/TTLFieldMapper.java index 49f2899cf6b..cb7637e0e39 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/TTLFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/TTLFieldMapper.java @@ -164,10 +164,6 @@ public class TTLFieldMapper extends LongFieldMapper implements InternalMapper, R return expirationTime - System.currentTimeMillis(); } - @Override - public void validate(ParseContext context) throws MapperParsingException { - } - @Override public void preParse(ParseContext context) throws IOException { } 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 c9e7370b444..41c63c755d5 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/TimestampFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/TimestampFieldMapper.java @@ -179,10 +179,6 @@ public class TimestampFieldMapper extends DateFieldMapper implements InternalMap return value(value); } - @Override - public void validate(ParseContext context) throws MapperParsingException { - } - @Override public void preParse(ParseContext context) throws IOException { super.parse(context); 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 eb168a47c01..6217769e696 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/TypeFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/TypeFieldMapper.java @@ -168,10 +168,6 @@ public class TypeFieldMapper extends AbstractFieldMapper implements Inte // we parse in pre parse } - @Override - public void validate(ParseContext context) throws MapperParsingException { - } - @Override public boolean includeInObject() { return false; 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 23449ff1b5e..28f574ad0b1 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/UidFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/UidFieldMapper.java @@ -165,10 +165,6 @@ public class UidFieldMapper extends AbstractFieldMapper implements Internal // nothing to do here, we either do it in post parse, or in pre parse. } - @Override - public void validate(ParseContext context) throws MapperParsingException { - } - @Override public boolean includeInObject() { return false; diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/VersionFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/VersionFieldMapper.java index 2e794c9ac00..3255ddd9c9d 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/VersionFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/VersionFieldMapper.java @@ -144,10 +144,6 @@ public class VersionFieldMapper extends AbstractFieldMapper implements Int } } - @Override - public void validate(ParseContext context) throws MapperParsingException { - } - @Override public boolean includeInObject() { return false; diff --git a/src/test/java/org/elasticsearch/cluster/metadata/MappingMetaDataParserTests.java b/src/test/java/org/elasticsearch/cluster/metadata/MappingMetaDataParserTests.java index 18fbb5e1948..6e0f6b863e9 100644 --- a/src/test/java/org/elasticsearch/cluster/metadata/MappingMetaDataParserTests.java +++ b/src/test/java/org/elasticsearch/cluster/metadata/MappingMetaDataParserTests.java @@ -43,8 +43,8 @@ public class MappingMetaDataParserTests extends ElasticsearchTestCase { md.parse(XContentFactory.xContent(bytes).createParser(bytes), parseContext); assertThat(parseContext.id(), equalTo("id")); assertThat(parseContext.idResolved(), equalTo(true)); - assertThat(parseContext.routing(), nullValue()); - assertThat(parseContext.routingResolved(), equalTo(false)); + assertThat(parseContext.routing(), equalTo("routing_value")); + assertThat(parseContext.routingResolved(), equalTo(true)); assertThat(parseContext.timestamp(), nullValue()); assertThat(parseContext.timestampResolved(), equalTo(false)); } @@ -106,8 +106,8 @@ public class MappingMetaDataParserTests extends ElasticsearchTestCase { md.parse(XContentFactory.xContent(bytes).createParser(bytes), parseContext); assertThat(parseContext.id(), nullValue()); assertThat(parseContext.idResolved(), equalTo(false)); - assertThat(parseContext.routing(), nullValue()); - assertThat(parseContext.routingResolved(), equalTo(false)); + assertThat(parseContext.routing(), equalTo("routing_value")); + assertThat(parseContext.routingResolved(), equalTo(true)); assertThat(parseContext.timestamp(), equalTo("1")); assertThat(parseContext.timestampResolved(), equalTo(true)); } @@ -160,8 +160,8 @@ public class MappingMetaDataParserTests extends ElasticsearchTestCase { md.parse(XContentFactory.xContent(bytes).createParser(bytes), parseContext); assertThat(parseContext.id(), equalTo("id")); assertThat(parseContext.idResolved(), equalTo(true)); - assertThat(parseContext.routing(), nullValue()); - assertThat(parseContext.routingResolved(), equalTo(false)); + assertThat(parseContext.routing(), equalTo("routing_value")); + assertThat(parseContext.routingResolved(), equalTo(true)); assertThat(parseContext.timestamp(), nullValue()); assertThat(parseContext.timestampResolved(), equalTo(false)); } @@ -202,8 +202,8 @@ public class MappingMetaDataParserTests extends ElasticsearchTestCase { md.parse(XContentFactory.xContent(bytes).createParser(bytes), parseContext); assertThat(parseContext.id(), nullValue()); assertThat(parseContext.idResolved(), equalTo(false)); - assertThat(parseContext.routing(), nullValue()); - assertThat(parseContext.routingResolved(), equalTo(false)); + assertThat(parseContext.routing(), equalTo("routing_value")); + assertThat(parseContext.routingResolved(), equalTo(true)); assertThat(parseContext.timestamp(), equalTo("1")); assertThat(parseContext.timestampResolved(), equalTo(true)); } diff --git a/src/test/java/org/elasticsearch/routing/SimpleRoutingTests.java b/src/test/java/org/elasticsearch/routing/SimpleRoutingTests.java index 0eaa2778f9e..2e97ed34275 100644 --- a/src/test/java/org/elasticsearch/routing/SimpleRoutingTests.java +++ b/src/test/java/org/elasticsearch/routing/SimpleRoutingTests.java @@ -235,6 +235,7 @@ public class SimpleRoutingTests extends ElasticsearchIntegrationTest { client().admin().indices().prepareCreate("test") .addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1") .startObject("_routing").field("required", true).field("path", "routing_field").endObject() + .startObject("routing_field").field("type", "string").field("index", randomBoolean() ? "no" : "not_analyzed").field("doc_values", randomBoolean() ? "yes" : "no").endObject() .endObject().endObject()) .execute().actionGet(); ensureGreen(); @@ -303,12 +304,6 @@ public class SimpleRoutingTests extends ElasticsearchIntegrationTest { client().admin().indices().prepareCreate("test") .addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1") .startObject("_routing").field("required", true).field("path", "routing_field").endObject() - .startObject("properties") - .startObject("routing_field") - .field("type", "long") - .field("doc_values", false) // TODO this test fails with doc values https://github.com/elasticsearch/elasticsearch/pull/5858 - .endObject() - .endObject() .endObject().endObject()) .execute().actionGet(); ensureGreen();