From 2450e3ccc84dd1bc5ff7faec27e2ba2e51aa9a61 Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Sun, 16 Aug 2015 10:08:04 -0700 Subject: [PATCH] Internal: Flatten IndicesModule and add tests The IndicesModule was made up of two submodules, one which handled registering queries, and the other for registering hunspell dictionaries. This change moves those into IndicesModule. It also adds a new extension point type, InstanceMap. This is simply a Map, where K and V are actual objects, not classes like most other extension points. I also added a test method to help testing instance map extensions. This was particularly painful because of how guice binds the key and value as separate bindings, and then reconstitutes them into a Map at injection time. In order to gain access to the object which links the key and value, I had to tweak our guice copy to not use an anonymous inner class for the Provider. Note that I also renamed the existing extension point types, since they were very redundant. For example, ExtensionPoint.MapExtensionPoint is now ExtensionPoint.ClassMap. See #12783. --- .../elasticsearch/cluster/ClusterModule.java | 6 +- .../common/geo/ShapesAvailability.java | 7 +- .../common/inject/spi/ProviderLookup.java | 39 ++++-- .../common/util/ExtensionPoint.java | 78 +++++++++--- .../elasticsearch/indices/IndicesModule.java | 111 +++++++++++++++--- .../analysis/IndicesAnalysisModule.java | 47 -------- .../indices/query/IndicesQueriesModule.java | 105 ----------------- .../search/highlight/Highlighters.java | 2 +- .../search/suggest/Suggesters.java | 3 +- .../common/inject/ModuleTestCase.java | 48 +++++++- .../index/analysis/AnalysisModuleTests.java | 7 +- .../index/analysis/AnalysisTestsHelper.java | 11 +- .../index/analysis/CharFilterTests.java | 5 +- .../index/analysis/CompoundAnalysisTests.java | 9 +- .../PatternCaptureTokenFilterTests.java | 3 +- .../index/analysis/StopAnalyzerTests.java | 3 +- .../synonyms/SynonymsAnalysisTest.java | 4 +- .../index/query/TemplateQueryParserTest.java | 13 +- .../query/plugin/DummyQueryParserPlugin.java | 16 +-- .../indices/IndicesModuleTests.java | 81 +++++++++++++ .../index/analysis/AnalysisTestUtils.java | 3 +- .../index/analysis/KuromojiAnalysisTests.java | 9 +- .../analysis/SimplePhoneticAnalysisTests.java | 4 +- .../SimpleSmartChineseAnalysisTests.java | 3 +- .../index/analysis/PolishAnalysisTests.java | 3 +- .../SimplePolishTokenFilterTests.java | 3 +- 26 files changed, 362 insertions(+), 261 deletions(-) delete mode 100644 core/src/main/java/org/elasticsearch/indices/analysis/IndicesAnalysisModule.java delete mode 100644 core/src/main/java/org/elasticsearch/indices/query/IndicesQueriesModule.java create mode 100644 core/src/test/java/org/elasticsearch/indices/IndicesModuleTests.java diff --git a/core/src/main/java/org/elasticsearch/cluster/ClusterModule.java b/core/src/main/java/org/elasticsearch/cluster/ClusterModule.java index 7a4a3e0722d31..69f43b5b7c11e 100644 --- a/core/src/main/java/org/elasticsearch/cluster/ClusterModule.java +++ b/core/src/main/java/org/elasticsearch/cluster/ClusterModule.java @@ -125,9 +125,9 @@ public class ClusterModule extends AbstractModule { private final Settings settings; private final DynamicSettings.Builder clusterDynamicSettings = new DynamicSettings.Builder(); private final DynamicSettings.Builder indexDynamicSettings = new DynamicSettings.Builder(); - private final ExtensionPoint.TypeExtensionPoint shardsAllocators = new ExtensionPoint.TypeExtensionPoint<>("shards_allocator", ShardsAllocator.class); - private final ExtensionPoint.SetExtensionPoint allocationDeciders = new ExtensionPoint.SetExtensionPoint<>("allocation_decider", AllocationDecider.class, AllocationDeciders.class); - private final ExtensionPoint.SetExtensionPoint indexTemplateFilters = new ExtensionPoint.SetExtensionPoint<>("index_template_filter", IndexTemplateFilter.class); + private final ExtensionPoint.SelectedType shardsAllocators = new ExtensionPoint.SelectedType<>("shards_allocator", ShardsAllocator.class); + private final ExtensionPoint.ClassSet allocationDeciders = new ExtensionPoint.ClassSet<>("allocation_decider", AllocationDecider.class, AllocationDeciders.class); + private final ExtensionPoint.ClassSet indexTemplateFilters = new ExtensionPoint.ClassSet<>("index_template_filter", IndexTemplateFilter.class); // pkg private so tests can mock Class clusterInfoServiceImpl = InternalClusterInfoService.class; diff --git a/core/src/main/java/org/elasticsearch/common/geo/ShapesAvailability.java b/core/src/main/java/org/elasticsearch/common/geo/ShapesAvailability.java index fce1833772874..8af203f2ce8e7 100644 --- a/core/src/main/java/org/elasticsearch/common/geo/ShapesAvailability.java +++ b/core/src/main/java/org/elasticsearch/common/geo/ShapesAvailability.java @@ -19,8 +19,6 @@ package org.elasticsearch.common.geo; -import org.elasticsearch.common.Classes; - /** */ public class ShapesAvailability { @@ -48,8 +46,5 @@ public class ShapesAvailability { JTS_AVAILABLE = xJTS_AVAILABLE; } - - private ShapesAvailability() { - - } + private ShapesAvailability() {} } diff --git a/core/src/main/java/org/elasticsearch/common/inject/spi/ProviderLookup.java b/core/src/main/java/org/elasticsearch/common/inject/spi/ProviderLookup.java index 44c35779def4b..06a732b192ad7 100644 --- a/core/src/main/java/org/elasticsearch/common/inject/spi/ProviderLookup.java +++ b/core/src/main/java/org/elasticsearch/common/inject/spi/ProviderLookup.java @@ -34,6 +34,31 @@ * @since 2.0 */ public final class ProviderLookup implements Element { + + // NOTE: this class is not part of guice and was added so the provder lookup's key can be acessible for tests + public static class ProviderImpl implements Provider { + private ProviderLookup lookup; + + private ProviderImpl(ProviderLookup lookup) { + this.lookup = lookup; + } + + @Override + public T get() { + checkState(lookup.delegate != null, + "This Provider cannot be used until the Injector has been created."); + return lookup.delegate.get(); + } + + @Override + public String toString() { + return "Provider<" + lookup.key.getTypeLiteral() + ">"; + } + + public Key getKey() { + return lookup.getKey(); + } + } private final Object source; private final Key key; private Provider delegate; @@ -86,18 +111,6 @@ public Provider getDelegate() { * IllegalStateException} if you try to use it beforehand. */ public Provider getProvider() { - return new Provider() { - @Override - public T get() { - checkState(delegate != null, - "This Provider cannot be used until the Injector has been created."); - return delegate.get(); - } - - @Override - public String toString() { - return "Provider<" + key.getTypeLiteral() + ">"; - } - }; + return new ProviderImpl<>(this); } } diff --git a/core/src/main/java/org/elasticsearch/common/util/ExtensionPoint.java b/core/src/main/java/org/elasticsearch/common/util/ExtensionPoint.java index 5414c4eb7a65a..258aa30c1b8d4 100644 --- a/core/src/main/java/org/elasticsearch/common/util/ExtensionPoint.java +++ b/core/src/main/java/org/elasticsearch/common/util/ExtensionPoint.java @@ -31,21 +31,18 @@ * all extensions by a single name and ensures that extensions are not registered * more than once. */ -public abstract class ExtensionPoint { +public abstract class ExtensionPoint { protected final String name; - protected final Class extensionClass; protected final Class[] singletons; /** * Creates a new extension point * * @param name the human readable underscore case name of the extension point. This is used in error messages etc. - * @param extensionClass the base class that should be extended * @param singletons a list of singletons to bind with this extension point - these are bound in {@link #bind(Binder)} */ - public ExtensionPoint(String name, Class extensionClass, Class... singletons) { + public ExtensionPoint(String name, Class... singletons) { this.name = name; - this.extensionClass = extensionClass; this.singletons = singletons; } @@ -62,29 +59,30 @@ public final void bind(Binder binder) { } /** - * Subclasses can bind their type, map or set exentions here. + * Subclasses can bind their type, map or set extensions here. */ protected abstract void bindExtensions(Binder binder); /** * A map based extension point which allows to register keyed implementations ie. parsers or some kind of strategies. */ - public static class MapExtensionPoint extends ExtensionPoint { + public static class ClassMap extends ExtensionPoint { + protected final Class extensionClass; private final Map> extensions = new HashMap<>(); private final Set reservedKeys; /** - * Creates a new {@link org.elasticsearch.common.util.ExtensionPoint.MapExtensionPoint} + * Creates a new {@link ClassMap} * * @param name the human readable underscore case name of the extension poing. This is used in error messages etc. * @param extensionClass the base class that should be extended * @param singletons a list of singletons to bind with this extension point - these are bound in {@link #bind(Binder)} * @param reservedKeys a set of reserved keys by internal implementations */ - public MapExtensionPoint(String name, Class extensionClass, Set reservedKeys, Class... singletons) { - super(name, extensionClass, singletons); + public ClassMap(String name, Class extensionClass, Set reservedKeys, Class... singletons) { + super(name, singletons); + this.extensionClass = extensionClass; this.reservedKeys = reservedKeys; - } /** @@ -118,13 +116,13 @@ protected final void bindExtensions(Binder binder) { } /** - * A Type extension point which basically allows to registerd keyed extensions like {@link org.elasticsearch.common.util.ExtensionPoint.MapExtensionPoint} + * A Type extension point which basically allows to registerd keyed extensions like {@link ClassMap} * but doesn't instantiate and bind all the registered key value pairs but instead replace a singleton based on a given setting via {@link #bindType(Binder, Settings, String, String)} * Note: {@link #bind(Binder)} is not supported by this class */ - public static final class TypeExtensionPoint extends MapExtensionPoint { + public static final class SelectedType extends ClassMap { - public TypeExtensionPoint(String name, Class extensionClass) { + public SelectedType(String name, Class extensionClass) { super(name, extensionClass, Collections.EMPTY_SET); } @@ -153,18 +151,20 @@ public String bindType(Binder binder, Settings settings, String settingsKey, Str /** * A set based extension point which allows to register extended classes that might be used to chain additional functionality etc. */ - public final static class SetExtensionPoint extends ExtensionPoint { + public final static class ClassSet extends ExtensionPoint { + protected final Class extensionClass; private final Set> extensions = new HashSet<>(); /** - * Creates a new {@link org.elasticsearch.common.util.ExtensionPoint.SetExtensionPoint} + * Creates a new {@link ClassSet} * * @param name the human readable underscore case name of the extension poing. This is used in error messages etc. * @param extensionClass the base class that should be extended * @param singletons a list of singletons to bind with this extension point - these are bound in {@link #bind(Binder)} */ - public SetExtensionPoint(String name, Class extensionClass, Class... singletons) { - super(name, extensionClass, singletons); + public ClassSet(String name, Class extensionClass, Class... singletons) { + super(name, singletons); + this.extensionClass = extensionClass; } /** @@ -188,4 +188,46 @@ protected final void bindExtensions(Binder binder) { } } } + + /** + * A an instance of a map, mapping one instance value to another. Both key and value are instances, not classes + * like with other extension points. + */ + public final static class InstanceMap extends ExtensionPoint { + private final Map map = new HashMap<>(); + private final Class keyType; + private final Class valueType; + + /** + * Creates a new {@link ClassSet} + * + * @param name the human readable underscore case name of the extension point. This is used in error messages. + * @param singletons a list of singletons to bind with this extension point - these are bound in {@link #bind(Binder)} + */ + public InstanceMap(String name, Class keyType, Class valueType, Class... singletons) { + super(name, singletons); + this.keyType = keyType; + this.valueType = valueType; + } + + /** + * Registers a mapping from {@param key} to {@param value} + * + * @throws IllegalArgumentException iff the key is already registered + */ + public final void registerExtension(K key, V value) { + V old = map.put(key, value); + if (old != null) { + throw new IllegalArgumentException("Cannot register [" + this.name + "] with key [" + key + "] to [" + value + "], already registered to [" + old + "]"); + } + } + + @Override + protected void bindExtensions(Binder binder) { + MapBinder mapBinder = MapBinder.newMapBinder(binder, keyType, valueType); + for (Map.Entry entry : map.entrySet()) { + mapBinder.addBinding(entry.getKey()).toInstance(entry.getValue()); + } + } + } } diff --git a/core/src/main/java/org/elasticsearch/indices/IndicesModule.java b/core/src/main/java/org/elasticsearch/indices/IndicesModule.java index 4e8946b533951..2704478ed4236 100644 --- a/core/src/main/java/org/elasticsearch/indices/IndicesModule.java +++ b/core/src/main/java/org/elasticsearch/indices/IndicesModule.java @@ -19,15 +19,18 @@ package org.elasticsearch.indices; -import com.google.common.collect.ImmutableList; - +import org.apache.lucene.analysis.hunspell.Dictionary; import org.elasticsearch.action.update.UpdateHelper; import org.elasticsearch.cluster.metadata.MetaDataIndexUpgradeService; +import org.elasticsearch.common.geo.ShapesAvailability; import org.elasticsearch.common.inject.AbstractModule; -import org.elasticsearch.common.inject.Module; -import org.elasticsearch.common.inject.SpawnModules; +import org.elasticsearch.common.inject.multibindings.MapBinder; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.indices.analysis.IndicesAnalysisModule; +import org.elasticsearch.common.util.ExtensionPoint; +import org.elasticsearch.index.query.*; +import org.elasticsearch.index.query.functionscore.FunctionScoreQueryParser; +import org.elasticsearch.indices.analysis.HunspellService; +import org.elasticsearch.indices.analysis.IndicesAnalysisService; import org.elasticsearch.indices.cache.query.IndicesQueryCache; import org.elasticsearch.indices.cache.request.IndicesRequestCache; import org.elasticsearch.indices.cluster.IndicesClusterStateService; @@ -35,7 +38,6 @@ import org.elasticsearch.indices.fielddata.cache.IndicesFieldDataCacheListener; import org.elasticsearch.indices.flush.SyncedFlushService; import org.elasticsearch.indices.memory.IndexingMemoryController; -import org.elasticsearch.indices.query.IndicesQueriesModule; import org.elasticsearch.indices.recovery.RecoverySettings; import org.elasticsearch.indices.recovery.RecoverySource; import org.elasticsearch.indices.recovery.RecoveryTarget; @@ -43,28 +45,100 @@ import org.elasticsearch.indices.store.TransportNodesListShardStoreMetaData; import org.elasticsearch.indices.ttl.IndicesTTLService; +import java.security.cert.Extension; +import java.util.HashMap; +import java.util.Map; + /** - * + * Configures classes and services that are shared by indices on each node. */ -public class IndicesModule extends AbstractModule implements SpawnModules { +public class IndicesModule extends AbstractModule { private final Settings settings; + private final ExtensionPoint.ClassSet queryParsers + = new ExtensionPoint.ClassSet<>("query_parser", QueryParser.class); + private final ExtensionPoint.InstanceMap hunspellDictionaries + = new ExtensionPoint.InstanceMap<>("hunspell_dictionary", String.class, Dictionary.class); + public IndicesModule(Settings settings) { this.settings = settings; + registerBuiltinQueryParsers(); } + + private void registerBuiltinQueryParsers() { + registerQueryParser(MatchQueryParser.class); + registerQueryParser(MultiMatchQueryParser.class); + registerQueryParser(NestedQueryParser.class); + registerQueryParser(HasChildQueryParser.class); + registerQueryParser(HasParentQueryParser.class); + registerQueryParser(DisMaxQueryParser.class); + registerQueryParser(IdsQueryParser.class); + registerQueryParser(MatchAllQueryParser.class); + registerQueryParser(QueryStringQueryParser.class); + registerQueryParser(BoostingQueryParser.class); + registerQueryParser(BoolQueryParser.class); + registerQueryParser(TermQueryParser.class); + registerQueryParser(TermsQueryParser.class); + registerQueryParser(FuzzyQueryParser.class); + registerQueryParser(RegexpQueryParser.class); + registerQueryParser(RangeQueryParser.class); + registerQueryParser(PrefixQueryParser.class); + registerQueryParser(WildcardQueryParser.class); + registerQueryParser(FilteredQueryParser.class); + registerQueryParser(ConstantScoreQueryParser.class); + registerQueryParser(SpanTermQueryParser.class); + registerQueryParser(SpanNotQueryParser.class); + registerQueryParser(SpanWithinQueryParser.class); + registerQueryParser(SpanContainingQueryParser.class); + registerQueryParser(FieldMaskingSpanQueryParser.class); + registerQueryParser(SpanFirstQueryParser.class); + registerQueryParser(SpanNearQueryParser.class); + registerQueryParser(SpanOrQueryParser.class); + registerQueryParser(MoreLikeThisQueryParser.class); + registerQueryParser(WrapperQueryParser.class); + registerQueryParser(IndicesQueryParser.class); + registerQueryParser(CommonTermsQueryParser.class); + registerQueryParser(SpanMultiTermQueryParser.class); + registerQueryParser(FunctionScoreQueryParser.class); + registerQueryParser(SimpleQueryStringParser.class); + registerQueryParser(TemplateQueryParser.class); + registerQueryParser(TypeQueryParser.class); + registerQueryParser(LimitQueryParser.class); + registerQueryParser(ScriptQueryParser.class); + registerQueryParser(GeoDistanceQueryParser.class); + registerQueryParser(GeoDistanceRangeQueryParser.class); + registerQueryParser(GeoBoundingBoxQueryParser.class); + registerQueryParser(GeohashCellQuery.Parser.class); + registerQueryParser(GeoPolygonQueryParser.class); + registerQueryParser(QueryFilterParser.class); + registerQueryParser(FQueryFilterParser.class); + registerQueryParser(AndQueryParser.class); + registerQueryParser(OrQueryParser.class); + registerQueryParser(NotQueryParser.class); + registerQueryParser(ExistsQueryParser.class); + registerQueryParser(MissingQueryParser.class); - @Override - public Iterable spawnModules() { - return ImmutableList.of(new IndicesQueriesModule(), new IndicesAnalysisModule()); + if (ShapesAvailability.JTS_AVAILABLE) { + registerQueryParser(GeoShapeQueryParser.class); + } + } + + public void registerQueryParser(Class queryParser) { + queryParsers.registerExtension(queryParser); + } + + public void registerHunspellDictionary(String name, Dictionary dictionary) { + hunspellDictionaries.registerExtension(name, dictionary); } @Override protected void configure() { - bind(IndicesLifecycle.class).to(InternalIndicesLifecycle.class).asEagerSingleton(); + bindQueryParsersExtension(); + bindHunspellExtension(); + bind(IndicesLifecycle.class).to(InternalIndicesLifecycle.class).asEagerSingleton(); bind(IndicesService.class).asEagerSingleton(); - bind(RecoverySettings.class).asEagerSingleton(); bind(RecoveryTarget.class).asEagerSingleton(); bind(RecoverySource.class).asEagerSingleton(); @@ -80,7 +154,16 @@ protected void configure() { bind(IndicesWarmer.class).asEagerSingleton(); bind(UpdateHelper.class).asEagerSingleton(); bind(MetaDataIndexUpgradeService.class).asEagerSingleton(); - bind(IndicesFieldDataCacheListener.class).asEagerSingleton(); } + + protected void bindQueryParsersExtension() { + queryParsers.bind(binder()); + } + + protected void bindHunspellExtension() { + hunspellDictionaries.bind(binder()); + bind(HunspellService.class).asEagerSingleton(); + bind(IndicesAnalysisService.class).asEagerSingleton(); + } } diff --git a/core/src/main/java/org/elasticsearch/indices/analysis/IndicesAnalysisModule.java b/core/src/main/java/org/elasticsearch/indices/analysis/IndicesAnalysisModule.java deleted file mode 100644 index 5c5de5f435669..0000000000000 --- a/core/src/main/java/org/elasticsearch/indices/analysis/IndicesAnalysisModule.java +++ /dev/null @@ -1,47 +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 com.google.common.collect.Maps; -import org.apache.lucene.analysis.hunspell.Dictionary; -import org.elasticsearch.common.inject.AbstractModule; -import org.elasticsearch.common.inject.multibindings.MapBinder; - -import java.util.Map; - -public class IndicesAnalysisModule extends AbstractModule { - - private final Map hunspellDictionaries = Maps.newHashMap(); - - public void addHunspellDictionary(String lang, Dictionary dictionary) { - hunspellDictionaries.put(lang, dictionary); - } - - @Override - protected void configure() { - bind(IndicesAnalysisService.class).asEagerSingleton(); - - MapBinder dictionariesBinder = MapBinder.newMapBinder(binder(), String.class, Dictionary.class); - for (Map.Entry entry : hunspellDictionaries.entrySet()) { - dictionariesBinder.addBinding(entry.getKey()).toInstance(entry.getValue()); - } - bind(HunspellService.class).asEagerSingleton(); - } -} \ No newline at end of file diff --git a/core/src/main/java/org/elasticsearch/indices/query/IndicesQueriesModule.java b/core/src/main/java/org/elasticsearch/indices/query/IndicesQueriesModule.java deleted file mode 100644 index fb7ca1784e37e..0000000000000 --- a/core/src/main/java/org/elasticsearch/indices/query/IndicesQueriesModule.java +++ /dev/null @@ -1,105 +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.query; - -import com.google.common.collect.Sets; -import org.elasticsearch.common.geo.ShapesAvailability; -import org.elasticsearch.common.inject.AbstractModule; -import org.elasticsearch.common.inject.multibindings.Multibinder; -import org.elasticsearch.index.query.*; -import org.elasticsearch.index.query.functionscore.FunctionScoreQueryParser; - -import java.util.Set; - -public class IndicesQueriesModule extends AbstractModule { - - private Set> queryParsersClasses = Sets.newHashSet(); - - public synchronized IndicesQueriesModule addQuery(Class queryParser) { - queryParsersClasses.add(queryParser); - return this; - } - - @Override - protected void configure() { - bind(IndicesQueriesRegistry.class).asEagerSingleton(); - - Multibinder qpBinders = Multibinder.newSetBinder(binder(), QueryParser.class); - for (Class queryParser : queryParsersClasses) { - qpBinders.addBinding().to(queryParser).asEagerSingleton(); - } - qpBinders.addBinding().to(MatchQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(MultiMatchQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(NestedQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(HasChildQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(HasParentQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(DisMaxQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(IdsQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(MatchAllQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(QueryStringQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(BoostingQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(BoolQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(TermQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(TermsQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(FuzzyQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(RegexpQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(RangeQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(PrefixQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(WildcardQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(FilteredQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(ConstantScoreQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(SpanTermQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(SpanNotQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(SpanWithinQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(SpanContainingQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(FieldMaskingSpanQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(SpanFirstQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(SpanNearQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(SpanOrQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(MoreLikeThisQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(WrapperQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(IndicesQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(CommonTermsQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(SpanMultiTermQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(FunctionScoreQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(SimpleQueryStringParser.class).asEagerSingleton(); - qpBinders.addBinding().to(TemplateQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(TypeQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(LimitQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(TermsQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(ScriptQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(GeoDistanceQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(GeoDistanceRangeQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(GeoBoundingBoxQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(GeohashCellQuery.Parser.class).asEagerSingleton(); - qpBinders.addBinding().to(GeoPolygonQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(QueryFilterParser.class).asEagerSingleton(); - qpBinders.addBinding().to(FQueryFilterParser.class).asEagerSingleton(); - qpBinders.addBinding().to(AndQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(OrQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(NotQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(ExistsQueryParser.class).asEagerSingleton(); - qpBinders.addBinding().to(MissingQueryParser.class).asEagerSingleton(); - - if (ShapesAvailability.JTS_AVAILABLE) { - qpBinders.addBinding().to(GeoShapeQueryParser.class).asEagerSingleton(); - } - } -} diff --git a/core/src/main/java/org/elasticsearch/search/highlight/Highlighters.java b/core/src/main/java/org/elasticsearch/search/highlight/Highlighters.java index 349227f5c57c9..1e519957aacec 100644 --- a/core/src/main/java/org/elasticsearch/search/highlight/Highlighters.java +++ b/core/src/main/java/org/elasticsearch/search/highlight/Highlighters.java @@ -29,7 +29,7 @@ /** * An extensions point and registry for all the highlighters a node supports. */ -public class Highlighters extends ExtensionPoint.MapExtensionPoint { +public class Highlighters extends ExtensionPoint.ClassMap { @Deprecated // remove in 3.0 private static final String FAST_VECTOR_HIGHLIGHTER = "fast-vector-highlighter"; diff --git a/core/src/main/java/org/elasticsearch/search/suggest/Suggesters.java b/core/src/main/java/org/elasticsearch/search/suggest/Suggesters.java index 1be80b5750236..25703e80b6a8b 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/Suggesters.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/Suggesters.java @@ -18,7 +18,6 @@ */ package org.elasticsearch.search.suggest; -import org.elasticsearch.common.inject.Binder; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.util.ExtensionPoint; import org.elasticsearch.script.ScriptService; @@ -31,7 +30,7 @@ /** * */ -public final class Suggesters extends ExtensionPoint.MapExtensionPoint { +public final class Suggesters extends ExtensionPoint.ClassMap { private final Map parsers; public Suggesters() { diff --git a/core/src/test/java/org/elasticsearch/common/inject/ModuleTestCase.java b/core/src/test/java/org/elasticsearch/common/inject/ModuleTestCase.java index 323b9f5ca4a5c..2b1330abe63c4 100644 --- a/core/src/test/java/org/elasticsearch/common/inject/ModuleTestCase.java +++ b/core/src/test/java/org/elasticsearch/common/inject/ModuleTestCase.java @@ -24,12 +24,15 @@ import org.elasticsearch.common.inject.spi.InstanceBinding; import org.elasticsearch.common.inject.spi.LinkedKeyBinding; import org.elasticsearch.common.inject.spi.ProviderInstanceBinding; +import org.elasticsearch.common.inject.spi.ProviderLookup; import org.elasticsearch.test.ESTestCase; import java.lang.annotation.Annotation; import java.lang.reflect.Type; +import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; /** @@ -77,7 +80,7 @@ public void assertBindingFailure(Module module, String... msgs) { /** * Configures the module and checks a Map of the "to" class - * is bound to "theClas". + * is bound to "theClass". */ public void assertMapMultiBinding(Module module, Class to, Class theClass) { List elements = Elements.getElements(module); @@ -138,10 +141,18 @@ public void assertSetMultiBinding(Module module, Class to, Class... classes) { assertTrue("Did not find provider for set of " + to.getName(), providerFound); } + /** + * Configures the module, and ensures an instance is bound to the "to" class, and the + * provided tester returns true on the instance. + */ public void assertInstanceBinding(Module module, Class to, Predicate tester) { assertInstanceBindingWithAnnotation(module, to, tester, null); } + /** + * Like {@link #assertInstanceBinding(Module, Class, Predicate)}, but filters the + * classes checked by the given annotation. + */ public void assertInstanceBindingWithAnnotation(Module module, Class to, Predicate tester, Class annotation) { List elements = Elements.getElements(module); for (Element element : elements) { @@ -161,4 +172,39 @@ public void assertInstanceBindingWithAnnotation(Module module, Class to, } fail("Did not find any instance binding to " + to.getName() + ". Found these bindings:\n" + s); } + + /** + * Configures the module, and ensures a map exists between the "keyType" and "valueType", + * and that all of the "expected" values are bound. + */ + @SuppressWarnings("unchecked") + public void assertMapInstanceBinding(Module module, Class keyType, Class valueType, Map expected) throws Exception { + // this method is insane because java type erasure makes it incredibly difficult... + Map keys = new HashMap<>(); + Map values = new HashMap<>(); + List elements = Elements.getElements(module); + for (Element element : elements) { + if (element instanceof InstanceBinding) { + InstanceBinding binding = (InstanceBinding) element; + if (binding.getKey().getRawType().equals(valueType)) { + values.put(binding.getKey(), (V)binding.getInstance()); + } else if (binding.getInstance() instanceof Map.Entry) { + Map.Entry entry = (Map.Entry)binding.getInstance(); + Object key = entry.getKey(); + Object providerValue = entry.getValue(); + if (key.getClass().equals(keyType) && providerValue instanceof ProviderLookup.ProviderImpl) { + ProviderLookup.ProviderImpl provider = (ProviderLookup.ProviderImpl)providerValue; + keys.put((K)key, provider.getKey()); + } + } + } + } + for (Map.Entry entry : expected.entrySet()) { + Key valueKey = keys.get(entry.getKey()); + assertNotNull("Could not find binding for key [" + entry.getKey() + "], found these keys:\n" + keys.keySet(), valueKey); + V value = values.get(valueKey); + assertNotNull("Could not find value for instance key [" + valueKey + "], found these bindings:\n" + elements); + assertEquals(entry.getValue(), value); + } + } } diff --git a/core/src/test/java/org/elasticsearch/index/analysis/AnalysisModuleTests.java b/core/src/test/java/org/elasticsearch/index/analysis/AnalysisModuleTests.java index 8a81705684d4d..4a09eeed6b611 100644 --- a/core/src/test/java/org/elasticsearch/index/analysis/AnalysisModuleTests.java +++ b/core/src/test/java/org/elasticsearch/index/analysis/AnalysisModuleTests.java @@ -40,7 +40,6 @@ import org.elasticsearch.index.IndexNameModule; import org.elasticsearch.index.analysis.filter1.MyFilterTokenFilterFactory; import org.elasticsearch.index.settings.IndexSettingsModule; -import org.elasticsearch.indices.analysis.IndicesAnalysisModule; import org.elasticsearch.indices.analysis.IndicesAnalysisService; import org.elasticsearch.test.ESTestCase; import org.hamcrest.MatcherAssert; @@ -55,7 +54,9 @@ import java.util.Set; import static org.elasticsearch.common.settings.Settings.settingsBuilder; -import static org.hamcrest.Matchers.*; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.Matchers.is; /** * @@ -66,7 +67,7 @@ public class AnalysisModuleTests extends ESTestCase { public AnalysisService getAnalysisService(Settings settings) { Index index = new Index("test"); - Injector parentInjector = new ModulesBuilder().add(new SettingsModule(settings), new EnvironmentModule(new Environment(settings)), new IndicesAnalysisModule()).createInjector(); + Injector parentInjector = new ModulesBuilder().add(new SettingsModule(settings), new EnvironmentModule(new Environment(settings))).createInjector(); AnalysisModule analysisModule = new AnalysisModule(settings, parentInjector.getInstance(IndicesAnalysisService.class)); analysisModule.addTokenFilter("myfilter", MyFilterTokenFilterFactory.class); injector = new ModulesBuilder().add( diff --git a/core/src/test/java/org/elasticsearch/index/analysis/AnalysisTestsHelper.java b/core/src/test/java/org/elasticsearch/index/analysis/AnalysisTestsHelper.java index 74ff95d4a144d..72eac6860ba66 100644 --- a/core/src/test/java/org/elasticsearch/index/analysis/AnalysisTestsHelper.java +++ b/core/src/test/java/org/elasticsearch/index/analysis/AnalysisTestsHelper.java @@ -30,7 +30,7 @@ import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexNameModule; import org.elasticsearch.index.settings.IndexSettingsModule; -import org.elasticsearch.indices.analysis.IndicesAnalysisModule; +import org.elasticsearch.indices.IndicesModule; import org.elasticsearch.indices.analysis.IndicesAnalysisService; import java.nio.file.Path; @@ -52,8 +52,15 @@ public static AnalysisService createAnalysisServiceFromSettings( if (settings.get(IndexMetaData.SETTING_VERSION_CREATED) == null) { settings = Settings.builder().put(settings).put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build(); } + IndicesModule indicesModule = new IndicesModule(settings) { + @Override + public void configure() { + // skip services + bindHunspellExtension(); + } + }; Injector parentInjector = new ModulesBuilder().add(new SettingsModule(settings), - new EnvironmentModule(new Environment(settings)), new IndicesAnalysisModule()).createInjector(); + new EnvironmentModule(new Environment(settings)), indicesModule).createInjector(); AnalysisModule analysisModule = new AnalysisModule(settings, parentInjector.getInstance(IndicesAnalysisService.class)); diff --git a/core/src/test/java/org/elasticsearch/index/analysis/CharFilterTests.java b/core/src/test/java/org/elasticsearch/index/analysis/CharFilterTests.java index ef2c6d4e7fb66..0171b4cc695d7 100644 --- a/core/src/test/java/org/elasticsearch/index/analysis/CharFilterTests.java +++ b/core/src/test/java/org/elasticsearch/index/analysis/CharFilterTests.java @@ -29,7 +29,6 @@ import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexNameModule; import org.elasticsearch.index.settings.IndexSettingsModule; -import org.elasticsearch.indices.analysis.IndicesAnalysisModule; import org.elasticsearch.indices.analysis.IndicesAnalysisService; import org.elasticsearch.test.ESTokenStreamTestCase; import org.junit.Test; @@ -51,7 +50,7 @@ public void testMappingCharFilter() throws Exception { .putArray("index.analysis.analyzer.custom_with_char_filter.char_filter", "my_mapping") .put("path.home", createTempDir().toString()) .build(); - Injector parentInjector = new ModulesBuilder().add(new SettingsModule(settings), new EnvironmentModule(new Environment(settings)), new IndicesAnalysisModule()).createInjector(); + Injector parentInjector = new ModulesBuilder().add(new SettingsModule(settings), new EnvironmentModule(new Environment(settings))).createInjector(); Injector injector = new ModulesBuilder().add( new IndexSettingsModule(index, settings), new IndexNameModule(index), @@ -77,7 +76,7 @@ public void testHtmlStripCharFilter() throws Exception { .putArray("index.analysis.analyzer.custom_with_char_filter.char_filter", "html_strip") .put("path.home", createTempDir().toString()) .build(); - Injector parentInjector = new ModulesBuilder().add(new SettingsModule(settings), new EnvironmentModule(new Environment(settings)), new IndicesAnalysisModule()).createInjector(); + Injector parentInjector = new ModulesBuilder().add(new SettingsModule(settings), new EnvironmentModule(new Environment(settings))).createInjector(); Injector injector = new ModulesBuilder().add( new IndexSettingsModule(index, settings), new IndexNameModule(index), diff --git a/core/src/test/java/org/elasticsearch/index/analysis/CompoundAnalysisTests.java b/core/src/test/java/org/elasticsearch/index/analysis/CompoundAnalysisTests.java index ad81450c33686..28b30e9ff5df8 100644 --- a/core/src/test/java/org/elasticsearch/index/analysis/CompoundAnalysisTests.java +++ b/core/src/test/java/org/elasticsearch/index/analysis/CompoundAnalysisTests.java @@ -37,7 +37,6 @@ import org.elasticsearch.index.analysis.compound.DictionaryCompoundWordTokenFilterFactory; import org.elasticsearch.index.analysis.filter1.MyFilterTokenFilterFactory; import org.elasticsearch.index.settings.IndexSettingsModule; -import org.elasticsearch.indices.analysis.IndicesAnalysisModule; import org.elasticsearch.indices.analysis.IndicesAnalysisService; import org.elasticsearch.test.ESTestCase; import org.hamcrest.MatcherAssert; @@ -48,7 +47,9 @@ import java.util.List; import static org.elasticsearch.common.settings.Settings.settingsBuilder; -import static org.hamcrest.Matchers.*; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasItems; +import static org.hamcrest.Matchers.instanceOf; /** */ @@ -58,7 +59,7 @@ public class CompoundAnalysisTests extends ESTestCase { public void testDefaultsCompoundAnalysis() throws Exception { Index index = new Index("test"); Settings settings = getJsonSettings(); - Injector parentInjector = new ModulesBuilder().add(new SettingsModule(settings), new EnvironmentModule(new Environment(settings)), new IndicesAnalysisModule()).createInjector(); + Injector parentInjector = new ModulesBuilder().add(new SettingsModule(settings), new EnvironmentModule(new Environment(settings))).createInjector(); AnalysisModule analysisModule = new AnalysisModule(settings, parentInjector.getInstance(IndicesAnalysisService.class)); analysisModule.addTokenFilter("myfilter", MyFilterTokenFilterFactory.class); Injector injector = new ModulesBuilder().add( @@ -85,7 +86,7 @@ public void testDictionaryDecompounder() throws Exception { private List analyze(Settings settings, String analyzerName, String text) throws IOException { Index index = new Index("test"); - Injector parentInjector = new ModulesBuilder().add(new SettingsModule(settings), new EnvironmentModule(new Environment(settings)), new IndicesAnalysisModule()).createInjector(); + Injector parentInjector = new ModulesBuilder().add(new SettingsModule(settings), new EnvironmentModule(new Environment(settings))).createInjector(); AnalysisModule analysisModule = new AnalysisModule(settings, parentInjector.getInstance(IndicesAnalysisService.class)); analysisModule.addTokenFilter("myfilter", MyFilterTokenFilterFactory.class); Injector injector = new ModulesBuilder().add( diff --git a/core/src/test/java/org/elasticsearch/index/analysis/PatternCaptureTokenFilterTests.java b/core/src/test/java/org/elasticsearch/index/analysis/PatternCaptureTokenFilterTests.java index 2796367f07f95..6a7275b73e152 100644 --- a/core/src/test/java/org/elasticsearch/index/analysis/PatternCaptureTokenFilterTests.java +++ b/core/src/test/java/org/elasticsearch/index/analysis/PatternCaptureTokenFilterTests.java @@ -30,7 +30,6 @@ import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexNameModule; import org.elasticsearch.index.settings.IndexSettingsModule; -import org.elasticsearch.indices.analysis.IndicesAnalysisModule; import org.elasticsearch.indices.analysis.IndicesAnalysisService; import org.elasticsearch.test.ESTokenStreamTestCase; import org.junit.Test; @@ -48,7 +47,7 @@ public void testPatternCaptureTokenFilter() throws Exception { .loadFromStream(json, getClass().getResourceAsStream(json)) .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) .build(); - Injector parentInjector = new ModulesBuilder().add(new SettingsModule(settings), new EnvironmentModule(new Environment(settings)), new IndicesAnalysisModule()).createInjector(); + Injector parentInjector = new ModulesBuilder().add(new SettingsModule(settings), new EnvironmentModule(new Environment(settings))).createInjector(); Injector injector = new ModulesBuilder().add( new IndexSettingsModule(index, settings), new IndexNameModule(index), diff --git a/core/src/test/java/org/elasticsearch/index/analysis/StopAnalyzerTests.java b/core/src/test/java/org/elasticsearch/index/analysis/StopAnalyzerTests.java index 5ec0178cea04f..9265587929cae 100644 --- a/core/src/test/java/org/elasticsearch/index/analysis/StopAnalyzerTests.java +++ b/core/src/test/java/org/elasticsearch/index/analysis/StopAnalyzerTests.java @@ -30,7 +30,6 @@ import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexNameModule; import org.elasticsearch.index.settings.IndexSettingsModule; -import org.elasticsearch.indices.analysis.IndicesAnalysisModule; import org.elasticsearch.indices.analysis.IndicesAnalysisService; import org.elasticsearch.test.ESTokenStreamTestCase; import org.junit.Test; @@ -48,7 +47,7 @@ public void testDefaultsCompoundAnalysis() throws Exception { .put("path.home", createTempDir().toString()) .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) .build(); - Injector parentInjector = new ModulesBuilder().add(new SettingsModule(settings), new EnvironmentModule(new Environment(settings)), new IndicesAnalysisModule()).createInjector(); + Injector parentInjector = new ModulesBuilder().add(new SettingsModule(settings), new EnvironmentModule(new Environment(settings))).createInjector(); Injector injector = new ModulesBuilder().add( new IndexSettingsModule(index, settings), new IndexNameModule(index), diff --git a/core/src/test/java/org/elasticsearch/index/analysis/synonyms/SynonymsAnalysisTest.java b/core/src/test/java/org/elasticsearch/index/analysis/synonyms/SynonymsAnalysisTest.java index e38ab9a00244f..371a092ff6e0d 100644 --- a/core/src/test/java/org/elasticsearch/index/analysis/synonyms/SynonymsAnalysisTest.java +++ b/core/src/test/java/org/elasticsearch/index/analysis/synonyms/SynonymsAnalysisTest.java @@ -39,7 +39,6 @@ import org.elasticsearch.index.analysis.AnalysisModule; import org.elasticsearch.index.analysis.AnalysisService; import org.elasticsearch.index.settings.IndexSettingsModule; -import org.elasticsearch.indices.analysis.IndicesAnalysisModule; import org.elasticsearch.indices.analysis.IndicesAnalysisService; import org.elasticsearch.test.ESTestCase; import org.hamcrest.MatcherAssert; @@ -80,8 +79,7 @@ public void testSynonymsAnalysis() throws IOException { Injector parentInjector = new ModulesBuilder().add( new SettingsModule(settings), - new EnvironmentModule(new Environment(settings)), - new IndicesAnalysisModule()) + new EnvironmentModule(new Environment(settings))) .createInjector(); Injector injector = new ModulesBuilder().add( new IndexSettingsModule(index, settings), diff --git a/core/src/test/java/org/elasticsearch/index/query/TemplateQueryParserTest.java b/core/src/test/java/org/elasticsearch/index/query/TemplateQueryParserTest.java index aab3c6fe1c4e7..34c890a51894a 100644 --- a/core/src/test/java/org/elasticsearch/index/query/TemplateQueryParserTest.java +++ b/core/src/test/java/org/elasticsearch/index/query/TemplateQueryParserTest.java @@ -21,6 +21,7 @@ import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.Query; import org.elasticsearch.Version; +import org.elasticsearch.action.ActionModule; import org.elasticsearch.cluster.ClusterService; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.inject.AbstractModule; @@ -39,15 +40,13 @@ import org.elasticsearch.index.analysis.AnalysisModule; import org.elasticsearch.index.cache.IndexCacheModule; import org.elasticsearch.index.query.functionscore.ScoreFunctionParser; -import org.elasticsearch.index.query.functionscore.ScoreFunctionParserMapper; import org.elasticsearch.index.settings.IndexSettingsModule; import org.elasticsearch.index.similarity.SimilarityModule; +import org.elasticsearch.indices.IndicesModule; import org.elasticsearch.indices.analysis.IndicesAnalysisService; import org.elasticsearch.indices.breaker.CircuitBreakerService; import org.elasticsearch.indices.breaker.NoneCircuitBreakerService; -import org.elasticsearch.indices.query.IndicesQueriesModule; import org.elasticsearch.script.ScriptModule; -import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.threadpool.ThreadPoolModule; @@ -80,7 +79,13 @@ public void setup() throws IOException { new EnvironmentModule(new Environment(settings)), new SettingsModule(settings), new ThreadPoolModule(new ThreadPool(settings)), - new IndicesQueriesModule(), + new IndicesModule(settings) { + @Override + public void configure() { + // skip services + bindQueryParsersExtension(); + } + }, new ScriptModule(settings), new IndexSettingsModule(index, settings), new IndexCacheModule(settings), diff --git a/core/src/test/java/org/elasticsearch/index/query/plugin/DummyQueryParserPlugin.java b/core/src/test/java/org/elasticsearch/index/query/plugin/DummyQueryParserPlugin.java index 2e0356e46194b..cdffcc8c36b64 100644 --- a/core/src/test/java/org/elasticsearch/index/query/plugin/DummyQueryParserPlugin.java +++ b/core/src/test/java/org/elasticsearch/index/query/plugin/DummyQueryParserPlugin.java @@ -23,15 +23,13 @@ import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.Weight; -import org.elasticsearch.common.inject.Module; -import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.query.QueryParser; import org.elasticsearch.index.query.QueryParsingException; -import org.elasticsearch.indices.query.IndicesQueriesModule; +import org.elasticsearch.indices.IndicesModule; import org.elasticsearch.plugins.AbstractPlugin; import java.io.IOException; @@ -48,16 +46,8 @@ public String description() { return "dummy query"; } - @Override - public void processModule(Module module) { - if (module instanceof IndicesQueriesModule) { - IndicesQueriesModule indicesQueriesModule = (IndicesQueriesModule) module; - indicesQueriesModule.addQuery(DummyQueryParser.class); - } - } - - public Settings settings() { - return Settings.EMPTY; + public void onModule(IndicesModule module) { + module.registerQueryParser(DummyQueryParser.class); } public static class DummyQueryBuilder extends QueryBuilder { diff --git a/core/src/test/java/org/elasticsearch/indices/IndicesModuleTests.java b/core/src/test/java/org/elasticsearch/indices/IndicesModuleTests.java new file mode 100644 index 0000000000000..7727d8c1b93a3 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/indices/IndicesModuleTests.java @@ -0,0 +1,81 @@ +/* + * 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; + +import org.apache.lucene.analysis.hunspell.Dictionary; +import org.apache.lucene.search.Query; +import org.elasticsearch.common.inject.ModuleTestCase; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.index.query.QueryParseContext; +import org.elasticsearch.index.query.QueryParser; +import org.elasticsearch.index.query.QueryParsingException; +import org.elasticsearch.index.query.TermQueryParser; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Collections; + +public class IndicesModuleTests extends ModuleTestCase { + + static class FakeQueryParser implements QueryParser { + @Override + public String[] names() { + return new String[] {"fake-query-parser"}; + } + @Override + public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException { + return null; + } + } + + public void testRegisterQueryParser() { + IndicesModule module = new IndicesModule(Settings.EMPTY); + module.registerQueryParser(FakeQueryParser.class); + assertSetMultiBinding(module, QueryParser.class, FakeQueryParser.class); + } + + public void testRegisterQueryParserDuplicate() { + IndicesModule module = new IndicesModule(Settings.EMPTY); + try { + module.registerQueryParser(TermQueryParser.class); + } catch (IllegalArgumentException e) { + assertEquals(e.getMessage(), "Can't register the same [query_parser] more than once for [" + TermQueryParser.class.getName() + "]"); + } + } + + public void testRegisterHunspellDictionary() throws Exception { + IndicesModule module = new IndicesModule(Settings.EMPTY); + InputStream aff = getClass().getResourceAsStream("/indices/analyze/conf_dir/hunspell/en_US/en_US.aff"); + InputStream dic = getClass().getResourceAsStream("/indices/analyze/conf_dir/hunspell/en_US/en_US.dic"); + Dictionary dictionary = new Dictionary(aff, dic); + module.registerHunspellDictionary("foo", dictionary); + assertMapInstanceBinding(module, String.class, Dictionary.class, Collections.singletonMap("foo", dictionary)); + } + + public void testRegisterHunspellDictionaryDuplicate() { + IndicesModule module = new IndicesModule(Settings.EMPTY); + try { + module.registerQueryParser(TermQueryParser.class); + } catch (IllegalArgumentException e) { + assertEquals(e.getMessage(), "Can't register the same [query_parser] more than once for [" + TermQueryParser.class.getName() + "]"); + } + } + +} diff --git a/plugins/analysis-icu/src/test/java/org/elasticsearch/index/analysis/AnalysisTestUtils.java b/plugins/analysis-icu/src/test/java/org/elasticsearch/index/analysis/AnalysisTestUtils.java index dec0e5dd63293..eceee3ef404d6 100644 --- a/plugins/analysis-icu/src/test/java/org/elasticsearch/index/analysis/AnalysisTestUtils.java +++ b/plugins/analysis-icu/src/test/java/org/elasticsearch/index/analysis/AnalysisTestUtils.java @@ -30,7 +30,6 @@ import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexNameModule; import org.elasticsearch.index.settings.IndexSettingsModule; -import org.elasticsearch.indices.analysis.IndicesAnalysisModule; import org.elasticsearch.indices.analysis.IndicesAnalysisService; import static org.elasticsearch.common.settings.Settings.settingsBuilder; @@ -42,7 +41,7 @@ public static AnalysisService createAnalysisService(Settings settings) { Settings indexSettings = settingsBuilder().put(settings) .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) .build(); - Injector parentInjector = new ModulesBuilder().add(new SettingsModule(settings), new EnvironmentModule(new Environment(settings)), new IndicesAnalysisModule()).createInjector(); + Injector parentInjector = new ModulesBuilder().add(new SettingsModule(settings), new EnvironmentModule(new Environment(settings))).createInjector(); Injector injector = new ModulesBuilder().add( new IndexSettingsModule(index, indexSettings), new IndexNameModule(index), diff --git a/plugins/analysis-kuromoji/src/test/java/org/elasticsearch/index/analysis/KuromojiAnalysisTests.java b/plugins/analysis-kuromoji/src/test/java/org/elasticsearch/index/analysis/KuromojiAnalysisTests.java index 63a31effa4712..b39103bb239ea 100644 --- a/plugins/analysis-kuromoji/src/test/java/org/elasticsearch/index/analysis/KuromojiAnalysisTests.java +++ b/plugins/analysis-kuromoji/src/test/java/org/elasticsearch/index/analysis/KuromojiAnalysisTests.java @@ -35,7 +35,6 @@ import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexNameModule; import org.elasticsearch.index.settings.IndexSettingsModule; -import org.elasticsearch.indices.analysis.IndicesAnalysisModule; import org.elasticsearch.indices.analysis.IndicesAnalysisService; import org.elasticsearch.plugin.analysis.kuromoji.AnalysisKuromojiPlugin; import org.elasticsearch.test.ESTestCase; @@ -48,7 +47,10 @@ import java.nio.file.Files; import java.nio.file.Path; -import static org.hamcrest.Matchers.*; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.Matchers.notNullValue; /** */ @@ -211,8 +213,7 @@ public AnalysisService createAnalysisService() throws IOException { Index index = new Index("test"); Injector parentInjector = new ModulesBuilder().add(new SettingsModule(settings), - new EnvironmentModule(new Environment(settings)), - new IndicesAnalysisModule()) + new EnvironmentModule(new Environment(settings))) .createInjector(); AnalysisModule analysisModule = new AnalysisModule(settings, parentInjector.getInstance(IndicesAnalysisService.class)); diff --git a/plugins/analysis-phonetic/src/test/java/org/elasticsearch/index/analysis/SimplePhoneticAnalysisTests.java b/plugins/analysis-phonetic/src/test/java/org/elasticsearch/index/analysis/SimplePhoneticAnalysisTests.java index e501d06cf3e57..1467626dd7bc3 100644 --- a/plugins/analysis-phonetic/src/test/java/org/elasticsearch/index/analysis/SimplePhoneticAnalysisTests.java +++ b/plugins/analysis-phonetic/src/test/java/org/elasticsearch/index/analysis/SimplePhoneticAnalysisTests.java @@ -30,7 +30,6 @@ import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexNameModule; import org.elasticsearch.index.settings.IndexSettingsModule; -import org.elasticsearch.indices.analysis.IndicesAnalysisModule; import org.elasticsearch.indices.analysis.IndicesAnalysisService; import org.elasticsearch.test.ESTestCase; import org.hamcrest.MatcherAssert; @@ -59,8 +58,7 @@ private AnalysisService testSimpleConfiguration(Settings settings) { Index index = new Index("test"); Injector parentInjector = new ModulesBuilder().add(new SettingsModule(settings), - new EnvironmentModule(new Environment(settings)), - new IndicesAnalysisModule()).createInjector(); + new EnvironmentModule(new Environment(settings))).createInjector(); Injector injector = new ModulesBuilder().add( new IndexSettingsModule(index, settings), new IndexNameModule(index), diff --git a/plugins/analysis-smartcn/src/test/java/org/elasticsearch/index/analysis/SimpleSmartChineseAnalysisTests.java b/plugins/analysis-smartcn/src/test/java/org/elasticsearch/index/analysis/SimpleSmartChineseAnalysisTests.java index 7065615968294..0f5d300465a22 100644 --- a/plugins/analysis-smartcn/src/test/java/org/elasticsearch/index/analysis/SimpleSmartChineseAnalysisTests.java +++ b/plugins/analysis-smartcn/src/test/java/org/elasticsearch/index/analysis/SimpleSmartChineseAnalysisTests.java @@ -30,7 +30,6 @@ import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexNameModule; import org.elasticsearch.index.settings.IndexSettingsModule; -import org.elasticsearch.indices.analysis.IndicesAnalysisModule; import org.elasticsearch.indices.analysis.IndicesAnalysisService; import org.elasticsearch.test.ESTestCase; import org.hamcrest.MatcherAssert; @@ -51,7 +50,7 @@ public void testDefaultsIcuAnalysis() { .put("path.home", createTempDir()) .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) .build(); - Injector parentInjector = new ModulesBuilder().add(new SettingsModule(EMPTY_SETTINGS), new EnvironmentModule(new Environment(settings)), new IndicesAnalysisModule()).createInjector(); + Injector parentInjector = new ModulesBuilder().add(new SettingsModule(EMPTY_SETTINGS), new EnvironmentModule(new Environment(settings))).createInjector(); Injector injector = new ModulesBuilder().add( new IndexSettingsModule(index, settings), new IndexNameModule(index), diff --git a/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/PolishAnalysisTests.java b/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/PolishAnalysisTests.java index a6a60d59a7d58..b17f3a12e3a23 100644 --- a/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/PolishAnalysisTests.java +++ b/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/PolishAnalysisTests.java @@ -34,7 +34,6 @@ import org.elasticsearch.index.analysis.pl.PolishAnalysisBinderProcessor; import org.elasticsearch.index.analysis.pl.PolishStemTokenFilterFactory; import org.elasticsearch.index.settings.IndexSettingsModule; -import org.elasticsearch.indices.analysis.IndicesAnalysisModule; import org.elasticsearch.indices.analysis.IndicesAnalysisService; import org.elasticsearch.test.ESTestCase; import org.hamcrest.MatcherAssert; @@ -56,7 +55,7 @@ public void testDefaultsPolishAnalysis() { .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) .build(); - Injector parentInjector = new ModulesBuilder().add(new SettingsModule(EMPTY_SETTINGS), new EnvironmentModule(new Environment(settings)), new IndicesAnalysisModule()).createInjector(); + Injector parentInjector = new ModulesBuilder().add(new SettingsModule(EMPTY_SETTINGS), new EnvironmentModule(new Environment(settings))).createInjector(); Injector injector = new ModulesBuilder().add( new IndexSettingsModule(index, settings), new IndexNameModule(index), diff --git a/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/SimplePolishTokenFilterTests.java b/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/SimplePolishTokenFilterTests.java index 2e1638836af95..70465417c6e80 100644 --- a/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/SimplePolishTokenFilterTests.java +++ b/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/SimplePolishTokenFilterTests.java @@ -36,7 +36,6 @@ import org.elasticsearch.index.IndexNameModule; import org.elasticsearch.index.analysis.pl.PolishAnalysisBinderProcessor; import org.elasticsearch.index.settings.IndexSettingsModule; -import org.elasticsearch.indices.analysis.IndicesAnalysisModule; import org.elasticsearch.indices.analysis.IndicesAnalysisService; import org.elasticsearch.test.ESTestCase; import org.junit.Test; @@ -102,7 +101,7 @@ private void testAnalyzer(String source, String... expected_terms) throws IOExce } private AnalysisService createAnalysisService(Index index, Settings settings) { - Injector parentInjector = new ModulesBuilder().add(new SettingsModule(settings), new EnvironmentModule(new Environment(settings)), new IndicesAnalysisModule()).createInjector(); + Injector parentInjector = new ModulesBuilder().add(new SettingsModule(settings), new EnvironmentModule(new Environment(settings))).createInjector(); Injector injector = new ModulesBuilder().add( new IndexSettingsModule(index, settings), new IndexNameModule(index),