From 7bd87e12a2a1c4a9ff9f986b42b9afefdd177964 Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Fri, 17 Feb 2012 15:03:52 +0200 Subject: [PATCH] Indices query should accept alias names, closes #1698. --- .../index/query/IndicesQueryParser.java | 18 ++++++++++++++++-- .../indices/query/IndicesQueriesRegistry.java | 6 ++++-- .../aliases/IndexAliasesServiceTests.java | 11 ++++++++++- .../percolator/PercolatorExecutorTests.java | 3 +++ .../query/SimpleIndexQueryParserTests.java | 11 ++++++++++- .../guice/IndexQueryParserModuleTests.java | 11 ++++++++++- .../plugin/IndexQueryParserPlugin2Tests.java | 11 ++++++++++- .../plugin/IndexQueryParserPluginTests.java | 11 ++++++++++- 8 files changed, 73 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/elasticsearch/index/query/IndicesQueryParser.java b/src/main/java/org/elasticsearch/index/query/IndicesQueryParser.java index 0cf6928e36a..3750282e03d 100644 --- a/src/main/java/org/elasticsearch/index/query/IndicesQueryParser.java +++ b/src/main/java/org/elasticsearch/index/query/IndicesQueryParser.java @@ -21,6 +21,9 @@ package org.elasticsearch.index.query; import com.google.common.collect.Sets; import org.apache.lucene.search.Query; +import org.elasticsearch.cluster.ClusterService; +import org.elasticsearch.cluster.metadata.MetaData; +import org.elasticsearch.common.Nullable; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.lucene.search.MatchNoDocsQuery; import org.elasticsearch.common.lucene.search.Queries; @@ -36,8 +39,12 @@ public class IndicesQueryParser implements QueryParser { public static final String NAME = "indices"; + @Nullable + private final ClusterService clusterService; + @Inject - public IndicesQueryParser() { + public IndicesQueryParser(@Nullable ClusterService clusterService) { + this.clusterService = clusterService; } @Override @@ -99,7 +106,14 @@ public class IndicesQueryParser implements QueryParser { if (indices.isEmpty()) { throw new QueryParsingException(parseContext.index(), "[indices] requires 'indices' element"); } - for (String index : indices) { + + String[] concreteIndices = indices.toArray(new String[indices.size()]); + if (clusterService != null) { + MetaData metaData = clusterService.state().metaData(); + concreteIndices = metaData.concreteIndices(indices.toArray(new String[indices.size()]), true, true); + } + + for (String index : concreteIndices) { if (Regex.simpleMatch(index, parseContext.index().name())) { return query; } diff --git a/src/main/java/org/elasticsearch/indices/query/IndicesQueriesRegistry.java b/src/main/java/org/elasticsearch/indices/query/IndicesQueriesRegistry.java index ccc0154f28b..c9a6dc62da5 100644 --- a/src/main/java/org/elasticsearch/indices/query/IndicesQueriesRegistry.java +++ b/src/main/java/org/elasticsearch/indices/query/IndicesQueriesRegistry.java @@ -21,6 +21,8 @@ package org.elasticsearch.indices.query; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Maps; +import org.elasticsearch.cluster.ClusterService; +import org.elasticsearch.common.Nullable; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.query.*; @@ -36,7 +38,7 @@ public class IndicesQueriesRegistry { private ImmutableMap filterParsers; @Inject - public IndicesQueriesRegistry(Settings settings) { + public IndicesQueriesRegistry(Settings settings, @Nullable ClusterService clusterService) { Map queryParsers = Maps.newHashMap(); addQueryParser(queryParsers, new TextQueryParser()); addQueryParser(queryParsers, new NestedQueryParser()); @@ -70,7 +72,7 @@ public class IndicesQueriesRegistry { addQueryParser(queryParsers, new FuzzyLikeThisQueryParser()); addQueryParser(queryParsers, new FuzzyLikeThisFieldQueryParser()); addQueryParser(queryParsers, new WrapperQueryParser()); - addQueryParser(queryParsers, new IndicesQueryParser()); + addQueryParser(queryParsers, new IndicesQueryParser(clusterService)); this.queryParsers = ImmutableMap.copyOf(queryParsers); Map filterParsers = Maps.newHashMap(); diff --git a/src/test/java/org/elasticsearch/test/unit/index/aliases/IndexAliasesServiceTests.java b/src/test/java/org/elasticsearch/test/unit/index/aliases/IndexAliasesServiceTests.java index 23a65f6d8ae..0b41403acfb 100644 --- a/src/test/java/org/elasticsearch/test/unit/index/aliases/IndexAliasesServiceTests.java +++ b/src/test/java/org/elasticsearch/test/unit/index/aliases/IndexAliasesServiceTests.java @@ -19,9 +19,12 @@ package org.elasticsearch.test.unit.index.aliases; +import org.elasticsearch.cluster.ClusterService; import org.elasticsearch.common.compress.CompressedString; +import org.elasticsearch.common.inject.AbstractModule; import org.elasticsearch.common.inject.Injector; import org.elasticsearch.common.inject.ModulesBuilder; +import org.elasticsearch.common.inject.util.Providers; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.SettingsModule; import org.elasticsearch.common.xcontent.ToXContent; @@ -67,7 +70,13 @@ public class IndexAliasesServiceTests { new ScriptModule(ImmutableSettings.Builder.EMPTY_SETTINGS), new SettingsModule(ImmutableSettings.Builder.EMPTY_SETTINGS), new IndexEngineModule(ImmutableSettings.Builder.EMPTY_SETTINGS), - new IndexCacheModule(ImmutableSettings.Builder.EMPTY_SETTINGS) + new IndexCacheModule(ImmutableSettings.Builder.EMPTY_SETTINGS), + new AbstractModule() { + @Override + protected void configure() { + bind(ClusterService.class).toProvider(Providers.of((ClusterService) null)); + } + } ).createInjector(); return injector.getInstance(IndexQueryParserService.class); } diff --git a/src/test/java/org/elasticsearch/test/unit/index/percolator/PercolatorExecutorTests.java b/src/test/java/org/elasticsearch/test/unit/index/percolator/PercolatorExecutorTests.java index cbc275db815..d132ed556ce 100644 --- a/src/test/java/org/elasticsearch/test/unit/index/percolator/PercolatorExecutorTests.java +++ b/src/test/java/org/elasticsearch/test/unit/index/percolator/PercolatorExecutorTests.java @@ -19,9 +19,11 @@ package org.elasticsearch.test.unit.index.percolator; +import org.elasticsearch.cluster.ClusterService; import org.elasticsearch.common.inject.AbstractModule; import org.elasticsearch.common.inject.Injector; import org.elasticsearch.common.inject.ModulesBuilder; +import org.elasticsearch.common.inject.util.Providers; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsModule; @@ -78,6 +80,7 @@ public class PercolatorExecutorTests { @Override protected void configure() { bind(PercolatorExecutor.class).asEagerSingleton(); + bind(ClusterService.class).toProvider(Providers.of((ClusterService) null)); } } ).createInjector(); diff --git a/src/test/java/org/elasticsearch/test/unit/index/query/SimpleIndexQueryParserTests.java b/src/test/java/org/elasticsearch/test/unit/index/query/SimpleIndexQueryParserTests.java index 0137c06a735..a12c31e9aee 100644 --- a/src/test/java/org/elasticsearch/test/unit/index/query/SimpleIndexQueryParserTests.java +++ b/src/test/java/org/elasticsearch/test/unit/index/query/SimpleIndexQueryParserTests.java @@ -23,8 +23,11 @@ import org.apache.lucene.index.Term; import org.apache.lucene.search.*; import org.apache.lucene.search.spans.*; import org.apache.lucene.util.NumericUtils; +import org.elasticsearch.cluster.ClusterService; +import org.elasticsearch.common.inject.AbstractModule; import org.elasticsearch.common.inject.Injector; import org.elasticsearch.common.inject.ModulesBuilder; +import org.elasticsearch.common.inject.util.Providers; import org.elasticsearch.common.lucene.search.*; import org.elasticsearch.common.lucene.search.function.BoostScoreFunction; import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery; @@ -90,7 +93,13 @@ public class SimpleIndexQueryParserTests { new IndexEngineModule(settings), new SimilarityModule(settings), new IndexQueryParserModule(settings), - new IndexNameModule(index) + new IndexNameModule(index), + new AbstractModule() { + @Override + protected void configure() { + bind(ClusterService.class).toProvider(Providers.of((ClusterService) null)); + } + } ).createInjector(); String mapping = copyToStringFromClasspath("/org/elasticsearch/test/unit/index/query/mapping.json"); diff --git a/src/test/java/org/elasticsearch/test/unit/index/query/guice/IndexQueryParserModuleTests.java b/src/test/java/org/elasticsearch/test/unit/index/query/guice/IndexQueryParserModuleTests.java index a1a3dcc5e68..d8ca138e145 100644 --- a/src/test/java/org/elasticsearch/test/unit/index/query/guice/IndexQueryParserModuleTests.java +++ b/src/test/java/org/elasticsearch/test/unit/index/query/guice/IndexQueryParserModuleTests.java @@ -19,8 +19,11 @@ package org.elasticsearch.test.unit.index.query.guice; +import org.elasticsearch.cluster.ClusterService; +import org.elasticsearch.common.inject.AbstractModule; import org.elasticsearch.common.inject.Injector; import org.elasticsearch.common.inject.ModulesBuilder; +import org.elasticsearch.common.inject.util.Providers; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsModule; import org.elasticsearch.index.Index; @@ -68,7 +71,13 @@ public class IndexQueryParserModuleTests { new IndexEngineModule(settings), new SimilarityModule(settings), new IndexQueryParserModule(settings), - new IndexNameModule(index) + new IndexNameModule(index), + new AbstractModule() { + @Override + protected void configure() { + bind(ClusterService.class).toProvider(Providers.of((ClusterService) null)); + } + } ).createInjector(); IndexQueryParserService indexQueryParserService = injector.getInstance(IndexQueryParserService.class); diff --git a/src/test/java/org/elasticsearch/test/unit/index/query/plugin/IndexQueryParserPlugin2Tests.java b/src/test/java/org/elasticsearch/test/unit/index/query/plugin/IndexQueryParserPlugin2Tests.java index fe8fe652603..6f98b2fd16b 100644 --- a/src/test/java/org/elasticsearch/test/unit/index/query/plugin/IndexQueryParserPlugin2Tests.java +++ b/src/test/java/org/elasticsearch/test/unit/index/query/plugin/IndexQueryParserPlugin2Tests.java @@ -19,8 +19,11 @@ package org.elasticsearch.test.unit.index.query.plugin; +import org.elasticsearch.cluster.ClusterService; +import org.elasticsearch.common.inject.AbstractModule; import org.elasticsearch.common.inject.Injector; import org.elasticsearch.common.inject.ModulesBuilder; +import org.elasticsearch.common.inject.util.Providers; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsModule; @@ -66,7 +69,13 @@ public class IndexQueryParserPlugin2Tests { new IndexEngineModule(settings), new SimilarityModule(settings), queryParserModule, - new IndexNameModule(index) + new IndexNameModule(index), + new AbstractModule() { + @Override + protected void configure() { + bind(ClusterService.class).toProvider(Providers.of((ClusterService) null)); + } + } ).createInjector(); IndexQueryParserService indexQueryParserService = injector.getInstance(IndexQueryParserService.class); diff --git a/src/test/java/org/elasticsearch/test/unit/index/query/plugin/IndexQueryParserPluginTests.java b/src/test/java/org/elasticsearch/test/unit/index/query/plugin/IndexQueryParserPluginTests.java index 9601590eff8..94621cc81e1 100644 --- a/src/test/java/org/elasticsearch/test/unit/index/query/plugin/IndexQueryParserPluginTests.java +++ b/src/test/java/org/elasticsearch/test/unit/index/query/plugin/IndexQueryParserPluginTests.java @@ -19,8 +19,11 @@ package org.elasticsearch.test.unit.index.query.plugin; +import org.elasticsearch.cluster.ClusterService; +import org.elasticsearch.common.inject.AbstractModule; import org.elasticsearch.common.inject.Injector; import org.elasticsearch.common.inject.ModulesBuilder; +import org.elasticsearch.common.inject.util.Providers; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsModule; @@ -75,7 +78,13 @@ public class IndexQueryParserPluginTests { new IndexEngineModule(settings), new SimilarityModule(settings), queryParserModule, - new IndexNameModule(index) + new IndexNameModule(index), + new AbstractModule() { + @Override + protected void configure() { + bind(ClusterService.class).toProvider(Providers.of((ClusterService) null)); + } + } ).createInjector(); IndexQueryParserService indexQueryParserService = injector.getInstance(IndexQueryParserService.class);