From 63bcb4f534e95725923886ec1069e6af03509416 Mon Sep 17 00:00:00 2001 From: Igor Motov Date: Sat, 27 Aug 2016 21:02:16 -0400 Subject: [PATCH] Named analyzer should close the analyzer that it wraps --- .../metadata/MetaDataIndexUpgradeService.java | 5 +++-- .../index/analysis/NamedAnalyzer.java | 12 ++++++++---- .../index/mapper/CompletionFieldMapper.java | 5 +++-- .../index/engine/InternalEngineTests.java | 3 ++- .../mapper/DocumentFieldMapperTests.java | 19 ++++++++++--------- .../index/mapper/FieldTypeTestCase.java | 19 ++++++++++--------- .../index/mapper/ParentFieldMapperTests.java | 3 ++- 7 files changed, 38 insertions(+), 28 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataIndexUpgradeService.java b/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataIndexUpgradeService.java index d9faa520682..27d28417b75 100644 --- a/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataIndexUpgradeService.java +++ b/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataIndexUpgradeService.java @@ -27,6 +27,7 @@ import org.elasticsearch.common.settings.IndexScopedSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.analysis.IndexAnalyzers; +import org.elasticsearch.index.analysis.AnalyzerScope; import org.elasticsearch.index.analysis.NamedAnalyzer; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.similarity.SimilarityService; @@ -115,7 +116,7 @@ public class MetaDataIndexUpgradeService extends AbstractComponent { // been started yet. However, we don't really need real analyzers at this stage - so we can fake it IndexSettings indexSettings = new IndexSettings(indexMetaData, this.settings); SimilarityService similarityService = new SimilarityService(indexSettings, Collections.emptyMap()); - final NamedAnalyzer fakeDefault = new NamedAnalyzer("fake_default", new Analyzer() { + final NamedAnalyzer fakeDefault = new NamedAnalyzer("fake_default", AnalyzerScope.INDEX, new Analyzer() { @Override protected TokenStreamComponents createComponents(String fieldName) { throw new UnsupportedOperationException("shouldn't be here"); @@ -128,7 +129,7 @@ public class MetaDataIndexUpgradeService extends AbstractComponent { @Override public NamedAnalyzer get(Object key) { assert key instanceof String : "key must be a string but was: " + key.getClass(); - return new NamedAnalyzer((String)key, fakeDefault.analyzer()); + return new NamedAnalyzer((String)key, AnalyzerScope.INDEX, fakeDefault.analyzer()); } @Override diff --git a/core/src/main/java/org/elasticsearch/index/analysis/NamedAnalyzer.java b/core/src/main/java/org/elasticsearch/index/analysis/NamedAnalyzer.java index 1dd562c4bb1..416967e94f5 100644 --- a/core/src/main/java/org/elasticsearch/index/analysis/NamedAnalyzer.java +++ b/core/src/main/java/org/elasticsearch/index/analysis/NamedAnalyzer.java @@ -39,10 +39,6 @@ public class NamedAnalyzer extends DelegatingAnalyzerWrapper { this(analyzer.name(), analyzer.scope(), analyzer.analyzer(), positionIncrementGap); } - public NamedAnalyzer(String name, Analyzer analyzer) { - this(name, AnalyzerScope.INDEX, analyzer); - } - public NamedAnalyzer(String name, AnalyzerScope scope, Analyzer analyzer) { this(name, scope, analyzer, Integer.MIN_VALUE); } @@ -119,4 +115,12 @@ public class NamedAnalyzer extends DelegatingAnalyzerWrapper { public int hashCode() { return Objects.hash(name); } + + @Override + public void close() { + super.close(); + if (scope == AnalyzerScope.INDEX) { + analyzer.close(); + } + } } diff --git a/core/src/main/java/org/elasticsearch/index/mapper/CompletionFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/CompletionFieldMapper.java index 09035bfa3ce..5a4edd39ac1 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/CompletionFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/CompletionFieldMapper.java @@ -38,6 +38,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser.NumberType; import org.elasticsearch.common.xcontent.XContentParser.Token; +import org.elasticsearch.index.analysis.AnalyzerScope; import org.elasticsearch.index.analysis.NamedAnalyzer; import org.elasticsearch.search.suggest.completion.CompletionSuggester; import org.elasticsearch.search.suggest.completion.context.ContextMapping; @@ -209,7 +210,7 @@ public class CompletionFieldMapper extends FieldMapper implements ArrayValueMapp public NamedAnalyzer indexAnalyzer() { final NamedAnalyzer indexAnalyzer = super.indexAnalyzer(); if (indexAnalyzer != null && !(indexAnalyzer.analyzer() instanceof CompletionAnalyzer)) { - return new NamedAnalyzer(indexAnalyzer.name(), + return new NamedAnalyzer(indexAnalyzer.name(), AnalyzerScope.INDEX, new CompletionAnalyzer(indexAnalyzer, preserveSep, preservePositionIncrements)); } @@ -220,7 +221,7 @@ public class CompletionFieldMapper extends FieldMapper implements ArrayValueMapp public NamedAnalyzer searchAnalyzer() { final NamedAnalyzer searchAnalyzer = super.searchAnalyzer(); if (searchAnalyzer != null && !(searchAnalyzer.analyzer() instanceof CompletionAnalyzer)) { - return new NamedAnalyzer(searchAnalyzer.name(), + return new NamedAnalyzer(searchAnalyzer.name(), AnalyzerScope.INDEX, new CompletionAnalyzer(searchAnalyzer, preserveSep, preservePositionIncrements)); } return searchAnalyzer; diff --git a/core/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java b/core/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java index 2ea45f7a40c..0f64e343d98 100644 --- a/core/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java +++ b/core/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java @@ -73,6 +73,7 @@ import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.VersionType; import org.elasticsearch.index.analysis.AnalysisRegistry; +import org.elasticsearch.index.analysis.AnalyzerScope; import org.elasticsearch.index.analysis.IndexAnalyzers; import org.elasticsearch.index.analysis.NamedAnalyzer; import org.elasticsearch.index.codec.CodecService; @@ -2011,7 +2012,7 @@ public class InternalEngineTests extends ESTestCase { Index index = new Index(indexName, "_na_"); IndexSettings indexSettings = IndexSettingsModule.newIndexSettings(index, settings); IndexAnalyzers indexAnalyzers = null; - NamedAnalyzer defaultAnalyzer = new NamedAnalyzer("default", new StandardAnalyzer()); + NamedAnalyzer defaultAnalyzer = new NamedAnalyzer("default", AnalyzerScope.INDEX, new StandardAnalyzer()); indexAnalyzers = new IndexAnalyzers(indexSettings, defaultAnalyzer, defaultAnalyzer, defaultAnalyzer, Collections.emptyMap()); SimilarityService similarityService = new SimilarityService(indexSettings, Collections.emptyMap()); MapperRegistry mapperRegistry = new IndicesModule(Collections.emptyList()).getMapperRegistry(); diff --git a/core/src/test/java/org/elasticsearch/index/mapper/DocumentFieldMapperTests.java b/core/src/test/java/org/elasticsearch/index/mapper/DocumentFieldMapperTests.java index 43fc1d5c82a..2c62a2952a8 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/DocumentFieldMapperTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/DocumentFieldMapperTests.java @@ -28,6 +28,7 @@ import org.apache.lucene.util.LuceneTestCase; import org.elasticsearch.Version; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.index.analysis.AnalyzerScope; import org.elasticsearch.index.analysis.NamedAnalyzer; import java.io.IOException; @@ -40,7 +41,7 @@ public class DocumentFieldMapperTests extends LuceneTestCase { private static class FakeAnalyzer extends Analyzer { private final String output; - + public FakeAnalyzer(String output) { this.output = output; } @@ -63,7 +64,7 @@ public class DocumentFieldMapperTests extends LuceneTestCase { }; return new TokenStreamComponents(tokenizer); } - + } static class FakeFieldType extends TermBasedFieldType { @@ -71,11 +72,11 @@ public class DocumentFieldMapperTests extends LuceneTestCase { public FakeFieldType() { super(); } - + FakeFieldType(FakeFieldType other) { super(other); } - + @Override public MappedFieldType clone() { return new FakeFieldType(this); @@ -85,7 +86,7 @@ public class DocumentFieldMapperTests extends LuceneTestCase { public String typeName() { return "fake"; } - + } static class FakeFieldMapper extends FieldMapper { @@ -104,15 +105,15 @@ public class DocumentFieldMapperTests extends LuceneTestCase { protected String contentType() { return null; } - + } public void testAnalyzers() throws IOException { FakeFieldType fieldType1 = new FakeFieldType(); fieldType1.setName("field1"); - fieldType1.setIndexAnalyzer(new NamedAnalyzer("foo", new FakeAnalyzer("index"))); - fieldType1.setSearchAnalyzer(new NamedAnalyzer("bar", new FakeAnalyzer("search"))); - fieldType1.setSearchQuoteAnalyzer(new NamedAnalyzer("baz", new FakeAnalyzer("search_quote"))); + fieldType1.setIndexAnalyzer(new NamedAnalyzer("foo", AnalyzerScope.INDEX, new FakeAnalyzer("index"))); + fieldType1.setSearchAnalyzer(new NamedAnalyzer("bar", AnalyzerScope.INDEX, new FakeAnalyzer("search"))); + fieldType1.setSearchQuoteAnalyzer(new NamedAnalyzer("baz", AnalyzerScope.INDEX, new FakeAnalyzer("search_quote"))); FieldMapper fieldMapper1 = new FakeFieldMapper("field1", fieldType1); FakeFieldType fieldType2 = new FakeFieldType(); diff --git a/core/src/test/java/org/elasticsearch/index/mapper/FieldTypeTestCase.java b/core/src/test/java/org/elasticsearch/index/mapper/FieldTypeTestCase.java index 37d0436c9db..96ca2e72b95 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/FieldTypeTestCase.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/FieldTypeTestCase.java @@ -20,6 +20,7 @@ package org.elasticsearch.index.mapper; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.index.analysis.AnalyzerScope; import org.elasticsearch.index.analysis.NamedAnalyzer; import org.elasticsearch.index.similarity.BM25SimilarityProvider; import org.elasticsearch.test.ESTestCase; @@ -68,49 +69,49 @@ public abstract class FieldTypeTestCase extends ESTestCase { new Modifier("analyzer", false) { @Override public void modify(MappedFieldType ft) { - ft.setIndexAnalyzer(new NamedAnalyzer("bar", new StandardAnalyzer())); + ft.setIndexAnalyzer(new NamedAnalyzer("bar", AnalyzerScope.INDEX, new StandardAnalyzer())); } }, new Modifier("analyzer", false) { @Override public void modify(MappedFieldType ft) { - ft.setIndexAnalyzer(new NamedAnalyzer("bar", new StandardAnalyzer())); + ft.setIndexAnalyzer(new NamedAnalyzer("bar", AnalyzerScope.INDEX, new StandardAnalyzer())); } @Override public void normalizeOther(MappedFieldType other) { - other.setIndexAnalyzer(new NamedAnalyzer("foo", new StandardAnalyzer())); + other.setIndexAnalyzer(new NamedAnalyzer("foo", AnalyzerScope.INDEX, new StandardAnalyzer())); } }, new Modifier("search_analyzer", true) { @Override public void modify(MappedFieldType ft) { - ft.setSearchAnalyzer(new NamedAnalyzer("bar", new StandardAnalyzer())); + ft.setSearchAnalyzer(new NamedAnalyzer("bar", AnalyzerScope.INDEX, new StandardAnalyzer())); } }, new Modifier("search_analyzer", true) { @Override public void modify(MappedFieldType ft) { - ft.setSearchAnalyzer(new NamedAnalyzer("bar", new StandardAnalyzer())); + ft.setSearchAnalyzer(new NamedAnalyzer("bar", AnalyzerScope.INDEX, new StandardAnalyzer())); } @Override public void normalizeOther(MappedFieldType other) { - other.setSearchAnalyzer(new NamedAnalyzer("foo", new StandardAnalyzer())); + other.setSearchAnalyzer(new NamedAnalyzer("foo", AnalyzerScope.INDEX, new StandardAnalyzer())); } }, new Modifier("search_quote_analyzer", true) { @Override public void modify(MappedFieldType ft) { - ft.setSearchQuoteAnalyzer(new NamedAnalyzer("bar", new StandardAnalyzer())); + ft.setSearchQuoteAnalyzer(new NamedAnalyzer("bar", AnalyzerScope.INDEX, new StandardAnalyzer())); } }, new Modifier("search_quote_analyzer", true) { @Override public void modify(MappedFieldType ft) { - ft.setSearchQuoteAnalyzer(new NamedAnalyzer("bar", new StandardAnalyzer())); + ft.setSearchQuoteAnalyzer(new NamedAnalyzer("bar", AnalyzerScope.INDEX, new StandardAnalyzer())); } @Override public void normalizeOther(MappedFieldType other) { - other.setSearchQuoteAnalyzer(new NamedAnalyzer("foo", new StandardAnalyzer())); + other.setSearchQuoteAnalyzer(new NamedAnalyzer("foo", AnalyzerScope.INDEX, new StandardAnalyzer())); } }, new Modifier("similarity", false) { diff --git a/core/src/test/java/org/elasticsearch/index/mapper/ParentFieldMapperTests.java b/core/src/test/java/org/elasticsearch/index/mapper/ParentFieldMapperTests.java index 078c60ce19b..9f026c59922 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/ParentFieldMapperTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/ParentFieldMapperTests.java @@ -31,6 +31,7 @@ import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexService; import org.elasticsearch.index.IndexSettings; +import org.elasticsearch.index.analysis.AnalyzerScope; import org.elasticsearch.index.analysis.IndexAnalyzers; import org.elasticsearch.index.analysis.NamedAnalyzer; import org.elasticsearch.index.mapper.MapperService.MergeReason; @@ -103,7 +104,7 @@ public class ParentFieldMapperTests extends ESSingleNodeTestCase { public void testNoParentNullFieldCreatedIfNoParentSpecified() throws Exception { Index index = new Index("_index", "testUUID"); IndexSettings indexSettings = IndexSettingsModule.newIndexSettings(index, Settings.EMPTY); - NamedAnalyzer namedAnalyzer = new NamedAnalyzer("default", new StandardAnalyzer()); + NamedAnalyzer namedAnalyzer = new NamedAnalyzer("default", AnalyzerScope.INDEX, new StandardAnalyzer()); IndexAnalyzers indexAnalyzers = new IndexAnalyzers(indexSettings, namedAnalyzer, namedAnalyzer, namedAnalyzer, Collections.emptyMap()); SimilarityService similarityService = new SimilarityService(indexSettings, Collections.emptyMap());