diff --git a/core/src/main/java/org/elasticsearch/action/admin/indices/analyze/TransportAnalyzeAction.java b/core/src/main/java/org/elasticsearch/action/admin/indices/analyze/TransportAnalyzeAction.java index d7e299b1cf1b5..1156637808578 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/indices/analyze/TransportAnalyzeAction.java +++ b/core/src/main/java/org/elasticsearch/action/admin/indices/analyze/TransportAnalyzeAction.java @@ -39,6 +39,7 @@ import org.elasticsearch.cluster.routing.ShardsIterator; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.UUIDs; +import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.io.FastStringReader; import org.elasticsearch.common.settings.Settings; @@ -179,7 +180,8 @@ public static AnalyzeResponse analyze(AnalyzeRequest request, String field, Anal } else if (request.tokenizer() != null) { final IndexSettings indexSettings = indexAnalyzers == null ? null : indexAnalyzers.getIndexSettings(); - TokenizerFactory tokenizerFactory = parseTokenizerFactory(request, indexAnalyzers, analysisRegistry, environment); + Tuple tokenizerFactory = parseTokenizerFactory(request, indexAnalyzers, + analysisRegistry, environment); TokenFilterFactory[] tokenFilterFactories = new TokenFilterFactory[0]; tokenFilterFactories = getTokenFilterFactories(request, indexSettings, analysisRegistry, environment, tokenFilterFactories); @@ -187,7 +189,7 @@ public static AnalyzeResponse analyze(AnalyzeRequest request, String field, Anal CharFilterFactory[] charFilterFactories = new CharFilterFactory[0]; charFilterFactories = getCharFilterFactories(request, indexSettings, analysisRegistry, environment, charFilterFactories); - analyzer = new CustomAnalyzer(tokenizerFactory, charFilterFactories, tokenFilterFactories); + analyzer = new CustomAnalyzer(tokenizerFactory.v1(), tokenizerFactory.v2(), charFilterFactories, tokenFilterFactories); closeAnalyzer = true; } else if (analyzer == null) { if (indexAnalyzers == null) { @@ -325,7 +327,8 @@ private static DetailAnalyzeResponse detailAnalyze(AnalyzeRequest request, Analy tokenFilterFactories[tokenFilterIndex].name(), tokenFiltersTokenListCreator[tokenFilterIndex].getArrayTokens()); } } - detailResponse = new DetailAnalyzeResponse(charFilteredLists, new DetailAnalyzeResponse.AnalyzeTokenList(tokenizerFactory.name(), tokenizerTokenListCreator.getArrayTokens()), tokenFilterLists); + detailResponse = new DetailAnalyzeResponse(charFilteredLists, new DetailAnalyzeResponse.AnalyzeTokenList( + customAnalyzer.getTokenizerName(), tokenizerTokenListCreator.getArrayTokens()), tokenFilterLists); } else { String name; if (analyzer instanceof NamedAnalyzer) { @@ -551,8 +554,9 @@ private static TokenFilterFactory[] getTokenFilterFactories(AnalyzeRequest reque return tokenFilterFactories; } - private static TokenizerFactory parseTokenizerFactory(AnalyzeRequest request, IndexAnalyzers indexAnalzyers, + private static Tuple parseTokenizerFactory(AnalyzeRequest request, IndexAnalyzers indexAnalzyers, AnalysisRegistry analysisRegistry, Environment environment) throws IOException { + String name; TokenizerFactory tokenizerFactory; final AnalyzeRequest.NameOrDefinition tokenizer = request.tokenizer(); // parse anonymous settings @@ -568,6 +572,7 @@ private static TokenizerFactory parseTokenizerFactory(AnalyzeRequest request, In throw new IllegalArgumentException("failed to find global tokenizer under [" + tokenizerTypeName + "]"); } // Need to set anonymous "name" of tokenizer + name = "_anonymous_tokenizer"; tokenizerFactory = tokenizerFactoryFactory.get(getNaIndexSettings(settings), environment, "_anonymous_tokenizer", settings); } else { AnalysisModule.AnalysisProvider tokenizerFactoryFactory; @@ -576,18 +581,20 @@ private static TokenizerFactory parseTokenizerFactory(AnalyzeRequest request, In if (tokenizerFactoryFactory == null) { throw new IllegalArgumentException("failed to find global tokenizer under [" + tokenizer.name + "]"); } + name = tokenizer.name; tokenizerFactory = tokenizerFactoryFactory.get(environment, tokenizer.name); } else { tokenizerFactoryFactory = analysisRegistry.getTokenizerProvider(tokenizer.name, indexAnalzyers.getIndexSettings()); if (tokenizerFactoryFactory == null) { throw new IllegalArgumentException("failed to find tokenizer under [" + tokenizer.name + "]"); } + name = tokenizer.name; tokenizerFactory = tokenizerFactoryFactory.get(indexAnalzyers.getIndexSettings(), environment, tokenizer.name, AnalysisRegistry.getSettingsFromIndexSettings(indexAnalzyers.getIndexSettings(), AnalysisRegistry.INDEX_ANALYSIS_TOKENIZER + "." + tokenizer.name)); } } - return tokenizerFactory; + return new Tuple<>(name, tokenizerFactory); } private static IndexSettings getNaIndexSettings(Settings settings) { diff --git a/core/src/main/java/org/elasticsearch/index/analysis/AbstractTokenizerFactory.java b/core/src/main/java/org/elasticsearch/index/analysis/AbstractTokenizerFactory.java index dfa177a7fbf08..bf6b2fd7c5b47 100644 --- a/core/src/main/java/org/elasticsearch/index/analysis/AbstractTokenizerFactory.java +++ b/core/src/main/java/org/elasticsearch/index/analysis/AbstractTokenizerFactory.java @@ -25,23 +25,14 @@ import org.elasticsearch.index.IndexSettings; public abstract class AbstractTokenizerFactory extends AbstractIndexComponent implements TokenizerFactory { - - private final String name; - protected final Version version; - - public AbstractTokenizerFactory(IndexSettings indexSettings, String name, Settings settings) { + // TODO drop `String ignored` in a followup + public AbstractTokenizerFactory(IndexSettings indexSettings, String ignored, Settings settings) { super(indexSettings); - this.name = name; this.version = Analysis.parseAnalysisVersion(this.indexSettings.getSettings(), settings, logger); } - @Override - public String name() { - return this.name; - } - public final Version version() { return version; } diff --git a/core/src/main/java/org/elasticsearch/index/analysis/CustomAnalyzer.java b/core/src/main/java/org/elasticsearch/index/analysis/CustomAnalyzer.java index 6879941390701..d70b4628f532c 100644 --- a/core/src/main/java/org/elasticsearch/index/analysis/CustomAnalyzer.java +++ b/core/src/main/java/org/elasticsearch/index/analysis/CustomAnalyzer.java @@ -27,6 +27,7 @@ public final class CustomAnalyzer extends Analyzer { + private final String tokenizerName; private final TokenizerFactory tokenizerFactory; private final CharFilterFactory[] charFilters; @@ -36,12 +37,14 @@ public final class CustomAnalyzer extends Analyzer { private final int positionIncrementGap; private final int offsetGap; - public CustomAnalyzer(TokenizerFactory tokenizerFactory, CharFilterFactory[] charFilters, TokenFilterFactory[] tokenFilters) { - this(tokenizerFactory, charFilters, tokenFilters, 0, -1); + public CustomAnalyzer(String tokenizerName, TokenizerFactory tokenizerFactory, CharFilterFactory[] charFilters, + TokenFilterFactory[] tokenFilters) { + this(tokenizerName, tokenizerFactory, charFilters, tokenFilters, 0, -1); } - public CustomAnalyzer(TokenizerFactory tokenizerFactory, CharFilterFactory[] charFilters, TokenFilterFactory[] tokenFilters, - int positionIncrementGap, int offsetGap) { + public CustomAnalyzer(String tokenizerName, TokenizerFactory tokenizerFactory, CharFilterFactory[] charFilters, + TokenFilterFactory[] tokenFilters, int positionIncrementGap, int offsetGap) { + this.tokenizerName = tokenizerName; this.tokenizerFactory = tokenizerFactory; this.charFilters = charFilters; this.tokenFilters = tokenFilters; @@ -49,6 +52,12 @@ public CustomAnalyzer(TokenizerFactory tokenizerFactory, CharFilterFactory[] cha this.offsetGap = offsetGap; } + /** + * The name of the tokenizer as configured by the user. + */ + public String getTokenizerName() { + return tokenizerName; + } public TokenizerFactory tokenizerFactory() { return tokenizerFactory; diff --git a/core/src/main/java/org/elasticsearch/index/analysis/CustomAnalyzerProvider.java b/core/src/main/java/org/elasticsearch/index/analysis/CustomAnalyzerProvider.java index f112304562283..3bf5d43375c90 100644 --- a/core/src/main/java/org/elasticsearch/index/analysis/CustomAnalyzerProvider.java +++ b/core/src/main/java/org/elasticsearch/index/analysis/CustomAnalyzerProvider.java @@ -80,7 +80,7 @@ public void build(final Map tokenizers, final Map charFilters, final Map create.apply(version); } } } diff --git a/core/src/main/java/org/elasticsearch/index/analysis/TokenizerFactory.java b/core/src/main/java/org/elasticsearch/index/analysis/TokenizerFactory.java index 6ca9d457cbc01..be96dbd65602b 100644 --- a/core/src/main/java/org/elasticsearch/index/analysis/TokenizerFactory.java +++ b/core/src/main/java/org/elasticsearch/index/analysis/TokenizerFactory.java @@ -21,9 +21,6 @@ import org.apache.lucene.analysis.Tokenizer; -public interface TokenizerFactory { - - String name(); - +public interface TokenizerFactory { // TODO replace with Supplier Tokenizer create(); } diff --git a/core/src/main/java/org/elasticsearch/indices/analysis/PreBuiltTokenizers.java b/core/src/main/java/org/elasticsearch/indices/analysis/PreBuiltTokenizers.java index 52e7ff6c9c4fa..9cc9ed1ea23b6 100644 --- a/core/src/main/java/org/elasticsearch/indices/analysis/PreBuiltTokenizers.java +++ b/core/src/main/java/org/elasticsearch/indices/analysis/PreBuiltTokenizers.java @@ -38,8 +38,6 @@ import org.elasticsearch.index.analysis.TokenizerFactory; import org.elasticsearch.indices.analysis.PreBuiltCacheFactory.CachingStrategy; -import java.util.Locale; - public enum PreBuiltTokenizers { STANDARD(CachingStrategy.ONE) { @@ -148,14 +146,8 @@ private interface MultiTermAwareTokenizerFactory extends TokenizerFactory, Multi public synchronized TokenizerFactory getTokenizerFactory(final Version version) { TokenizerFactory tokenizerFactory = cache.get(version); if (tokenizerFactory == null) { - final String finalName = name().toLowerCase(Locale.ROOT); if (getMultiTermComponent(version) != null) { tokenizerFactory = new MultiTermAwareTokenizerFactory() { - @Override - public String name() { - return finalName; - } - @Override public Tokenizer create() { return PreBuiltTokenizers.this.create(version); @@ -168,11 +160,6 @@ public Object getMultiTermComponent() { }; } else { tokenizerFactory = new TokenizerFactory() { - @Override - public String name() { - return finalName; - } - @Override public Tokenizer create() { return PreBuiltTokenizers.this.create(version); diff --git a/core/src/test/java/org/elasticsearch/indices/analysis/DummyAnalysisPlugin.java b/core/src/test/java/org/elasticsearch/indices/analysis/DummyAnalysisPlugin.java deleted file mode 100644 index bbfeacfc5905c..0000000000000 --- a/core/src/test/java/org/elasticsearch/indices/analysis/DummyAnalysisPlugin.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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.indices.analysis; - -import org.apache.lucene.analysis.Analyzer; -import org.elasticsearch.index.analysis.AnalyzerProvider; -import org.elasticsearch.index.analysis.CharFilterFactory; -import org.elasticsearch.index.analysis.TokenFilterFactory; -import org.elasticsearch.index.analysis.TokenizerFactory; -import org.elasticsearch.indices.analysis.AnalysisModule.AnalysisProvider; -import org.elasticsearch.plugins.AnalysisPlugin; -import org.elasticsearch.plugins.Plugin; - -import java.util.Map; - -import static java.util.Collections.singletonMap; - -public class DummyAnalysisPlugin extends Plugin implements AnalysisPlugin { - @Override - public Map> getCharFilters() { - return singletonMap("dummy_char_filter", (a, b, c, d) -> new DummyCharFilterFactory()); - } - - @Override - public Map> getTokenFilters() { - return singletonMap("dummy_token_filter", (a, b, c, d) -> new DummyTokenFilterFactory()); - } - - @Override - public Map> getTokenizers() { - return singletonMap("dummy_tokenizer", (a, b, c, d) -> new DummyTokenizerFactory()); - } - - @Override - public Map>> getAnalyzers() { - return singletonMap("dummy", (a, b, c, d) -> new DummyAnalyzerProvider()); - } - -} diff --git a/core/src/test/java/org/elasticsearch/indices/analysis/DummyAnalyzer.java b/core/src/test/java/org/elasticsearch/indices/analysis/DummyAnalyzer.java deleted file mode 100644 index 61b5d2eb319f3..0000000000000 --- a/core/src/test/java/org/elasticsearch/indices/analysis/DummyAnalyzer.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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.indices.analysis; - -import org.apache.lucene.analysis.StopwordAnalyzerBase; - -public class DummyAnalyzer extends StopwordAnalyzerBase { - - protected DummyAnalyzer() { - } - - @Override - protected TokenStreamComponents createComponents(String fieldName) { - return null; - } -} diff --git a/core/src/test/java/org/elasticsearch/indices/analysis/DummyAnalyzerProvider.java b/core/src/test/java/org/elasticsearch/indices/analysis/DummyAnalyzerProvider.java deleted file mode 100644 index 68beb817d700b..0000000000000 --- a/core/src/test/java/org/elasticsearch/indices/analysis/DummyAnalyzerProvider.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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.indices.analysis; - -import org.elasticsearch.index.analysis.AnalyzerProvider; -import org.elasticsearch.index.analysis.AnalyzerScope; - -public class DummyAnalyzerProvider implements AnalyzerProvider { - @Override - public String name() { - return "dummy"; - } - - @Override - public AnalyzerScope scope() { - return AnalyzerScope.INDICES; - } - - @Override - public DummyAnalyzer get() { - return new DummyAnalyzer(); - } -} diff --git a/core/src/test/java/org/elasticsearch/indices/analysis/DummyCharFilterFactory.java b/core/src/test/java/org/elasticsearch/indices/analysis/DummyCharFilterFactory.java deleted file mode 100644 index 8c5896e59ecbe..0000000000000 --- a/core/src/test/java/org/elasticsearch/indices/analysis/DummyCharFilterFactory.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * 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.indices.analysis; - -import org.elasticsearch.index.analysis.CharFilterFactory; - -import java.io.Reader; - -public class DummyCharFilterFactory implements CharFilterFactory { - @Override - public String name() { - return "dummy_char_filter"; - } - - @Override - public Reader create(Reader reader) { - return null; - } -} diff --git a/core/src/test/java/org/elasticsearch/indices/analysis/DummyTokenFilterFactory.java b/core/src/test/java/org/elasticsearch/indices/analysis/DummyTokenFilterFactory.java deleted file mode 100644 index 489e4dce7b850..0000000000000 --- a/core/src/test/java/org/elasticsearch/indices/analysis/DummyTokenFilterFactory.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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.indices.analysis; - -import org.apache.lucene.analysis.TokenStream; -import org.elasticsearch.index.analysis.TokenFilterFactory; - -public class DummyTokenFilterFactory implements TokenFilterFactory { - @Override public String name() { - return "dummy_token_filter"; - } - - @Override public TokenStream create(TokenStream tokenStream) { - return null; - } -} diff --git a/core/src/test/java/org/elasticsearch/indices/analysis/DummyTokenizerFactory.java b/core/src/test/java/org/elasticsearch/indices/analysis/DummyTokenizerFactory.java deleted file mode 100644 index a27c6ae7dba26..0000000000000 --- a/core/src/test/java/org/elasticsearch/indices/analysis/DummyTokenizerFactory.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * 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.indices.analysis; - -import org.apache.lucene.analysis.Tokenizer; -import org.elasticsearch.index.analysis.TokenizerFactory; - -public class DummyTokenizerFactory implements TokenizerFactory { - @Override - public String name() { - return "dummy_tokenizer"; - } - - @Override - public Tokenizer create() { - return null; - } -} diff --git a/core/src/test/java/org/elasticsearch/indices/analysis/PreBuiltAnalyzerIntegrationIT.java b/core/src/test/java/org/elasticsearch/indices/analysis/PreBuiltAnalyzerIntegrationIT.java index d6e93ce559e11..7722795525df5 100644 --- a/core/src/test/java/org/elasticsearch/indices/analysis/PreBuiltAnalyzerIntegrationIT.java +++ b/core/src/test/java/org/elasticsearch/indices/analysis/PreBuiltAnalyzerIntegrationIT.java @@ -46,7 +46,7 @@ public class PreBuiltAnalyzerIntegrationIT extends ESIntegTestCase { @Override protected Collection> nodePlugins() { - return Arrays.asList(DummyAnalysisPlugin.class, InternalSettingsPlugin.class); + return Arrays.asList(InternalSettingsPlugin.class); } public void testThatPreBuiltAnalyzersAreNotClosedOnIndexClose() throws Exception { @@ -114,41 +114,6 @@ public void testThatPreBuiltAnalyzersAreNotClosedOnIndexClose() throws Exception assertLuceneAnalyzersAreNotClosed(loadedAnalyzers); } - /** - * Test case for #5030: Upgrading analysis plugins fails - * See https://github.com/elastic/elasticsearch/issues/5030 - */ - public void testThatPluginAnalyzersCanBeUpdated() throws Exception { - final XContentBuilder mapping = jsonBuilder().startObject() - .startObject("type") - .startObject("properties") - .startObject("foo") - .field("type", "text") - .field("analyzer", "dummy") - .endObject() - .startObject("bar") - .field("type", "text") - .field("analyzer", "my_dummy") - .endObject() - .endObject() - .endObject() - .endObject(); - - Settings versionSettings = settings(randomVersion(random())) - .put("index.analysis.analyzer.my_dummy.type", "custom") - .put("index.analysis.analyzer.my_dummy.filter", "my_dummy_token_filter") - .put("index.analysis.analyzer.my_dummy.char_filter", "my_dummy_char_filter") - .put("index.analysis.analyzer.my_dummy.tokenizer", "my_dummy_tokenizer") - .put("index.analysis.tokenizer.my_dummy_tokenizer.type", "dummy_tokenizer") - .put("index.analysis.filter.my_dummy_token_filter.type", "dummy_token_filter") - .put("index.analysis.char_filter.my_dummy_char_filter.type", "dummy_char_filter") - .build(); - - client().admin().indices().prepareCreate("test-analysis-dummy").addMapping("type", mapping).setSettings(versionSettings).get(); - - ensureGreen(); - } - private void assertThatAnalyzersHaveBeenLoaded(Map> expectedLoadedAnalyzers) { for (Map.Entry> entry : expectedLoadedAnalyzers.entrySet()) { for (Version version : entry.getValue()) {