Skip to content

Commit

Permalink
Mappings: Fix _field_names to be disabled on pre 1.3.0 indexes
Browse files Browse the repository at this point in the history
In elastic#9893, an enabled flag was added for _field_names.  However,
backcompat for indexes created before 1.3.0 (when _field_names
was added) was lost. This change corrects the mapper
to always be disabled when used with older indexes that
cannot have _field_names.

closes elastic#10268
  • Loading branch information
rjernst committed Mar 25, 2015
1 parent de816c2 commit bd16117
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public Mapper.Builder parse(String name, Map<String, Object> node, ParserContext

private final FieldType defaultFieldType;
private EnabledAttributeMapper enabledState;
private final boolean pre13Index; // if the index was created before 1.3, _field_names is always disabled

public FieldNamesFieldMapper(Settings indexSettings) {
this(Defaults.NAME, Defaults.NAME, Defaults.BOOST, new FieldType(Defaults.FIELD_TYPE), null, null, Defaults.ENABLED_STATE, null, indexSettings);
Expand All @@ -144,11 +145,12 @@ public FieldNamesFieldMapper(String name, String indexName, float boost, FieldTy
super(new Names(name, indexName, indexName, name), boost, fieldType, null, Lucene.KEYWORD_ANALYZER,
Lucene.KEYWORD_ANALYZER, postingsProvider, docValuesProvider, null, null, fieldDataSettings, indexSettings);
this.defaultFieldType = Defaults.FIELD_TYPE;
this.pre13Index = Version.indexCreated(indexSettings).before(Version.V_1_3_0);
this.enabledState = enabledState;
}

public boolean enabled() {
return enabledState.enabled;
return pre13Index == false && enabledState.enabled;
}

@Override
Expand Down Expand Up @@ -255,6 +257,9 @@ protected String contentType() {

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
if (pre13Index) {
return builder;
}
boolean includeDefaults = params.paramAsBoolean("include_defaults", false);

if (includeDefaults == false && fieldType().equals(Defaults.FIELD_TYPE) && enabledState == Defaults.ENABLED_STATE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.merge.policy.MergePolicyModule;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.node.internal.InternalNode;
import org.elasticsearch.rest.action.admin.indices.upgrade.UpgradeTest;
Expand All @@ -50,6 +51,7 @@
import java.util.*;

import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.hamcrest.CoreMatchers.equalTo;

@TimeoutSuite(millis = 40 * TimeUnits.MINUTE)
public class OldIndexBackwardsCompatibilityTests extends StaticIndexBackwardCompatibilityTest {
Expand Down Expand Up @@ -144,6 +146,11 @@ void assertBasicSearchWorks() {

searchReq.addSort("long_sort", SortOrder.ASC);
ElasticsearchAssertions.assertNoFailures(searchReq.get());

searchReq = client().prepareSearch("test").setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), FilterBuilders.existsFilter("string")));
searchRsp = searchReq.get();
ElasticsearchAssertions.assertNoFailures(searchRsp);
assertThat(numDocs, equalTo(searchRsp.getHits().getTotalHits()));
}

void assertRealtimeGetWorks() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@

package org.elasticsearch.index.mapper.internal;

import org.elasticsearch.Version;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.DocumentMapperParser;
Expand Down Expand Up @@ -108,6 +112,14 @@ public void testDisabled() throws Exception {
assertNull(doc.rootDoc().get("_field_names"));
}

public void testPre13Disabled() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").endObject().endObject().string();
Settings indexSettings = ImmutableSettings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_2_4.id).build();
DocumentMapper docMapper = createIndex("test", indexSettings).mapperService().documentMapperParser().parse(mapping);
FieldNamesFieldMapper fieldNamesMapper = docMapper.rootMapper(FieldNamesFieldMapper.class);
assertFalse(fieldNamesMapper.enabled());
}

public void testDisablingBackcompat() throws Exception {
// before 1.5, disabling happened by setting index:no
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
Expand Down

0 comments on commit bd16117

Please sign in to comment.