Indices query should accept alias names, closes #1698.

This commit is contained in:
Shay Banon 2012-02-17 15:03:52 +02:00
parent ff3ebe4a4b
commit 7bd87e12a2
8 changed files with 73 additions and 9 deletions

View File

@ -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;
}

View File

@ -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<String, FilterParser> filterParsers;
@Inject
public IndicesQueriesRegistry(Settings settings) {
public IndicesQueriesRegistry(Settings settings, @Nullable ClusterService clusterService) {
Map<String, QueryParser> 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<String, FilterParser> filterParsers = Maps.newHashMap();

View File

@ -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);
}

View File

@ -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();

View File

@ -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");

View File

@ -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);

View File

@ -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);

View File

@ -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);