Skip to content

Commit

Permalink
move to use guice to register also the default query parsers and filters
Browse files Browse the repository at this point in the history
  • Loading branch information
kimchy committed Sep 12, 2010
1 parent 2442e1f commit 77a7938
Show file tree
Hide file tree
Showing 16 changed files with 273 additions and 448 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public void processXContentQueryFilter(String name, Class<? extends XContentFilt

public IndexQueryParserModule(Settings settings) {
this.settings = settings;
this.processors.add(new DefaultQueryProcessors());
}

public IndexQueryParserModule addProcessor(QueryParsersProcessor processor) {
Expand Down Expand Up @@ -216,4 +217,50 @@ public IndexQueryParserModule addProcessor(QueryParsersProcessor processor) {

bind(IndexQueryParserService.class).asEagerSingleton();
}

private static class DefaultQueryProcessors extends QueryParsersProcessor {
@Override public void processXContentQueryParsers(XContentQueryParsersBindings bindings) {
bindings.processXContentQueryParser(DisMaxQueryParser.NAME, DisMaxQueryParser.class);
bindings.processXContentQueryParser(MatchAllQueryParser.NAME, MatchAllQueryParser.class);
bindings.processXContentQueryParser(QueryStringQueryParser.NAME, QueryStringQueryParser.class);
bindings.processXContentQueryParser(BoolQueryParser.NAME, BoolQueryParser.class);
bindings.processXContentQueryParser(TermQueryParser.NAME, TermQueryParser.class);
bindings.processXContentQueryParser(FuzzyQueryParser.NAME, FuzzyQueryParser.class);
bindings.processXContentQueryParser(FieldQueryParser.NAME, FieldQueryParser.class);
bindings.processXContentQueryParser(RangeQueryParser.NAME, RangeQueryParser.class);
bindings.processXContentQueryParser(PrefixQueryParser.NAME, PrefixQueryParser.class);
bindings.processXContentQueryParser(WildcardQueryParser.NAME, WildcardQueryParser.class);
bindings.processXContentQueryParser(FilteredQueryParser.NAME, FilteredQueryParser.class);
bindings.processXContentQueryParser(ConstantScoreQueryParser.NAME, ConstantScoreQueryParser.class);
bindings.processXContentQueryParser(CustomBoostFactorQueryParser.NAME, CustomBoostFactorQueryParser.class);
bindings.processXContentQueryParser(CustomScoreQueryParser.NAME, CustomScoreQueryParser.class);
bindings.processXContentQueryParser(SpanTermQueryParser.NAME, SpanTermQueryParser.class);
bindings.processXContentQueryParser(SpanNotQueryParser.NAME, SpanNotQueryParser.class);
bindings.processXContentQueryParser(SpanFirstQueryParser.NAME, SpanFirstQueryParser.class);
bindings.processXContentQueryParser(SpanNearQueryParser.NAME, SpanNearQueryParser.class);
bindings.processXContentQueryParser(SpanOrQueryParser.NAME, SpanOrQueryParser.class);
bindings.processXContentQueryParser(MoreLikeThisQueryParser.NAME, MoreLikeThisQueryParser.class);
bindings.processXContentQueryParser(MoreLikeThisFieldQueryParser.NAME, MoreLikeThisFieldQueryParser.class);
bindings.processXContentQueryParser(FuzzyLikeThisQueryParser.NAME, FuzzyLikeThisQueryParser.class);
bindings.processXContentQueryParser(FuzzyLikeThisFieldQueryParser.NAME, FuzzyLikeThisFieldQueryParser.class);
}

@Override public void processXContentFilterParsers(XContentFilterParsersBindings bindings) {
bindings.processXContentQueryFilter(TermFilterParser.NAME, TermFilterParser.class);
bindings.processXContentQueryFilter(TermsFilterParser.NAME, TermsFilterParser.class);
bindings.processXContentQueryFilter(RangeFilterParser.NAME, RangeFilterParser.class);
bindings.processXContentQueryFilter(PrefixFilterParser.NAME, PrefixFilterParser.class);
bindings.processXContentQueryFilter(ScriptFilterParser.NAME, ScriptFilterParser.class);
bindings.processXContentQueryFilter(GeoDistanceFilterParser.NAME, GeoDistanceFilterParser.class);
bindings.processXContentQueryFilter(GeoBoundingBoxFilterParser.NAME, GeoBoundingBoxFilterParser.class);
bindings.processXContentQueryFilter(GeoPolygonFilterParser.NAME, GeoPolygonFilterParser.class);
bindings.processXContentQueryFilter(QueryFilterParser.NAME, QueryFilterParser.class);
bindings.processXContentQueryFilter(FQueryFilterParser.NAME, FQueryFilterParser.class);
bindings.processXContentQueryFilter(BoolFilterParser.NAME, BoolFilterParser.class);
bindings.processXContentQueryFilter(AndFilterParser.NAME, AndFilterParser.class);
bindings.processXContentQueryFilter(OrFilterParser.NAME, OrFilterParser.class);
bindings.processXContentQueryFilter(NotFilterParser.NAME, NotFilterParser.class);
bindings.processXContentQueryFilter(MatchAllFilterParser.NAME, MatchAllFilterParser.class);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,7 @@
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.AbstractIndexComponent;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.analysis.AnalysisService;
import org.elasticsearch.index.cache.IndexCache;
import org.elasticsearch.index.engine.IndexEngine;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.query.xcontent.XContentIndexQueryParser;
import org.elasticsearch.index.settings.IndexSettings;
import org.elasticsearch.index.similarity.SimilarityService;
import org.elasticsearch.script.ScriptService;

import javax.annotation.Nullable;
import java.util.Map;
Expand All @@ -53,15 +46,7 @@ public static final class Defaults {

private final Map<String, IndexQueryParser> indexQueryParsers;

public IndexQueryParserService(Index index, MapperService mapperService, IndexCache indexCache, IndexEngine indexEngine, AnalysisService analysisService) {
this(index, EMPTY_SETTINGS, new ScriptService(EMPTY_SETTINGS), mapperService, indexCache, indexEngine, analysisService, null, null);
}

@Inject public IndexQueryParserService(Index index, @IndexSettings Settings indexSettings,
ScriptService scriptService,
MapperService mapperService, IndexCache indexCache,
IndexEngine indexEngine, AnalysisService analysisService,
@Nullable SimilarityService similarityService,
@Nullable Map<String, IndexQueryParserFactory> indexQueryParsersFactories) {
super(index, indexSettings);
Map<String, Settings> queryParserGroupSettings;
Expand All @@ -81,11 +66,6 @@ public IndexQueryParserService(Index index, MapperService mapperService, IndexCa
qparsers.put(qparserName, entry.getValue().create(qparserName, qparserSettings));
}
}
if (!qparsers.containsKey(Defaults.DEFAULT)) {
IndexQueryParser defaultQueryParser = new XContentIndexQueryParser(index, indexSettings, scriptService, mapperService, indexCache, indexEngine, analysisService, similarityService, null, null, Defaults.DEFAULT, null);
qparsers.put(Defaults.DEFAULT, defaultQueryParser);
}

indexQueryParsers = ImmutableMap.copyOf(qparsers);

defaultIndexQueryParser = indexQueryParser(Defaults.DEFAULT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@
*/
public class BoolFilterParser extends AbstractIndexComponent implements XContentFilterParser {

public static final String NAME = "bool";

@Inject public BoolFilterParser(Index index, @IndexSettings Settings settings) {
super(index, settings);
}

@Override public String[] names() {
return new String[]{"bool"};
return new String[]{NAME};
}

@Override public Filter parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@
*/
public class BoolQueryParser extends AbstractIndexComponent implements XContentQueryParser {

public static final String NAME = "bool";

@Inject public BoolQueryParser(Index index, @IndexSettings Settings settings) {
super(index, settings);
}

@Override public String[] names() {
return new String[]{"bool"};
return new String[]{NAME};
}

@Override public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.lucene.search.FuzzyLikeThisQuery;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.AbstractIndexComponent;
Expand Down Expand Up @@ -53,7 +54,7 @@ public class FuzzyLikeThisFieldQueryParser extends AbstractIndexComponent implem

public static final String NAME = "flt_field";

public FuzzyLikeThisFieldQueryParser(Index index, @IndexSettings Settings indexSettings) {
@Inject public FuzzyLikeThisFieldQueryParser(Index index, @IndexSettings Settings indexSettings) {
super(index, indexSettings);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.lucene.search.FuzzyLikeThisQuery;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.collect.Lists;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.AbstractIndexComponent;
Expand Down Expand Up @@ -51,7 +52,7 @@ public class FuzzyLikeThisQueryParser extends AbstractIndexComponent implements

public static final String NAME = "flt";

public FuzzyLikeThisQueryParser(Index index, @IndexSettings Settings indexSettings) {
@Inject public FuzzyLikeThisQueryParser(Index index, @IndexSettings Settings indexSettings) {
super(index, indexSettings);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.lucene.index.Term;
import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.AbstractIndexComponent;
Expand All @@ -41,7 +42,7 @@ public class FuzzyQueryParser extends AbstractIndexComponent implements XContent

public static final String NAME = "fuzzy";

public FuzzyQueryParser(Index index, @IndexSettings Settings indexSettings) {
@Inject public FuzzyQueryParser(Index index, @IndexSettings Settings indexSettings) {
super(index, indexSettings);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.lucene.search.Query;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.Sets;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.lucene.search.MoreLikeThisQuery;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentParser;
Expand All @@ -43,7 +44,7 @@ public class MoreLikeThisFieldQueryParser extends AbstractIndexComponent impleme

public static final String NAME = "mlt_field";

public MoreLikeThisFieldQueryParser(Index index, @IndexSettings Settings indexSettings) {
@Inject public MoreLikeThisFieldQueryParser(Index index, @IndexSettings Settings indexSettings) {
super(index, indexSettings);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.lucene.search.Query;
import org.elasticsearch.common.collect.Lists;
import org.elasticsearch.common.collect.Sets;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.lucene.search.MoreLikeThisQuery;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentParser;
Expand All @@ -42,7 +43,7 @@ public class MoreLikeThisQueryParser extends AbstractIndexComponent implements X

public static final String NAME = "mlt";

public MoreLikeThisQueryParser(Index index, @IndexSettings Settings indexSettings) {
@Inject public MoreLikeThisQueryParser(Index index, @IndexSettings Settings indexSettings) {
super(index, indexSettings);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,7 @@ public class QueryParseContext {

private final Index index;

private final ScriptService scriptService;

private final MapperService mapperService;

private final SimilarityService similarityService;

private final IndexCache indexCache;

private final IndexEngine indexEngine;

private final XContentQueryParserRegistry queryParserRegistry;
XContentIndexQueryParser indexQueryParser;

private final Map<String, Filter> namedFilters = Maps.newHashMap();

Expand All @@ -70,17 +60,9 @@ public class QueryParseContext {

private XContentParser parser;

public QueryParseContext(Index index, XContentQueryParserRegistry queryParserRegistry,
ScriptService scriptService,
MapperService mapperService, SimilarityService similarityService,
IndexCache indexCache, IndexEngine indexEngine) {
public QueryParseContext(Index index, XContentIndexQueryParser indexQueryParser) {
this.index = index;
this.queryParserRegistry = queryParserRegistry;
this.scriptService = scriptService;
this.mapperService = mapperService;
this.similarityService = similarityService;
this.indexCache = indexCache;
this.indexEngine = indexEngine;
this.indexQueryParser = indexQueryParser;
}

public void reset(XContentParser jp) {
Expand All @@ -93,27 +75,27 @@ public XContentParser parser() {
}

public ScriptService scriptService() {
return scriptService;
return indexQueryParser.scriptService;
}

public MapperService mapperService() {
return mapperService;
return indexQueryParser.mapperService;
}

public IndexEngine indexEngine() {
return this.indexEngine;
return indexQueryParser.indexEngine;
}

@Nullable public SimilarityService similarityService() {
return this.similarityService;
return indexQueryParser.similarityService;
}

public Similarity searchSimilarity() {
return similarityService != null ? similarityService.defaultSearchSimilarity() : null;
return indexQueryParser.similarityService != null ? indexQueryParser.similarityService.defaultSearchSimilarity() : null;
}

public IndexCache indexCache() {
return indexCache;
return indexQueryParser.indexCache;
}

public MapperQueryParser queryParser(QueryParserSettings settings) {
Expand All @@ -127,7 +109,7 @@ public MultiFieldMapperQueryParser queryParser(MultiFieldQueryParserSettings set
}

public Filter cacheFilterIfPossible(Filter filter) {
return indexCache.filter().cache(filter);
return indexQueryParser.indexCache.filter().cache(filter);
}

public void addNamedFilter(String name, Filter filter) {
Expand Down Expand Up @@ -155,7 +137,7 @@ public Query parseInnerQuery() throws IOException, QueryParsingException {
token = parser.nextToken();
assert token == XContentParser.Token.START_OBJECT || token == XContentParser.Token.START_ARRAY;

XContentQueryParser queryParser = queryParserRegistry.queryParser(queryName);
XContentQueryParser queryParser = indexQueryParser.queryParser(queryName);
if (queryParser == null) {
throw new QueryParsingException(index, "No query parser registered for [" + queryName + "]");
}
Expand All @@ -181,7 +163,7 @@ public Filter parseInnerFilter() throws IOException, QueryParsingException {
token = parser.nextToken();
assert token == XContentParser.Token.START_OBJECT || token == XContentParser.Token.START_ARRAY;

XContentFilterParser filterParser = queryParserRegistry.filterParser(queryName);
XContentFilterParser filterParser = indexQueryParser.filterParser(queryName);
if (filterParser == null) {
throw new QueryParsingException(index, "No query parser registered for [" + queryName + "]");
}
Expand All @@ -194,7 +176,7 @@ public Filter parseInnerFilter() throws IOException, QueryParsingException {
}

public FieldMapper fieldMapper(String name) {
FieldMappers fieldMappers = mapperService.smartNameFieldMappers(name);
FieldMappers fieldMappers = indexQueryParser.mapperService.smartNameFieldMappers(name);
if (fieldMappers == null) {
return null;
}
Expand All @@ -210,6 +192,6 @@ public String indexName(String name) {
}

public MapperService.SmartNameFieldMappers smartFieldMappers(String name) {
return mapperService.smartName(name);
return indexQueryParser.mapperService.smartName(name);
}
}
Loading

0 comments on commit 77a7938

Please sign in to comment.