Mappings: Lock down _size field

This also changes the stored setting for _size to true (for
indexes created in 2.x).

see #8143
closes #9913
This commit is contained in:
Ryan Ernst 2015-02-26 14:02:24 -08:00
parent f87fa8172e
commit 9d708e20a0
7 changed files with 24 additions and 22 deletions

View File

@ -257,6 +257,7 @@ to provide special features. They now have limited configuration options.
* `_routing` configuration is limited to requiring the field. * `_routing` configuration is limited to requiring the field.
* `_boost` has been removed. * `_boost` has been removed.
* `_field_names` configuration is limited to disabling the field. * `_field_names` configuration is limited to disabling the field.
* `_size` configuration is limited to enabling the field.
=== Codecs === Codecs

View File

@ -21,6 +21,7 @@ package org.elasticsearch.index.mapper.internal;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType; import org.apache.lucene.document.FieldType;
import org.elasticsearch.Version;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
@ -55,6 +56,7 @@ public class SizeFieldMapper extends IntegerFieldMapper implements RootMapper {
public static final FieldType SIZE_FIELD_TYPE = new FieldType(IntegerFieldMapper.Defaults.FIELD_TYPE); public static final FieldType SIZE_FIELD_TYPE = new FieldType(IntegerFieldMapper.Defaults.FIELD_TYPE);
static { static {
SIZE_FIELD_TYPE.setStored(true);
SIZE_FIELD_TYPE.freeze(); SIZE_FIELD_TYPE.freeze();
} }
} }
@ -90,7 +92,7 @@ public class SizeFieldMapper extends IntegerFieldMapper implements RootMapper {
if (fieldName.equals("enabled")) { if (fieldName.equals("enabled")) {
builder.enabled(nodeBooleanValue(fieldNode) ? EnabledAttributeMapper.ENABLED : EnabledAttributeMapper.DISABLED); builder.enabled(nodeBooleanValue(fieldNode) ? EnabledAttributeMapper.ENABLED : EnabledAttributeMapper.DISABLED);
iterator.remove(); iterator.remove();
} else if (fieldName.equals("store")) { } else if (fieldName.equals("store") && parserContext.indexVersionCreated().before(Version.V_2_0_0)) {
builder.store(parseStore(fieldName, fieldNode.toString())); builder.store(parseStore(fieldName, fieldNode.toString()));
iterator.remove(); iterator.remove();
} }
@ -162,14 +164,14 @@ public class SizeFieldMapper extends IntegerFieldMapper implements RootMapper {
boolean includeDefaults = params.paramAsBoolean("include_defaults", false); boolean includeDefaults = params.paramAsBoolean("include_defaults", false);
// all are defaults, no need to write it at all // all are defaults, no need to write it at all
if (!includeDefaults && enabledState == Defaults.ENABLED_STATE && fieldType().stored() == Defaults.SIZE_FIELD_TYPE.stored()) { if (!includeDefaults && enabledState == Defaults.ENABLED_STATE && (writePre2xSettings == false || fieldType().stored() == false)) {
return builder; return builder;
} }
builder.startObject(contentType()); builder.startObject(contentType());
if (includeDefaults || enabledState != Defaults.ENABLED_STATE) { if (includeDefaults || enabledState != Defaults.ENABLED_STATE) {
builder.field("enabled", enabledState.enabled); builder.field("enabled", enabledState.enabled);
} }
if (includeDefaults || fieldType().stored() != Defaults.SIZE_FIELD_TYPE.stored()) { if (writePre2xSettings && (includeDefaults || fieldType().stored() == true)) {
builder.field("store", fieldType().stored()); builder.field("store", fieldType().stored());
} }
builder.endObject(); builder.endObject();

View File

@ -1067,8 +1067,8 @@ public class GetActionTests extends ElasticsearchIntegrationTest {
@Test @Test
public void testUngeneratedFieldsNotPartOfSourceUnstored() throws IOException { public void testUngeneratedFieldsNotPartOfSourceUnstored() throws IOException {
indexSingleDocumentWithUngeneratedFieldsThatAreNeverPartOf_source(false, randomBoolean()); indexSingleDocumentWithUngeneratedFieldsThatAreNeverPartOf_source(false, randomBoolean());
String[] fieldsList = {"_timestamp", "_size"}; String[] fieldsList = {"_timestamp"};
String[] alwaysStoredFieldsList = {"_routing"}; String[] alwaysStoredFieldsList = {"_routing", "_size"};
// before refresh - document is only in translog // before refresh - document is only in translog
assertGetFieldsAlwaysNull(indexOrAlias(), "doc", "1", fieldsList, "1"); assertGetFieldsAlwaysNull(indexOrAlias(), "doc", "1", fieldsList, "1");
assertGetFieldsAlwaysWorks(indexOrAlias(), "doc", "1", alwaysStoredFieldsList, "1"); assertGetFieldsAlwaysWorks(indexOrAlias(), "doc", "1", alwaysStoredFieldsList, "1");
@ -1111,7 +1111,6 @@ public class GetActionTests extends ElasticsearchIntegrationTest {
" \"enabled\": true\n" + " \"enabled\": true\n" +
" },\n" + " },\n" +
" \"_size\": {\n" + " \"_size\": {\n" +
" \"store\": \"" + storedString + "\",\n" +
" \"enabled\": true\n" + " \"enabled\": true\n" +
" }\n" + " }\n" +
" }\n" + " }\n" +

View File

@ -19,7 +19,11 @@
package org.elasticsearch.index.mapper.size; package org.elasticsearch.index.mapper.size;
import org.elasticsearch.Version;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.DocumentMapperParser; import org.elasticsearch.index.mapper.DocumentMapperParser;
@ -32,7 +36,6 @@ import static org.hamcrest.Matchers.*;
public class SizeMappingTests extends ElasticsearchSingleNodeTest { public class SizeMappingTests extends ElasticsearchSingleNodeTest {
@Test
public void testSizeEnabled() throws Exception { public void testSizeEnabled() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_size").field("enabled", true).endObject() .startObject("_size").field("enabled", true).endObject()
@ -46,29 +49,28 @@ public class SizeMappingTests extends ElasticsearchSingleNodeTest {
.bytes(); .bytes();
ParsedDocument doc = docMapper.parse(SourceToParse.source(source).type("type").id("1")); ParsedDocument doc = docMapper.parse(SourceToParse.source(source).type("type").id("1"));
assertThat(doc.rootDoc().getField("_size").fieldType().stored(), equalTo(false));
assertThat(doc.rootDoc().getField("_size").tokenStream(docMapper.mappers().indexAnalyzer(), null), notNullValue());
}
@Test
public void testSizeEnabledAndStored() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_size").field("enabled", true).field("store", "yes").endObject()
.endObject().endObject().string();
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping);
BytesReference source = XContentFactory.jsonBuilder()
.startObject()
.field("field", "value")
.endObject()
.bytes();
ParsedDocument doc = docMapper.parse(SourceToParse.source(source).type("type").id("1"));
assertThat(doc.rootDoc().getField("_size").fieldType().stored(), equalTo(true)); assertThat(doc.rootDoc().getField("_size").fieldType().stored(), equalTo(true));
assertThat(doc.rootDoc().getField("_size").tokenStream(docMapper.mappers().indexAnalyzer(), null), notNullValue()); assertThat(doc.rootDoc().getField("_size").tokenStream(docMapper.mappers().indexAnalyzer(), null), notNullValue());
} }
@Test public void testSizeEnabledAndStoredBackcompat() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_size").field("enabled", true).field("store", "yes").endObject()
.endObject().endObject().string();
Settings indexSettings = ImmutableSettings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_4_2.id).build();
DocumentMapper docMapper = createIndex("test", indexSettings).mapperService().documentMapperParser().parse(mapping);
BytesReference source = XContentFactory.jsonBuilder()
.startObject()
.field("field", "value")
.endObject()
.bytes();
ParsedDocument doc = docMapper.parse(SourceToParse.source(source).type("type").id("1"));
assertThat(doc.rootDoc().getField("_size").fieldType().stored(), equalTo(true));
assertThat(doc.rootDoc().getField("_size").tokenStream(docMapper.mappers().indexAnalyzer(), null), notNullValue());
}
public void testSizeDisabled() throws Exception { public void testSizeDisabled() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_size").field("enabled", false).endObject() .startObject("_size").field("enabled", false).endObject()
@ -85,7 +87,6 @@ public class SizeMappingTests extends ElasticsearchSingleNodeTest {
assertThat(doc.rootDoc().getField("_size"), nullValue()); assertThat(doc.rootDoc().getField("_size"), nullValue());
} }
@Test
public void testSizeNotSet() throws Exception { public void testSizeNotSet() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.endObject().endObject().string(); .endObject().endObject().string();
@ -101,7 +102,6 @@ public class SizeMappingTests extends ElasticsearchSingleNodeTest {
assertThat(doc.rootDoc().getField("_size"), nullValue()); assertThat(doc.rootDoc().getField("_size"), nullValue());
} }
@Test
public void testThatDisablingWorksWhenMerging() throws Exception { public void testThatDisablingWorksWhenMerging() throws Exception {
String enabledMapping = XContentFactory.jsonBuilder().startObject().startObject("type") String enabledMapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_size").field("enabled", true).endObject() .startObject("_size").field("enabled", true).endObject()

View File

@ -166,7 +166,6 @@ public class UpdateMappingTests extends ElasticsearchSingleNodeTest {
.startObject("type") .startObject("type")
.startObject("_size") .startObject("_size")
.field("enabled", enabled) .field("enabled", enabled)
.field("store", true)
.endObject() .endObject()
.endObject() .endObject()
.endObject(); .endObject();

View File

@ -560,7 +560,7 @@ public class PercolatorTests extends ElasticsearchIntegrationTest {
@Test @Test
public void percolateWithSizeField() throws Exception { public void percolateWithSizeField() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type1") String mapping = XContentFactory.jsonBuilder().startObject().startObject("type1")
.startObject("_size").field("enabled", true).field("store", "yes").endObject() .startObject("_size").field("enabled", true).endObject()
.startObject("properties").startObject("field1").field("type", "string").endObject().endObject() .startObject("properties").startObject("field1").field("type", "string").endObject().endObject()
.endObject().endObject().string(); .endObject().endObject().string();

View File

@ -72,8 +72,9 @@ public class SearchFieldsTests extends ElasticsearchIntegrationTest {
client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForYellowStatus().execute().actionGet(); client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForYellowStatus().execute().actionGet();
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type1") String mapping = XContentFactory.jsonBuilder().startObject().startObject("type1")
// _timestamp is randomly enabled via templates but we don't want it here to test stored fields behaviour // _timestamp and _size are randomly enabled via templates but we don't want it here to test stored fields behaviour
.startObject("_timestamp").field("enabled", false).endObject() .startObject("_timestamp").field("enabled", false).endObject()
.startObject("_size").field("enabled", false).endObject()
.startObject("properties") .startObject("properties")
.startObject("field1").field("type", "string").field("store", "yes").endObject() .startObject("field1").field("type", "string").field("store", "yes").endObject()
.startObject("field2").field("type", "string").field("store", "no").endObject() .startObject("field2").field("type", "string").field("store", "no").endObject()