diff --git a/core/src/main/java/org/elasticsearch/index/mapper/AllFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/AllFieldMapper.java index f3001db3926..0f88d3223ed 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/AllFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/AllFieldMapper.java @@ -133,6 +133,7 @@ public class AllFieldMapper extends MetadataFieldMapper { } parseTextField(builder, builder.name, node, parserContext); + boolean enabledSet = false; for (Iterator> iterator = node.entrySet().iterator(); iterator.hasNext();) { Map.Entry entry = iterator.next(); String fieldName = entry.getKey(); @@ -140,9 +141,16 @@ public class AllFieldMapper extends MetadataFieldMapper { if (fieldName.equals("enabled")) { boolean enabled = TypeParsers.nodeBooleanValueLenient(name, "enabled", fieldNode); builder.enabled(enabled ? EnabledAttributeMapper.ENABLED : EnabledAttributeMapper.DISABLED); + enabledSet = true; iterator.remove(); } } + if (enabledSet == false && parserContext.indexVersionCreated().before(Version.V_6_0_0_alpha1)) { + // So there is no "enabled" field, however, the index was created prior to 6.0, + // and therefore the default for this particular index should be "true" for + // enabling _all + builder.enabled(EnabledAttributeMapper.ENABLED); + } return builder; } @@ -150,7 +158,13 @@ public class AllFieldMapper extends MetadataFieldMapper { public MetadataFieldMapper getDefault(MappedFieldType fieldType, ParserContext context) { final Settings indexSettings = context.mapperService().getIndexSettings().getSettings(); if (fieldType != null) { - return new AllFieldMapper(indexSettings, fieldType); + if (context.indexVersionCreated().before(Version.V_6_0_0_alpha1)) { + // The index was created prior to 6.0, and therefore the default for this + // particular index should be "true" for enabling _all + return new AllFieldMapper(fieldType.clone(), EnabledAttributeMapper.ENABLED, indexSettings); + } else { + return new AllFieldMapper(indexSettings, fieldType); + } } else { return parse(NAME, Collections.emptyMap(), context) .build(new BuilderContext(indexSettings, new ContentPath(1))); @@ -197,7 +211,6 @@ public class AllFieldMapper extends MetadataFieldMapper { private AllFieldMapper(MappedFieldType fieldType, EnabledAttributeMapper enabled, Settings indexSettings) { super(NAME, fieldType, Defaults.FIELD_TYPE, indexSettings); this.enabledState = enabled; - } public boolean enabled() { diff --git a/core/src/test/java/org/elasticsearch/index/mapper/AllFieldIT.java b/core/src/test/java/org/elasticsearch/index/mapper/AllFieldIT.java new file mode 100644 index 00000000000..2be58b3b68e --- /dev/null +++ b/core/src/test/java/org/elasticsearch/index/mapper/AllFieldIT.java @@ -0,0 +1,109 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.index.mapper; + +import org.elasticsearch.Version; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.test.ESIntegTestCase; +import org.elasticsearch.test.InternalSettingsPlugin; + +import java.util.Arrays; +import java.util.Collection; + +import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits; + +public class AllFieldIT extends ESIntegTestCase { + + @Override + protected Collection> nodePlugins() { + return Arrays.asList(InternalSettingsPlugin.class); // uses index.version.created + } + + public void test5xIndicesContinueToUseAll() throws Exception { + // Default 5.x settings + assertAcked(prepareCreate("test").setSettings("index.version.created", Version.V_5_1_1.id)); + client().prepareIndex("test", "type", "1").setSource("body", "foo").get(); + refresh(); + SearchResponse resp = client().prepareSearch("test").setQuery(QueryBuilders.matchQuery("_all", "foo")).get(); + assertHitCount(resp, 1); + assertSearchHits(resp, "1"); + + // _all explicitly enabled + assertAcked(prepareCreate("test2") + .setSource(jsonBuilder() + .startObject() + .startObject("mappings") + .startObject("type") + .startObject("_all") + .field("enabled", true) + .endObject() // _all + .endObject() // type + .endObject() // mappings + .endObject()) + .setSettings("index.version.created", Version.V_5_4_0_ID)); + client().prepareIndex("test2", "type", "1").setSource("foo", "bar").get(); + refresh(); + resp = client().prepareSearch("test2").setQuery(QueryBuilders.matchQuery("_all", "bar")).get(); + assertHitCount(resp, 1); + assertSearchHits(resp, "1"); + + // _all explicitly disabled + assertAcked(prepareCreate("test3") + .setSource(jsonBuilder() + .startObject() + .startObject("mappings") + .startObject("type") + .startObject("_all") + .field("enabled", false) + .endObject() // _all + .endObject() // type + .endObject() // mappings + .endObject()) + .setSettings("index.version.created", Version.V_5_4_0_ID)); + client().prepareIndex("test3", "type", "1").setSource("foo", "baz").get(); + refresh(); + resp = client().prepareSearch("test3").setQuery(QueryBuilders.matchQuery("_all", "baz")).get(); + assertHitCount(resp, 0); + + // _all present, but not enabled or disabled (default settings) + assertAcked(prepareCreate("test4") + .setSource(jsonBuilder() + .startObject() + .startObject("mappings") + .startObject("type") + .startObject("_all") + .endObject() // _all + .endObject() // type + .endObject() // mappings + .endObject()) + .setSettings("index.version.created", Version.V_5_4_0_ID)); + client().prepareIndex("test4", "type", "1").setSource("foo", "eggplant").get(); + refresh(); + resp = client().prepareSearch("test4").setQuery(QueryBuilders.matchQuery("_all", "eggplant")).get(); + assertHitCount(resp, 1); + assertSearchHits(resp, "1"); + } + +} diff --git a/core/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java index dd0a429f02e..ca3850c4118 100644 --- a/core/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java @@ -46,6 +46,7 @@ import org.apache.lucene.search.spans.SpanOrQuery; import org.apache.lucene.search.spans.SpanTermQuery; import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.automaton.TooComplexToDeterminizeException; +import org.elasticsearch.Version; import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.compress.CompressedXContent; @@ -833,6 +834,9 @@ public class QueryStringQueryBuilderTests extends AbstractQueryTestCase 0); + assumeTrue("5.x behaves differently, so skip on non-6.x indices", + indexVersionCreated.onOrAfter(Version.V_6_0_0_alpha1)); + QueryShardContext context = createShardContext(); QueryStringQueryBuilder queryBuilder = new QueryStringQueryBuilder("foo:*"); Query query = queryBuilder.toQuery(context); diff --git a/core/src/test/java/org/elasticsearch/index/query/SimpleQueryStringBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/SimpleQueryStringBuilderTests.java index 4c087671c96..f13e01e52ae 100644 --- a/core/src/test/java/org/elasticsearch/index/query/SimpleQueryStringBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/SimpleQueryStringBuilderTests.java @@ -31,6 +31,7 @@ import org.apache.lucene.search.PrefixQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.TermQuery; import org.apache.lucene.util.TestUtil; +import org.elasticsearch.Version; import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.common.ParsingException; import org.elasticsearch.index.mapper.MapperService; @@ -198,6 +199,9 @@ public class SimpleQueryStringBuilderTests extends AbstractQueryTestCase> DOUBLE_FIELD_NAME, BOOLEAN_FIELD_NAME, DATE_FIELD_NAME, DATE_RANGE_FIELD_NAME, GEO_POINT_FIELD_NAME, }; private static final int NUMBER_OF_TESTQUERIES = 20; + protected static Version indexVersionCreated; + private static ServiceHolder serviceHolder; private static int queryNameId = 0; private static Settings nodeSettings; @@ -185,8 +187,8 @@ public abstract class AbstractQueryTestCase> protected Settings indexSettings() { // we have to prefer CURRENT since with the range of versions we support it's rather unlikely to get the current actually. - Version indexVersionCreated = randomBoolean() ? Version.CURRENT - : VersionUtils.randomVersionBetween(random(), null, Version.CURRENT); + indexVersionCreated = randomBoolean() ? Version.CURRENT + : VersionUtils.randomVersionBetween(random(), null, Version.CURRENT); return Settings.builder() .put(IndexMetaData.SETTING_VERSION_CREATED, indexVersionCreated) .build();