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.
This commit is contained in:
javanna 2015-08-13 17:47:47 +02:00 committed by Luca Cavanna
parent 135aab0092
commit babfea0ab7
3 changed files with 16 additions and 15 deletions

View File

@ -116,7 +116,9 @@ public class IndexQueryParserService extends AbstractIndexComponent {
return this.queryStringLenient; 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<? extends QueryBuilder<? extends QueryBuilder>> queryParser(String name) {
return indicesQueriesRegistry.queryParsers().get(name); return indicesQueriesRegistry.queryParsers().get(name);
} }
@ -233,8 +235,7 @@ public class IndexQueryParserService extends AbstractIndexComponent {
@Nullable @Nullable
public QueryBuilder parseInnerQueryBuilder(QueryParseContext parseContext) throws IOException { public QueryBuilder parseInnerQueryBuilder(QueryParseContext parseContext) throws IOException {
parseContext.parseFieldMatcher(parseFieldMatcher); parseContext.parseFieldMatcher(parseFieldMatcher);
QueryBuilder query = parseContext.parseInnerQueryBuilder(); return parseContext.parseInnerQueryBuilder();
return query;
} }
@Nullable @Nullable

View File

@ -35,13 +35,13 @@ import java.util.Set;
public class IndicesQueriesRegistry extends AbstractComponent { public class IndicesQueriesRegistry extends AbstractComponent {
private ImmutableMap<String, QueryParser> queryParsers; private ImmutableMap<String, QueryParser<? extends QueryBuilder<? extends QueryBuilder>>> queryParsers;
@Inject @Inject
public IndicesQueriesRegistry(Settings settings, Set<QueryParser> injectedQueryParsers, NamedWriteableRegistry namedWriteableRegistry) { public IndicesQueriesRegistry(Settings settings, Set<QueryParser<? extends QueryBuilder<? extends QueryBuilder>>> injectedQueryParsers, NamedWriteableRegistry namedWriteableRegistry) {
super(settings); super(settings);
Map<String, QueryParser> queryParsers = Maps.newHashMap(); Map<String, QueryParser<? extends QueryBuilder<? extends QueryBuilder>>> queryParsers = Maps.newHashMap();
for (QueryParser queryParser : injectedQueryParsers) { for (QueryParser<? extends QueryBuilder<? extends QueryBuilder>> queryParser : injectedQueryParsers) {
for (String name : queryParser.names()) { for (String name : queryParser.names()) {
queryParsers.put(name, queryParser); queryParsers.put(name, queryParser);
} }
@ -56,7 +56,7 @@ public class IndicesQueriesRegistry extends AbstractComponent {
/** /**
* Returns all the registered query parsers * Returns all the registered query parsers
*/ */
public ImmutableMap<String, QueryParser> queryParsers() { public ImmutableMap<String, QueryParser<? extends QueryBuilder<? extends QueryBuilder>>> queryParsers() {
return queryParsers; return queryParsers;
} }
} }

View File

@ -244,9 +244,9 @@ public abstract class BaseQueryTestCase<QB extends AbstractQueryBuilder<QB>> ext
try (BytesStreamOutput output = new BytesStreamOutput()) { try (BytesStreamOutput output = new BytesStreamOutput()) {
firstQuery.writeTo(output); firstQuery.writeTo(output);
try (StreamInput in = new NamedWriteableAwareStreamInput(StreamInput.wrap(output.bytes()), namedWriteableRegistry)) { try (StreamInput in = new NamedWriteableAwareStreamInput(StreamInput.wrap(output.bytes()), namedWriteableRegistry)) {
QueryBuilder<? extends QueryBuilder> prototype = queryParserService.queryParser(firstQuery.getWriteableName()).getBuilderPrototype();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
QB secondQuery = (QB)prototype.readFrom(in); QueryParser<QB> queryParser = (QueryParser<QB>)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 //query _name never should affect the result of toQuery, we randomly set it to make sure
if (randomBoolean()) { if (randomBoolean()) {
secondQuery.queryName(secondQuery.queryName() == null ? randomAsciiOfLengthBetween(1, 30) : secondQuery.queryName() + randomAsciiOfLengthBetween(1, 10)); secondQuery.queryName(secondQuery.queryName() == null ? randomAsciiOfLengthBetween(1, 30) : secondQuery.queryName() + randomAsciiOfLengthBetween(1, 10));
@ -269,7 +269,8 @@ public abstract class BaseQueryTestCase<QB extends AbstractQueryBuilder<QB>> ext
/** /**
* Few queries allow you to set the boost and queryName but don't do anything with it. This method allows * 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() { protected boolean supportsBoostAndQueryName() {
return true; return true;
@ -328,8 +329,7 @@ public abstract class BaseQueryTestCase<QB extends AbstractQueryBuilder<QB>> ext
* @return a new {@link QueryParseContext} based on the base test index and queryParserService * @return a new {@link QueryParseContext} based on the base test index and queryParserService
*/ */
protected static QueryParseContext createParseContext() { protected static QueryParseContext createParseContext() {
QueryParseContext parseContext = createShardContext().parseContext(); return createShardContext().parseContext();
return parseContext;
} }
protected static void assertQueryHeader(XContentParser parser, String expectedParserName) throws IOException { protected static void assertQueryHeader(XContentParser parser, String expectedParserName) throws IOException {
@ -410,7 +410,7 @@ public abstract class BaseQueryTestCase<QB extends AbstractQueryBuilder<QB>> ext
protected static Tuple<String, Object> getRandomFieldNameAndValue() { protected static Tuple<String, Object> getRandomFieldNameAndValue() {
// if no type is set then return random field name and value // if no type is set then return random field name and value
if (currentTypes == null || currentTypes.length == 0) { if (currentTypes == null || currentTypes.length == 0) {
return new Tuple(randomAsciiOfLengthBetween(1, 10), randomAsciiOfLengthBetween(1, 50)); return new Tuple<String, Object>(randomAsciiOfLengthBetween(1, 10), randomAsciiOfLengthBetween(1, 50));
} }
// mapped fields // mapped fields
String fieldName = randomFrom(mappedFieldNames); String fieldName = randomFrom(mappedFieldNames);
@ -437,7 +437,7 @@ public abstract class BaseQueryTestCase<QB extends AbstractQueryBuilder<QB>> ext
if (randomBoolean()) { if (randomBoolean()) {
fieldName = randomAsciiOfLengthBetween(1, 10); fieldName = randomAsciiOfLengthBetween(1, 10);
} }
return new Tuple(fieldName, value); return new Tuple<>(fieldName, value);
} }
protected static Fuzziness randomFuzziness(String fieldName) { protected static Fuzziness randomFuzziness(String fieldName) {