Skip to content

Commit

Permalink
Mappings: Lock down _uid field
Browse files Browse the repository at this point in the history
Also, cleanup writePre20Settings so it is shared across all field
mappers.

see #8143
  • Loading branch information
rjernst committed Feb 23, 2015
1 parent 0fa5b87 commit 822ffe9
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ public Mapper.Builder parse(String name, Map<String, Object> 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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ public Mapper.Builder parse(String name, Map<String, Object> 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);
Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -61,7 +61,6 @@ public class UidFieldMapper extends AbstractFieldMapper<Uid> 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;
Expand All @@ -83,7 +82,7 @@ public static class Builder extends AbstractFieldMapper.Builder<Builder, UidFiel

public Builder() {
super(Defaults.NAME, Defaults.FIELD_TYPE);
indexName = Defaults.INDEX_NAME;
indexName = Defaults.NAME;
}

@Override
Expand All @@ -96,6 +95,9 @@ public static class TypeParser implements Mapper.TypeParser {
@Override
public Mapper.Builder<?, ?> parse(String name, Map<String, Object> 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;
}
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1561,16 +1561,15 @@ 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()));
ensureGreen();
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);

Expand Down

0 comments on commit 822ffe9

Please sign in to comment.