From babfea0ab7a1114f35cc3f3ea76da26b0882d4ff Mon Sep 17 00:00:00 2001 From: javanna Date: Thu, 13 Aug 2015 17:47:47 +0200 Subject: [PATCH] Query refactoring: fixed some leftover generics warnings Leftover after adding the query builder type to QueryParser. getBuilderPrototype is better typed now and doesn't require unchecked cast anymore. Fixed also some Tuple usage without types. --- .../index/query/IndexQueryParserService.java | 7 ++++--- .../indices/query/IndicesQueriesRegistry.java | 10 +++++----- .../index/query/BaseQueryTestCase.java | 14 +++++++------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/index/query/IndexQueryParserService.java b/core/src/main/java/org/elasticsearch/index/query/IndexQueryParserService.java index 5be792bba5f..4941ab8cf8b 100644 --- a/core/src/main/java/org/elasticsearch/index/query/IndexQueryParserService.java +++ b/core/src/main/java/org/elasticsearch/index/query/IndexQueryParserService.java @@ -116,7 +116,9 @@ public class IndexQueryParserService extends AbstractIndexComponent { return this.queryStringLenient; } - public QueryParser queryParser(String name) { + //norelease we might want to get rid of this as it was temporarily introduced for our default doToQuery impl + //seems to be used only in tests + public QueryParser> queryParser(String name) { return indicesQueriesRegistry.queryParsers().get(name); } @@ -233,8 +235,7 @@ public class IndexQueryParserService extends AbstractIndexComponent { @Nullable public QueryBuilder parseInnerQueryBuilder(QueryParseContext parseContext) throws IOException { parseContext.parseFieldMatcher(parseFieldMatcher); - QueryBuilder query = parseContext.parseInnerQueryBuilder(); - return query; + return parseContext.parseInnerQueryBuilder(); } @Nullable diff --git a/core/src/main/java/org/elasticsearch/indices/query/IndicesQueriesRegistry.java b/core/src/main/java/org/elasticsearch/indices/query/IndicesQueriesRegistry.java index 925eaa826e6..4daa2dc3ab2 100644 --- a/core/src/main/java/org/elasticsearch/indices/query/IndicesQueriesRegistry.java +++ b/core/src/main/java/org/elasticsearch/indices/query/IndicesQueriesRegistry.java @@ -35,13 +35,13 @@ import java.util.Set; public class IndicesQueriesRegistry extends AbstractComponent { - private ImmutableMap queryParsers; + private ImmutableMap>> queryParsers; @Inject - public IndicesQueriesRegistry(Settings settings, Set injectedQueryParsers, NamedWriteableRegistry namedWriteableRegistry) { + public IndicesQueriesRegistry(Settings settings, Set>> injectedQueryParsers, NamedWriteableRegistry namedWriteableRegistry) { super(settings); - Map queryParsers = Maps.newHashMap(); - for (QueryParser queryParser : injectedQueryParsers) { + Map>> queryParsers = Maps.newHashMap(); + for (QueryParser> queryParser : injectedQueryParsers) { for (String name : queryParser.names()) { queryParsers.put(name, queryParser); } @@ -56,7 +56,7 @@ public class IndicesQueriesRegistry extends AbstractComponent { /** * Returns all the registered query parsers */ - public ImmutableMap queryParsers() { + public ImmutableMap>> queryParsers() { return queryParsers; } } \ No newline at end of file diff --git a/core/src/test/java/org/elasticsearch/index/query/BaseQueryTestCase.java b/core/src/test/java/org/elasticsearch/index/query/BaseQueryTestCase.java index 1f267c6dc43..cfb5b9fb477 100644 --- a/core/src/test/java/org/elasticsearch/index/query/BaseQueryTestCase.java +++ b/core/src/test/java/org/elasticsearch/index/query/BaseQueryTestCase.java @@ -244,9 +244,9 @@ public abstract class BaseQueryTestCase> ext try (BytesStreamOutput output = new BytesStreamOutput()) { firstQuery.writeTo(output); try (StreamInput in = new NamedWriteableAwareStreamInput(StreamInput.wrap(output.bytes()), namedWriteableRegistry)) { - QueryBuilder prototype = queryParserService.queryParser(firstQuery.getWriteableName()).getBuilderPrototype(); @SuppressWarnings("unchecked") - QB secondQuery = (QB)prototype.readFrom(in); + QueryParser queryParser = (QueryParser)queryParserService.queryParser(firstQuery.getWriteableName()); + QB secondQuery = queryParser.getBuilderPrototype().readFrom(in); //query _name never should affect the result of toQuery, we randomly set it to make sure if (randomBoolean()) { secondQuery.queryName(secondQuery.queryName() == null ? randomAsciiOfLengthBetween(1, 30) : secondQuery.queryName() + randomAsciiOfLengthBetween(1, 10)); @@ -269,7 +269,8 @@ public abstract class BaseQueryTestCase> ext /** * Few queries allow you to set the boost and queryName but don't do anything with it. This method allows - * to disable boost and queryName related tests for those queries. + * to disable boost and queryName related tests for those queries. Those queries are easy to identify: their parsers + * don't parse `boost` and `_name` as they don't apply to the specific query e.g. filter query or wrapper query */ protected boolean supportsBoostAndQueryName() { return true; @@ -328,8 +329,7 @@ public abstract class BaseQueryTestCase> ext * @return a new {@link QueryParseContext} based on the base test index and queryParserService */ protected static QueryParseContext createParseContext() { - QueryParseContext parseContext = createShardContext().parseContext(); - return parseContext; + return createShardContext().parseContext(); } protected static void assertQueryHeader(XContentParser parser, String expectedParserName) throws IOException { @@ -410,7 +410,7 @@ public abstract class BaseQueryTestCase> ext protected static Tuple getRandomFieldNameAndValue() { // if no type is set then return random field name and value if (currentTypes == null || currentTypes.length == 0) { - return new Tuple(randomAsciiOfLengthBetween(1, 10), randomAsciiOfLengthBetween(1, 50)); + return new Tuple(randomAsciiOfLengthBetween(1, 10), randomAsciiOfLengthBetween(1, 50)); } // mapped fields String fieldName = randomFrom(mappedFieldNames); @@ -437,7 +437,7 @@ public abstract class BaseQueryTestCase> ext if (randomBoolean()) { fieldName = randomAsciiOfLengthBetween(1, 10); } - return new Tuple(fieldName, value); + return new Tuple<>(fieldName, value); } protected static Fuzziness randomFuzziness(String fieldName) {