Mappings: Lock down _uid field

Also, cleanup writePre20Settings so it is shared across all field
mappers.

see #8143
This commit is contained in:
Ryan Ernst 2015-02-23 13:57:33 -08:00
parent 0fa5b87fdd
commit 822ffe9e70
5 changed files with 15 additions and 15 deletions

View File

@ -302,7 +302,7 @@ public abstract class AbstractFieldMapper<T> implements FieldMapper<T> {
protected FieldDataType fieldDataType; protected FieldDataType fieldDataType;
protected final MultiFields multiFields; protected final MultiFields multiFields;
protected CopyTo copyTo; protected CopyTo copyTo;
protected final boolean writePre20Metadata; protected final boolean writePre2xSettings;
protected AbstractFieldMapper(Names names, float boost, FieldType fieldType, Boolean docValues, NamedAnalyzer indexAnalyzer, protected AbstractFieldMapper(Names names, float boost, FieldType fieldType, Boolean docValues, NamedAnalyzer indexAnalyzer,
NamedAnalyzer searchAnalyzer, SimilarityProvider similarity, NamedAnalyzer searchAnalyzer, SimilarityProvider similarity,
@ -348,7 +348,7 @@ public abstract class AbstractFieldMapper<T> implements FieldMapper<T> {
} }
this.multiFields = multiFields; this.multiFields = multiFields;
this.copyTo = copyTo; 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 @Nullable
@ -683,7 +683,7 @@ public abstract class AbstractFieldMapper<T> implements FieldMapper<T> {
protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException { protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException {
builder.field("type", contentType()); 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()); builder.field("index_name", names.indexNameClean());
} }

View File

@ -137,7 +137,6 @@ public class IdFieldMapper extends AbstractFieldMapper<String> implements Intern
} }
private final String path; private final String path;
private final boolean writePre20Settings;
public IdFieldMapper(Settings indexSettings) { public IdFieldMapper(Settings indexSettings) {
this(Defaults.NAME, Defaults.INDEX_NAME, Defaults.BOOST, idFieldType(indexSettings), null, Defaults.PATH, null, indexSettings); this(Defaults.NAME, Defaults.INDEX_NAME, Defaults.BOOST, idFieldType(indexSettings), null, Defaults.PATH, null, indexSettings);
@ -148,7 +147,6 @@ public class IdFieldMapper extends AbstractFieldMapper<String> implements Intern
super(new Names(name, indexName, indexName, name), boost, fieldType, docValues, Lucene.KEYWORD_ANALYZER, super(new Names(name, indexName, indexName, name), boost, fieldType, docValues, Lucene.KEYWORD_ANALYZER,
Lucene.KEYWORD_ANALYZER, null, null, fieldDataSettings, indexSettings); Lucene.KEYWORD_ANALYZER, null, null, fieldDataSettings, indexSettings);
this.path = path; this.path = path;
this.writePre20Settings = Version.indexCreated(indexSettings).before(Version.V_2_0_0);
} }
private static FieldType idFieldType(Settings indexSettings) { private static FieldType idFieldType(Settings indexSettings) {
@ -363,7 +361,7 @@ public class IdFieldMapper extends AbstractFieldMapper<String> implements Intern
if (includeDefaults || fieldType.indexOptions() != Defaults.FIELD_TYPE.indexOptions()) { if (includeDefaults || fieldType.indexOptions() != Defaults.FIELD_TYPE.indexOptions()) {
builder.field("index", indexTokenizeOptionToString(fieldType.indexOptions() != IndexOptions.NONE, fieldType.tokenized())); 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); builder.field("path", path);
} }

View File

@ -123,7 +123,6 @@ public class RoutingFieldMapper extends AbstractFieldMapper<String> implements I
private boolean required; private boolean required;
private final String path; private final String path;
private final boolean writePre20Settings;
public RoutingFieldMapper(Settings indexSettings) { public RoutingFieldMapper(Settings indexSettings) {
this(Defaults.FIELD_TYPE, Defaults.REQUIRED, Defaults.PATH, null, indexSettings); this(Defaults.FIELD_TYPE, Defaults.REQUIRED, Defaults.PATH, null, indexSettings);
@ -134,7 +133,6 @@ public class RoutingFieldMapper extends AbstractFieldMapper<String> implements I
Lucene.KEYWORD_ANALYZER, null, null, fieldDataSettings, indexSettings); Lucene.KEYWORD_ANALYZER, null, null, fieldDataSettings, indexSettings);
this.required = required; this.required = required;
this.path = path; this.path = path;
this.writePre20Settings = Version.indexCreated(indexSettings).before(Version.V_2_0_0);
} }
@Override @Override
@ -238,7 +236,7 @@ public class RoutingFieldMapper extends AbstractFieldMapper<String> implements I
if (includeDefaults || required != Defaults.REQUIRED) { if (includeDefaults || required != Defaults.REQUIRED) {
builder.field("required", required); builder.field("required", required);
} }
if (writePre20Settings && (includeDefaults || path != Defaults.PATH)) { if (writePre2xSettings && (includeDefaults || path != Defaults.PATH)) {
builder.field("path", path); builder.field("path", path);
} }
builder.endObject(); builder.endObject();

