diff --git a/core/src/main/java/org/elasticsearch/index/analysis/AnalysisService.java b/core/src/main/java/org/elasticsearch/index/analysis/AnalysisService.java index c76446c6676..ba8b1e083af 100644 --- a/core/src/main/java/org/elasticsearch/index/analysis/AnalysisService.java +++ b/core/src/main/java/org/elasticsearch/index/analysis/AnalysisService.java @@ -48,7 +48,6 @@ public class AnalysisService extends AbstractIndexComponent implements Closeable private final Map charFilters; private final Map tokenFilters; - private final NamedAnalyzer defaultAnalyzer; private final NamedAnalyzer defaultIndexAnalyzer; private final NamedAnalyzer defaultSearchAnalyzer; private final NamedAnalyzer defaultSearchQuoteAnalyzer; @@ -263,12 +262,20 @@ public class AnalysisService extends AbstractIndexComponent implements Closeable } } - defaultAnalyzer = analyzers.get("default"); + NamedAnalyzer defaultAnalyzer = analyzers.get("default"); if (defaultAnalyzer == null) { throw new IllegalArgumentException("no default analyzer configured"); } - defaultIndexAnalyzer = analyzers.containsKey("default_index") ? analyzers.get("default_index") : analyzers.get("default"); - defaultSearchAnalyzer = analyzers.containsKey("default_search") ? analyzers.get("default_search") : analyzers.get("default"); + if (analyzers.containsKey("default_index")) { + final Version createdVersion = Version.indexCreated(indexSettings); + if (createdVersion.onOrAfter(Version.V_3_0_0)) { + throw new IllegalArgumentException("setting [index.analysis.analyzer.default_index] is not supported anymore, use [index.analysis.analyzer.default] instead for index [" + index.getName() + "]"); + } else { + deprecationLogger.deprecated("setting [index.analysis.analyzer.default_index] is deprecated, use [index.analysis.analyzer.default] instead for index [{}]", index.getName()); + } + } + defaultIndexAnalyzer = analyzers.containsKey("default_index") ? analyzers.get("default_index") : defaultAnalyzer; + defaultSearchAnalyzer = analyzers.containsKey("default_search") ? analyzers.get("default_search") : defaultAnalyzer; defaultSearchQuoteAnalyzer = analyzers.containsKey("default_search_quote") ? analyzers.get("default_search_quote") : defaultSearchAnalyzer; for (Map.Entry analyzer : analyzers.entrySet()) { @@ -299,10 +306,6 @@ public class AnalysisService extends AbstractIndexComponent implements Closeable return analyzers.get(name); } - public NamedAnalyzer defaultAnalyzer() { - return defaultAnalyzer; - } - public NamedAnalyzer defaultIndexAnalyzer() { return defaultIndexAnalyzer; } diff --git a/core/src/test/java/org/elasticsearch/index/analysis/AnalysisServiceTests.java b/core/src/test/java/org/elasticsearch/index/analysis/AnalysisServiceTests.java new file mode 100644 index 00000000000..cd3a8c643ca --- /dev/null +++ b/core/src/test/java/org/elasticsearch/index/analysis/AnalysisServiceTests.java @@ -0,0 +1,124 @@ +/* + * 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.analysis; + +import org.apache.lucene.analysis.en.EnglishAnalyzer; +import org.apache.lucene.analysis.standard.StandardAnalyzer; +import org.elasticsearch.Version; +import org.elasticsearch.cluster.metadata.IndexMetaData; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.index.Index; +import org.elasticsearch.indices.analysis.IndicesAnalysisService; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.VersionUtils; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import static org.hamcrest.Matchers.instanceOf; + +public class AnalysisServiceTests extends ESTestCase { + + private static AnalyzerProviderFactory analyzerProvider(final String name) { + return new AnalyzerProviderFactory() { + @Override + public AnalyzerProvider create(String name, Settings settings) { + return new PreBuiltAnalyzerProvider(name, AnalyzerScope.INDEX, new EnglishAnalyzer()); + } + }; + } + + public void testDefaultAnalyzers() { + Version version = VersionUtils.randomVersion(getRandom()); + Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build(); + IndicesAnalysisService indicesAnalysisService = new IndicesAnalysisService(settings); + AnalysisService analysisService = new AnalysisService(new Index("index"), settings, indicesAnalysisService, + Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap()); + assertThat(analysisService.defaultIndexAnalyzer().analyzer(), instanceOf(StandardAnalyzer.class)); + assertThat(analysisService.defaultSearchAnalyzer().analyzer(), instanceOf(StandardAnalyzer.class)); + assertThat(analysisService.defaultSearchQuoteAnalyzer().analyzer(), instanceOf(StandardAnalyzer.class)); + } + + public void testOverrideDefaultAnalyzer() { + Version version = VersionUtils.randomVersion(getRandom()); + Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build(); + IndicesAnalysisService indicesAnalysisService = new IndicesAnalysisService(settings); + AnalysisService analysisService = new AnalysisService(new Index("index"), settings, indicesAnalysisService, + Collections.singletonMap("default", analyzerProvider("default")), + Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap()); + assertThat(analysisService.defaultIndexAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class)); + assertThat(analysisService.defaultSearchAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class)); + assertThat(analysisService.defaultSearchQuoteAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class)); + } + + public void testOverrideDefaultIndexAnalyzer() { + Version version = VersionUtils.randomVersionBetween(getRandom(), Version.V_3_0_0, Version.CURRENT); + Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build(); + IndicesAnalysisService indicesAnalysisService = new IndicesAnalysisService(settings); + try { + AnalysisService analysisService = new AnalysisService(new Index("index"), settings, indicesAnalysisService, + Collections.singletonMap("default_index", new PreBuiltAnalyzerProviderFactory("default_index", AnalyzerScope.INDEX, new EnglishAnalyzer())), + Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap()); + fail("Expected ISE"); + } catch (IllegalArgumentException e) { + // expected + assertTrue(e.getMessage().contains("[index.analysis.analyzer.default_index] is not supported")); + } + } + + public void testBackCompatOverrideDefaultIndexAnalyzer() { + Version version = VersionUtils.randomVersionBetween(getRandom(), VersionUtils.getFirstVersion(), VersionUtils.getPreviousVersion(Version.V_3_0_0)); + Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build(); + IndicesAnalysisService indicesAnalysisService = new IndicesAnalysisService(settings); + AnalysisService analysisService = new AnalysisService(new Index("index"), settings, indicesAnalysisService, + Collections.singletonMap("default_index", analyzerProvider("default_index")), + Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap()); + assertThat(analysisService.defaultIndexAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class)); + assertThat(analysisService.defaultSearchAnalyzer().analyzer(), instanceOf(StandardAnalyzer.class)); + assertThat(analysisService.defaultSearchQuoteAnalyzer().analyzer(), instanceOf(StandardAnalyzer.class)); + } + + public void testOverrideDefaultSearchAnalyzer() { + Version version = VersionUtils.randomVersion(getRandom()); + Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build(); + IndicesAnalysisService indicesAnalysisService = new IndicesAnalysisService(settings); + AnalysisService analysisService = new AnalysisService(new Index("index"), settings, indicesAnalysisService, + Collections.singletonMap("default_search", analyzerProvider("default_search")), + Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap()); + assertThat(analysisService.defaultIndexAnalyzer().analyzer(), instanceOf(StandardAnalyzer.class)); + assertThat(analysisService.defaultSearchAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class)); + assertThat(analysisService.defaultSearchQuoteAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class)); + } + + public void testBackCompatOverrideDefaultIndexAndSearchAnalyzer() { + Version version = VersionUtils.randomVersionBetween(getRandom(), VersionUtils.getFirstVersion(), VersionUtils.getPreviousVersion(Version.V_3_0_0)); + Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build(); + IndicesAnalysisService indicesAnalysisService = new IndicesAnalysisService(settings); + Map analyzers = new HashMap<>(); + analyzers.put("default_index", analyzerProvider("default_index")); + analyzers.put("default_search", analyzerProvider("default_search")); + AnalysisService analysisService = new AnalysisService(new Index("index"), settings, indicesAnalysisService, + analyzers, Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap()); + assertThat(analysisService.defaultIndexAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class)); + assertThat(analysisService.defaultSearchAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class)); + assertThat(analysisService.defaultSearchQuoteAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class)); + } +} diff --git a/core/src/test/java/org/elasticsearch/index/query/AbstractQueryTestCase.java b/core/src/test/java/org/elasticsearch/index/query/AbstractQueryTestCase.java index fce0a9478b8..f8a66730853 100644 --- a/core/src/test/java/org/elasticsearch/index/query/AbstractQueryTestCase.java +++ b/core/src/test/java/org/elasticsearch/index/query/AbstractQueryTestCase.java @@ -20,6 +20,7 @@ package org.elasticsearch.index.query; import com.carrotsearch.randomizedtesting.generators.CodepointSetGenerator; +import com.fasterxml.jackson.core.io.JsonStringEncoder; import org.apache.lucene.search.Query; import org.elasticsearch.Version; @@ -552,7 +553,13 @@ public abstract class AbstractQueryTestCase> Object value; switch (fieldName) { case STRING_FIELD_NAME: - value = rarely() ? randomUnicodeOfLength(10) : randomAsciiOfLengthBetween(1, 10); // unicode in 10% cases + if (rarely()) { + // unicode in 10% cases + JsonStringEncoder encoder = JsonStringEncoder.getInstance(); + value = new String(encoder.quoteAsString(randomUnicodeOfLength(10))); + } else { + value = randomAsciiOfLengthBetween(1, 10); + } break; case INT_FIELD_NAME: value = randomIntBetween(0, 10); diff --git a/core/src/test/java/org/elasticsearch/index/query/AbstractTermQueryTestCase.java b/core/src/test/java/org/elasticsearch/index/query/AbstractTermQueryTestCase.java index ccfe6190011..adab170127e 100644 --- a/core/src/test/java/org/elasticsearch/index/query/AbstractTermQueryTestCase.java +++ b/core/src/test/java/org/elasticsearch/index/query/AbstractTermQueryTestCase.java @@ -19,6 +19,8 @@ package org.elasticsearch.index.query; +import com.fasterxml.jackson.core.io.JsonStringEncoder; + import org.junit.Test; import java.util.HashMap; @@ -45,7 +47,8 @@ public abstract class AbstractTermQueryTestCase