global node indices level queries to be created by guice

This commit is contained in:
Shay Banon 2012-09-20 11:54:46 +02:00
parent 4e19de1a08
commit f8e1291243
3 changed files with 130 additions and 74 deletions

View File

@ -19,12 +19,127 @@
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 java.util.Set;
public class IndicesQueriesModule extends AbstractModule {
private Set<Class<QueryParser>> queryParsersClasses = Sets.newHashSet();
private Set<QueryParser> queryParsers = Sets.newHashSet();
private Set<Class<FilterParser>> filterParsersClasses = Sets.newHashSet();
private Set<FilterParser> filterParsers = Sets.newHashSet();
public synchronized IndicesQueriesModule addQuery(Class<QueryParser> queryParser) {
queryParsersClasses.add(queryParser);
return this;
}
public synchronized IndicesQueriesModule addQuery(QueryParser queryParser) {
queryParsers.add(queryParser);
return this;
}
public synchronized IndicesQueriesModule addFilter(Class<FilterParser> filterParser) {
filterParsersClasses.add(filterParser);
return this;
}
public synchronized IndicesQueriesModule addFilter(FilterParser filterParser) {
filterParsers.add(filterParser);
return this;
}
@Override
protected void configure() {
bind(IndicesQueriesRegistry.class).asEagerSingleton();
Multibinder<QueryParser> qpBinders = Multibinder.newSetBinder(binder(), QueryParser.class);
for (Class<QueryParser> queryParser : queryParsersClasses) {
qpBinders.addBinding().to(queryParser).asEagerSingleton();
}
for (QueryParser queryParser : queryParsers) {
qpBinders.addBinding().toInstance(queryParser);
}
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(TopChildrenQueryParser.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(FieldQueryParser.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(CustomBoostFactorQueryParser.class).asEagerSingleton();
qpBinders.addBinding().to(CustomScoreQueryParser.class).asEagerSingleton();
qpBinders.addBinding().to(CustomFiltersScoreQueryParser.class).asEagerSingleton();
qpBinders.addBinding().to(SpanTermQueryParser.class).asEagerSingleton();
qpBinders.addBinding().to(SpanNotQueryParser.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(MoreLikeThisFieldQueryParser.class).asEagerSingleton();
qpBinders.addBinding().to(FuzzyLikeThisQueryParser.class).asEagerSingleton();
qpBinders.addBinding().to(FuzzyLikeThisFieldQueryParser.class).asEagerSingleton();
qpBinders.addBinding().to(WrapperQueryParser.class).asEagerSingleton();
qpBinders.addBinding().to(IndicesQueryParser.class).asEagerSingleton();
if (ShapesAvailability.JTS_AVAILABLE) {
qpBinders.addBinding().to(GeoShapeQueryParser.class).asEagerSingleton();
}
Multibinder<FilterParser> fpBinders = Multibinder.newSetBinder(binder(), FilterParser.class);
for (Class<FilterParser> filterParser : filterParsersClasses) {
fpBinders.addBinding().to(filterParser).asEagerSingleton();
}
for (FilterParser filterParser : filterParsers) {
fpBinders.addBinding().toInstance(filterParser);
}
fpBinders.addBinding().to(HasChildFilterParser.class).asEagerSingleton();
fpBinders.addBinding().to(HasParentFilterParser.class).asEagerSingleton();
fpBinders.addBinding().to(NestedFilterParser.class).asEagerSingleton();
fpBinders.addBinding().to(TypeFilterParser.class).asEagerSingleton();
fpBinders.addBinding().to(IdsFilterParser.class).asEagerSingleton();
fpBinders.addBinding().to(LimitFilterParser.class).asEagerSingleton();
fpBinders.addBinding().to(TermFilterParser.class).asEagerSingleton();
fpBinders.addBinding().to(TermsFilterParser.class).asEagerSingleton();
fpBinders.addBinding().to(RangeFilterParser.class).asEagerSingleton();
fpBinders.addBinding().to(NumericRangeFilterParser.class).asEagerSingleton();
fpBinders.addBinding().to(PrefixFilterParser.class).asEagerSingleton();
fpBinders.addBinding().to(ScriptFilterParser.class).asEagerSingleton();
fpBinders.addBinding().to(GeoDistanceFilterParser.class).asEagerSingleton();
fpBinders.addBinding().to(GeoDistanceRangeFilterParser.class).asEagerSingleton();
fpBinders.addBinding().to(GeoBoundingBoxFilterParser.class).asEagerSingleton();
fpBinders.addBinding().to(GeoPolygonFilterParser.class).asEagerSingleton();
if (ShapesAvailability.JTS_AVAILABLE) {
fpBinders.addBinding().to(GeoShapeFilterParser.class).asEagerSingleton();
}
fpBinders.addBinding().to(QueryFilterParser.class).asEagerSingleton();
fpBinders.addBinding().to(FQueryFilterParser.class).asEagerSingleton();
fpBinders.addBinding().to(BoolFilterParser.class).asEagerSingleton();
fpBinders.addBinding().to(AndFilterParser.class).asEagerSingleton();
fpBinders.addBinding().to(OrFilterParser.class).asEagerSingleton();
fpBinders.addBinding().to(NotFilterParser.class).asEagerSingleton();
fpBinders.addBinding().to(MatchAllFilterParser.class).asEagerSingleton();
fpBinders.addBinding().to(ExistsFilterParser.class).asEagerSingleton();
fpBinders.addBinding().to(MissingFilterParser.class).asEagerSingleton();
fpBinders.addBinding().to(IndicesFilterParser.class).asEagerSingleton();
fpBinders.addBinding().to(WrapperFilterParser.class).asEagerSingleton();
}
}

View File

@ -21,110 +21,49 @@ 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.geo.ShapesAvailability;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.query.*;
import org.elasticsearch.index.query.FilterParser;
import org.elasticsearch.index.query.QueryParser;
import java.util.Map;
import java.util.Set;
/**
*
*/
public class IndicesQueriesRegistry {
public class IndicesQueriesRegistry extends AbstractComponent {
private ImmutableMap<String, QueryParser> queryParsers;
private ImmutableMap<String, FilterParser> filterParsers;
@Inject
public IndicesQueriesRegistry(Settings settings, @Nullable ClusterService clusterService) {
public IndicesQueriesRegistry(Settings settings, Set<QueryParser> injectedQueryParsers, Set<FilterParser> injectedFilterParsers) {
super(settings);
Map<String, QueryParser> queryParsers = Maps.newHashMap();
addQueryParser(queryParsers, new MatchQueryParser());
addQueryParser(queryParsers, new MultiMatchQueryParser());
addQueryParser(queryParsers, new NestedQueryParser());
addQueryParser(queryParsers, new HasChildQueryParser());
addQueryParser(queryParsers, new HasParentQueryParser());
addQueryParser(queryParsers, new TopChildrenQueryParser());
addQueryParser(queryParsers, new DisMaxQueryParser());
addQueryParser(queryParsers, new IdsQueryParser());
addQueryParser(queryParsers, new MatchAllQueryParser());
addQueryParser(queryParsers, new QueryStringQueryParser(settings));
addQueryParser(queryParsers, new BoostingQueryParser());
addQueryParser(queryParsers, new BoolQueryParser(settings));
addQueryParser(queryParsers, new TermQueryParser());
addQueryParser(queryParsers, new TermsQueryParser());
addQueryParser(queryParsers, new FuzzyQueryParser());
addQueryParser(queryParsers, new FieldQueryParser(settings));
addQueryParser(queryParsers, new RangeQueryParser());
addQueryParser(queryParsers, new PrefixQueryParser());
addQueryParser(queryParsers, new WildcardQueryParser());
addQueryParser(queryParsers, new FilteredQueryParser());
addQueryParser(queryParsers, new ConstantScoreQueryParser());
addQueryParser(queryParsers, new CustomBoostFactorQueryParser());
addQueryParser(queryParsers, new CustomScoreQueryParser());
addQueryParser(queryParsers, new CustomFiltersScoreQueryParser());
addQueryParser(queryParsers, new SpanTermQueryParser());
addQueryParser(queryParsers, new SpanNotQueryParser());
addQueryParser(queryParsers, new SpanFirstQueryParser());
addQueryParser(queryParsers, new SpanNearQueryParser());
addQueryParser(queryParsers, new SpanOrQueryParser());
addQueryParser(queryParsers, new MoreLikeThisQueryParser());
addQueryParser(queryParsers, new MoreLikeThisFieldQueryParser());
addQueryParser(queryParsers, new FuzzyLikeThisQueryParser());
addQueryParser(queryParsers, new FuzzyLikeThisFieldQueryParser());
addQueryParser(queryParsers, new WrapperQueryParser());
addQueryParser(queryParsers, new IndicesQueryParser(clusterService));
if (ShapesAvailability.JTS_AVAILABLE) {
addQueryParser(queryParsers, new GeoShapeQueryParser());
for (QueryParser queryParser : injectedQueryParsers) {
addQueryParser(queryParsers, queryParser);
}
this.queryParsers = ImmutableMap.copyOf(queryParsers);
Map<String, FilterParser> filterParsers = Maps.newHashMap();
addFilterParser(filterParsers, new HasChildFilterParser());
addFilterParser(filterParsers, new HasParentFilterParser());
addFilterParser(filterParsers, new NestedFilterParser());
addFilterParser(filterParsers, new TypeFilterParser());
addFilterParser(filterParsers, new IdsFilterParser());
addFilterParser(filterParsers, new LimitFilterParser());
addFilterParser(filterParsers, new TermFilterParser());
addFilterParser(filterParsers, new TermsFilterParser());
addFilterParser(filterParsers, new RangeFilterParser());
addFilterParser(filterParsers, new NumericRangeFilterParser());
addFilterParser(filterParsers, new PrefixFilterParser());
addFilterParser(filterParsers, new ScriptFilterParser());
addFilterParser(filterParsers, new GeoDistanceFilterParser());
addFilterParser(filterParsers, new GeoDistanceRangeFilterParser());
addFilterParser(filterParsers, new GeoBoundingBoxFilterParser());
addFilterParser(filterParsers, new GeoPolygonFilterParser());
if (ShapesAvailability.JTS_AVAILABLE) {
addFilterParser(filterParsers, new GeoShapeFilterParser());
for (FilterParser filterParser : injectedFilterParsers) {
addFilterParser(filterParsers, filterParser);
}
addFilterParser(filterParsers, new QueryFilterParser());
addFilterParser(filterParsers, new FQueryFilterParser());
addFilterParser(filterParsers, new BoolFilterParser());
addFilterParser(filterParsers, new AndFilterParser());
addFilterParser(filterParsers, new OrFilterParser());
addFilterParser(filterParsers, new NotFilterParser());
addFilterParser(filterParsers, new MatchAllFilterParser());
addFilterParser(filterParsers, new ExistsFilterParser());
addFilterParser(filterParsers, new MissingFilterParser());
addFilterParser(filterParsers, new IndicesFilterParser(clusterService));
addFilterParser(filterParsers, new WrapperFilterParser());
this.filterParsers = ImmutableMap.copyOf(filterParsers);
}
/**
* Adds a global query parser.
*/
public void addQueryParser(QueryParser queryParser) {
public synchronized void addQueryParser(QueryParser queryParser) {
Map<String, QueryParser> queryParsers = Maps.newHashMap(this.queryParsers);
addQueryParser(queryParsers, queryParser);
this.queryParsers = ImmutableMap.copyOf(queryParsers);
}
public void addFilterParser(FilterParser filterParser) {
public synchronized void addFilterParser(FilterParser filterParser) {
Map<String, FilterParser> filterParsers = Maps.newHashMap(this.filterParsers);
addFilterParser(filterParsers, filterParser);
this.filterParsers = ImmutableMap.copyOf(filterParsers);

View File

@ -42,6 +42,7 @@ import org.elasticsearch.index.query.IndexQueryParserService;
import org.elasticsearch.index.settings.IndexSettingsModule;
import org.elasticsearch.index.similarity.SimilarityModule;
import org.elasticsearch.indices.InvalidAliasNameException;
import org.elasticsearch.indices.query.IndicesQueriesModule;
import org.elasticsearch.script.ScriptModule;
import org.testng.annotations.Test;
@ -62,6 +63,7 @@ public class IndexAliasesServiceTests {
public static IndexQueryParserService newIndexQueryParserService() {
Injector injector = new ModulesBuilder().add(
new IndicesQueriesModule(),
new IndexSettingsModule(new Index("test"), ImmutableSettings.Builder.EMPTY_SETTINGS),
new IndexNameModule(new Index("test")),
new IndexQueryParserModule(ImmutableSettings.Builder.EMPTY_SETTINGS),