View File

@ -26,9 +26,9 @@ import org.apache.lucene.index.IndexOptions;
import org.apache.lucene.index.IndexableField; import org.apache.lucene.index.IndexableField;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRef;
import org.elasticsearch.Version;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.lucene.Lucene;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.fielddata.FieldDataType; import org.elasticsearch.index.fielddata.FieldDataType;
@ -61,7 +61,6 @@ public class UidFieldMapper extends AbstractFieldMapper<Uid> implements Internal
public static class Defaults extends AbstractFieldMapper.Defaults { public static class Defaults extends AbstractFieldMapper.Defaults {
public static final String NAME = UidFieldMapper.NAME; 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 FIELD_TYPE = new FieldType(AbstractFieldMapper.Defaults.FIELD_TYPE);
public static final FieldType NESTED_FIELD_TYPE; public static final FieldType NESTED_FIELD_TYPE;
@ -83,7 +82,7 @@ public class UidFieldMapper extends AbstractFieldMapper<Uid> implements Internal
public Builder() { public Builder() {
super(Defaults.NAME, Defaults.FIELD_TYPE); super(Defaults.NAME, Defaults.FIELD_TYPE);
indexName = Defaults.INDEX_NAME; indexName = Defaults.NAME;
} }
@Override @Override
@ -96,6 +95,9 @@ public class UidFieldMapper extends AbstractFieldMapper<Uid> implements Internal
@Override @Override
public Mapper.Builder<?, ?> parse(String name, Map<String, Object> node, ParserContext parserContext) throws MapperParsingException { public Mapper.Builder<?, ?> parse(String name, Map<String, Object> node, ParserContext parserContext) throws MapperParsingException {
Builder builder = uid(); 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); parseField(builder, builder.name, node, parserContext);
return builder; return builder;
} }
@ -200,6 +202,9 @@ public class UidFieldMapper extends AbstractFieldMapper<Uid> implements Internal
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
if (writePre2xSettings == false) {
return builder;
}
boolean includeDefaults = params.paramAsBoolean("include_defaults", false); boolean includeDefaults = params.paramAsBoolean("include_defaults", false);
// if defaults, don't output // if defaults, don't output

View File

@ -1561,8 +1561,7 @@ public class SimpleSortTests extends ElasticsearchIntegrationTest {
final boolean idDocValues = maybeDocValues(); final boolean idDocValues = maybeDocValues();
final boolean timestampDocValues = maybeDocValues(); final boolean timestampDocValues = maybeDocValues();
assertAcked(prepareCreate("test") assertAcked(prepareCreate("test")
.addMapping("typ", XContentFactory.jsonBuilder().startObject().startObject("typ") .addMapping("type", XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_uid").startObject("fielddata").field("format", maybeDocValues() ? "doc_values" : null).endObject().endObject()
.startObject("_id").field("index", !idDocValues || randomBoolean() ? "not_analyzed" : "no").startObject("fielddata").field("format", idDocValues ? "doc_values" : null).endObject().endObject() .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() .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())); .endObject().endObject()));
@ -1570,7 +1569,7 @@ public class SimpleSortTests extends ElasticsearchIntegrationTest {
final int numDocs = randomIntBetween(10, 20); final int numDocs = randomIntBetween(10, 20);
IndexRequestBuilder[] indexReqs = new IndexRequestBuilder[numDocs]; IndexRequestBuilder[] indexReqs = new IndexRequestBuilder[numDocs];
for (int i = 0; i < numDocs; ++i) { 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); indexRandom(true, indexReqs);