From 927111c91dac5e3cef868eead3cfe8fd4075bee5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Thu, 29 Jun 2017 17:10:20 +0200 Subject: [PATCH] Remove QueryParseContext from parsing QueryBuilders (#25448) Currently QueryParseContext is only a thin wrapper around an XContentParser that adds little functionality of its own. I provides helpers for long deprecated field names which can be removed and two helper methods that can be made static and moved to other classes. This is a first step in helping to remove QueryParseContext entirely. --- .../cluster/metadata/AliasValidator.java | 6 +- .../index/query/AbstractQueryBuilder.java | 47 +++++++++++ .../index/query/BoolQueryBuilder.java | 28 +++---- .../index/query/BoostingQueryBuilder.java | 15 ++-- .../index/query/CommonTermsQueryBuilder.java | 6 +- .../query/ConstantScoreQueryBuilder.java | 8 +- .../index/query/DisMaxQueryBuilder.java | 8 +- .../index/query/ExistsQueryBuilder.java | 4 +- .../query/FieldMaskingSpanQueryBuilder.java | 6 +- .../index/query/FuzzyQueryBuilder.java | 5 +- .../query/GeoBoundingBoxQueryBuilder.java | 8 +- .../index/query/GeoDistanceQueryBuilder.java | 6 +- .../index/query/GeoPolygonQueryBuilder.java | 8 +- .../index/query/GeoShapeQueryBuilder.java | 4 +- .../index/query/IdsQueryBuilder.java | 7 +- .../index/query/InnerHitBuilder.java | 12 +-- .../index/query/MatchAllQueryBuilder.java | 8 +- .../index/query/MatchNoneQueryBuilder.java | 4 +- .../query/MatchPhrasePrefixQueryBuilder.java | 5 +- .../index/query/MatchPhraseQueryBuilder.java | 5 +- .../index/query/MatchQueryBuilder.java | 5 +- .../index/query/MoreLikeThisQueryBuilder.java | 19 ++--- .../index/query/MultiMatchQueryBuilder.java | 4 +- .../index/query/NestedQueryBuilder.java | 7 +- .../index/query/PrefixQueryBuilder.java | 6 +- .../index/query/QueryParseContext.java | 79 ------------------ .../index/query/QueryParser.java | 15 ++-- .../index/query/QueryStringQueryBuilder.java | 3 +- .../index/query/RangeQueryBuilder.java | 6 +- .../index/query/RegexpQueryBuilder.java | 5 +- .../index/query/ScriptQueryBuilder.java | 5 +- .../index/query/SimpleQueryStringBuilder.java | 4 +- .../query/SpanContainingQueryBuilder.java | 7 +- .../index/query/SpanFirstQueryBuilder.java | 6 +- .../query/SpanMultiTermQueryBuilder.java | 5 +- .../index/query/SpanNearQueryBuilder.java | 6 +- .../index/query/SpanNotQueryBuilder.java | 8 +- .../index/query/SpanOrQueryBuilder.java | 6 +- .../index/query/SpanTermQueryBuilder.java | 3 +- .../index/query/SpanWithinQueryBuilder.java | 8 +- .../index/query/TermQueryBuilder.java | 6 +- .../index/query/TermsQueryBuilder.java | 12 ++- .../index/query/TypeQueryBuilder.java | 5 +- .../index/query/WildcardQueryBuilder.java | 5 +- .../index/query/WrapperQueryBuilder.java | 7 +- .../functionscore/DecayFunctionParser.java | 4 +- .../FieldValueFactorFunctionBuilder.java | 4 +- .../FunctionScoreQueryBuilder.java | 18 ++-- .../RandomScoreFunctionBuilder.java | 5 +- .../functionscore/ScoreFunctionParser.java | 4 +- .../ScriptScoreFunctionBuilder.java | 5 +- .../elasticsearch/indices/IndicesService.java | 4 +- .../rest/action/RestActions.java | 31 ++++++- .../elasticsearch/search/SearchModule.java | 5 +- .../aggregations/AggregatorFactories.java | 4 +- .../adjacency/AdjacencyMatrixAggregator.java | 30 +++---- .../filter/FilterAggregationBuilder.java | 4 +- .../filters/FiltersAggregationBuilder.java | 8 +- .../SignificantTermsAggregationBuilder.java | 4 +- .../SignificantTextAggregationBuilder.java | 33 ++++---- .../tophits/TopHitsAggregationBuilder.java | 7 +- .../search/builder/SearchSourceBuilder.java | 17 ++-- .../search/collapse/CollapseBuilder.java | 4 +- .../highlight/AbstractHighlighterBuilder.java | 11 +-- .../subphase/highlight/HighlightBuilder.java | 12 +-- .../search/internal/AliasFilter.java | 4 +- .../search/rescore/QueryRescorerBuilder.java | 4 +- .../search/sort/FieldSortBuilder.java | 9 +- .../search/sort/GeoDistanceSortBuilder.java | 8 +- .../search/sort/ScoreSortBuilder.java | 7 +- .../search/sort/ScriptSortBuilder.java | 9 +- .../search/sort/SortBuilder.java | 22 +++-- .../suggest/phrase/PhraseSuggester.java | 6 +- .../search/MultiSearchRequestTests.java | 3 +- .../index/query/InnerHitBuilderTests.java | 3 +- .../index/query/QueryParseContextTests.java | 43 ++-------- .../query/QueryStringQueryBuilderTests.java | 6 +- .../FunctionScoreQueryBuilderTests.java | 6 +- .../index/query/plugin/DummyQueryBuilder.java | 5 +- .../rest/action/RestActionsTests.java | 82 +++++++++++++++++++ .../highlight/HighlightBuilderTests.java | 22 ++--- .../ShardSearchTransportRequestTests.java | 4 +- .../search/sort/AbstractSortTestCase.java | 10 +-- .../search/sort/FieldSortBuilderTests.java | 9 +- .../sort/GeoDistanceSortBuilderTests.java | 17 ++-- .../search/sort/ScoreSortBuilderTests.java | 12 +-- .../search/sort/ScriptSortBuilderTests.java | 20 ++--- .../search/sort/SortBuilderTests.java | 4 +- .../migration/migrate_6_0/rest.asciidoc | 6 ++ .../join/query/HasChildQueryBuilder.java | 10 +-- .../join/query/HasParentQueryBuilder.java | 8 +- .../join/query/ParentIdQueryBuilder.java | 4 +- .../percolator/PercolateQueryBuilder.java | 4 +- .../percolator/PercolatorFieldMapper.java | 4 +- .../PercolatorFieldMapperTests.java | 7 +- .../http/TestDeprecatedQueryBuilder.java | 5 +- .../test/AbstractQueryTestCase.java | 5 +- 97 files changed, 462 insertions(+), 562 deletions(-) create mode 100644 core/src/test/java/org/elasticsearch/rest/action/RestActionsTests.java diff --git a/core/src/main/java/org/elasticsearch/cluster/metadata/AliasValidator.java b/core/src/main/java/org/elasticsearch/cluster/metadata/AliasValidator.java index bce6e45c793..7df82adc2c6 100644 --- a/core/src/main/java/org/elasticsearch/cluster/metadata/AliasValidator.java +++ b/core/src/main/java/org/elasticsearch/cluster/metadata/AliasValidator.java @@ -30,13 +30,14 @@ import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.indices.InvalidAliasNameException; import java.io.IOException; import java.util.function.Function; +import static org.elasticsearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder; + /** * Validator for an alias, to be used before adding an alias to the index metadata * and make sure the alias is valid @@ -141,8 +142,7 @@ public class AliasValidator extends AbstractComponent { } private static void validateAliasFilter(XContentParser parser, QueryShardContext queryShardContext) throws IOException { - QueryParseContext queryParseContext = queryShardContext.newParseContext(parser); - QueryBuilder parseInnerQueryBuilder = queryParseContext.parseInnerQueryBuilder(); + QueryBuilder parseInnerQueryBuilder = parseInnerQueryBuilder(parser); QueryBuilder queryBuilder = QueryBuilder.rewriteQuery(parseInnerQueryBuilder, queryShardContext); queryBuilder.toFilter(queryShardContext); } diff --git a/core/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java index 72af5414763..0ed05e299c9 100644 --- a/core/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java @@ -31,8 +31,11 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.lucene.BytesRefs; import org.elasticsearch.common.xcontent.AbstractObjectParser; +import org.elasticsearch.common.xcontent.NamedXContentRegistry.UnknownNamedObjectException; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentLocation; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.XContentParser.Token; import org.elasticsearch.common.xcontent.XContentType; import java.io.IOException; @@ -286,6 +289,50 @@ public abstract class AbstractQueryBuilder> protected void extractInnerHitBuilders(Map innerHits) { } + /** + * Parses a query excluding the query element that wraps it + */ + public static QueryBuilder parseInnerQueryBuilder(XContentParser parser) throws IOException { + if (parser.currentToken() != XContentParser.Token.START_OBJECT) { + if (parser.nextToken() != XContentParser.Token.START_OBJECT) { + throw new ParsingException(parser.getTokenLocation(), "[_na] query malformed, must start with start_object"); + } + } + if (parser.nextToken() == XContentParser.Token.END_OBJECT) { + // we encountered '{}' for a query clause, it used to be supported, deprecated in 5.0 and removed in 6.0 + throw new IllegalArgumentException("query malformed, empty clause found at [" + parser.getTokenLocation() +"]"); + } + if (parser.currentToken() != XContentParser.Token.FIELD_NAME) { + throw new ParsingException(parser.getTokenLocation(), "[_na] query malformed, no field after start_object"); + } + String queryName = parser.currentName(); + // move to the next START_OBJECT + if (parser.nextToken() != XContentParser.Token.START_OBJECT) { + throw new ParsingException(parser.getTokenLocation(), "[" + queryName + "] query malformed, no start_object after query name"); + } + QueryBuilder result; + try { + // TODO what can we pass in here + result = parser.namedObject(QueryBuilder.class, queryName, null); + } catch (UnknownNamedObjectException e) { + // Preserve the error message from 5.0 until we have a compellingly better message so we don't break BWC. + // This intentionally doesn't include the causing exception because that'd change the "root_cause" of any unknown query errors + throw new ParsingException(new XContentLocation(e.getLineNumber(), e.getColumnNumber()), + "no [query] registered for [" + e.getName() + "]"); + } + //end_object of the specific query (e.g. match, multi_match etc.) element + if (parser.currentToken() != XContentParser.Token.END_OBJECT) { + throw new ParsingException(parser.getTokenLocation(), + "[" + queryName + "] malformed query, expected [END_OBJECT] but found [" + parser.currentToken() + "]"); + } + //end_object of the query object + if (parser.nextToken() != XContentParser.Token.END_OBJECT) { + throw new ParsingException(parser.getTokenLocation(), + "[" + queryName + "] malformed query, expected [END_OBJECT] but found [" + parser.currentToken() + "]"); + } + return result; + } + // Like Objects.requireNotNull(...) but instead throws a IllegalArgumentException protected static T requireValue(T value, String message) { if (value == null) { diff --git a/core/src/main/java/org/elasticsearch/index/query/BoolQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/BoolQueryBuilder.java index cab2aee910f..d20361419c7 100644 --- a/core/src/main/java/org/elasticsearch/index/query/BoolQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/BoolQueryBuilder.java @@ -275,11 +275,9 @@ public class BoolQueryBuilder extends AbstractQueryBuilder { builder.endArray(); } - public static BoolQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException, ParsingException { - XContentParser parser = parseContext.parser(); - + public static BoolQueryBuilder fromXContent(XContentParser parser) throws IOException, ParsingException { boolean adjustPureNegative = BoolQueryBuilder.ADJUST_PURE_NEGATIVE_DEFAULT; - float boost = AbstractQueryBuilder.DEFAULT_BOOST; + float boost = DEFAULT_BOOST; String minimumShouldMatch = null; final List mustClauses = new ArrayList<>(); @@ -293,22 +291,20 @@ public class BoolQueryBuilder extends AbstractQueryBuilder { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); - } else if (parseContext.isDeprecatedSetting(currentFieldName)) { - // skip } else if (token == XContentParser.Token.START_OBJECT) { switch (currentFieldName) { case MUST: - mustClauses.add(parseContext.parseInnerQueryBuilder()); + mustClauses.add(parseInnerQueryBuilder(parser)); break; case SHOULD: - shouldClauses.add(parseContext.parseInnerQueryBuilder()); + shouldClauses.add(parseInnerQueryBuilder(parser)); break; case FILTER: - filterClauses.add(parseContext.parseInnerQueryBuilder()); + filterClauses.add(parseInnerQueryBuilder(parser)); break; case MUST_NOT: case MUSTNOT: - mustNotClauses.add(parseContext.parseInnerQueryBuilder()); + mustNotClauses.add(parseInnerQueryBuilder(parser)); break; default: throw new ParsingException(parser.getTokenLocation(), "[bool] query does not support [" + currentFieldName + "]"); @@ -317,17 +313,17 @@ public class BoolQueryBuilder extends AbstractQueryBuilder { while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { switch (currentFieldName) { case MUST: - mustClauses.add(parseContext.parseInnerQueryBuilder()); + mustClauses.add(parseInnerQueryBuilder(parser)); break; case SHOULD: - shouldClauses.add(parseContext.parseInnerQueryBuilder()); + shouldClauses.add(parseInnerQueryBuilder(parser)); break; case FILTER: - filterClauses.add(parseContext.parseInnerQueryBuilder()); + filterClauses.add(parseInnerQueryBuilder(parser)); break; case MUST_NOT: case MUSTNOT: - mustNotClauses.add(parseContext.parseInnerQueryBuilder()); + mustNotClauses.add(parseInnerQueryBuilder(parser)); break; default: throw new ParsingException(parser.getTokenLocation(), "bool query does not support [" + currentFieldName + "]"); @@ -338,11 +334,11 @@ public class BoolQueryBuilder extends AbstractQueryBuilder { // ignore } else if (MINIMUM_SHOULD_MATCH.match(currentFieldName)) { minimumShouldMatch = parser.textOrNull(); - } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) { + } else if (BOOST_FIELD.match(currentFieldName)) { boost = parser.floatValue(); } else if (ADJUST_PURE_NEGATIVE.match(currentFieldName)) { adjustPureNegative = parser.booleanValue(); - } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) { + } else if (NAME_FIELD.match(currentFieldName)) { queryName = parser.text(); } else { throw new ParsingException(parser.getTokenLocation(), "[bool] query does not support [" + currentFieldName + "]"); diff --git a/core/src/main/java/org/elasticsearch/index/query/BoostingQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/BoostingQueryBuilder.java index 11aaa90fba4..833e3a2ed0d 100644 --- a/core/src/main/java/org/elasticsearch/index/query/BoostingQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/BoostingQueryBuilder.java @@ -32,6 +32,7 @@ import java.io.IOException; import java.util.Map; import java.util.Objects; + /** * The BoostingQuery class can be used to effectively demote results that match a given query. * Unlike the "NOT" clause, this still selects documents that contain undesirable terms, @@ -136,14 +137,12 @@ public class BoostingQueryBuilder extends AbstractQueryBuilder builder.endObject(); } - public static DisMaxQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException { - XContentParser parser = parseContext.parser(); - + public static DisMaxQueryBuilder fromXContent(XContentParser parser) throws IOException { float boost = AbstractQueryBuilder.DEFAULT_BOOST; float tieBreaker = DisMaxQueryBuilder.DEFAULT_TIE_BREAKER; @@ -140,7 +138,7 @@ public class DisMaxQueryBuilder extends AbstractQueryBuilder } else if (token == XContentParser.Token.START_OBJECT) { if (QUERIES_FIELD.match(currentFieldName)) { queriesFound = true; - queries.add(parseContext.parseInnerQueryBuilder()); + queries.add(parseInnerQueryBuilder(parser)); } else { throw new ParsingException(parser.getTokenLocation(), "[dis_max] query does not support [" + currentFieldName + "]"); } @@ -148,7 +146,7 @@ public class DisMaxQueryBuilder extends AbstractQueryBuilder if (QUERIES_FIELD.match(currentFieldName)) { queriesFound = true; while (token != XContentParser.Token.END_ARRAY) { - queries.add(parseContext.parseInnerQueryBuilder()); + queries.add(parseInnerQueryBuilder(parser)); token = parser.nextToken(); } } else { diff --git a/core/src/main/java/org/elasticsearch/index/query/ExistsQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/ExistsQueryBuilder.java index f8e3bdec0d9..799998e2c9f 100644 --- a/core/src/main/java/org/elasticsearch/index/query/ExistsQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/ExistsQueryBuilder.java @@ -83,9 +83,7 @@ public class ExistsQueryBuilder extends AbstractQueryBuilder builder.endObject(); } - public static ExistsQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException { - XContentParser parser = parseContext.parser(); - + public static ExistsQueryBuilder fromXContent(XContentParser parser) throws IOException { String fieldPattern = null; String queryName = null; float boost = AbstractQueryBuilder.DEFAULT_BOOST; diff --git a/core/src/main/java/org/elasticsearch/index/query/FieldMaskingSpanQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/FieldMaskingSpanQueryBuilder.java index a7ef6d9551b..9fd037f5610 100644 --- a/core/src/main/java/org/elasticsearch/index/query/FieldMaskingSpanQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/FieldMaskingSpanQueryBuilder.java @@ -100,9 +100,7 @@ public class FieldMaskingSpanQueryBuilder extends AbstractQueryBuilder i builder.endObject(); } - public static FuzzyQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException { - XContentParser parser = parseContext.parser(); + public static FuzzyQueryBuilder fromXContent(XContentParser parser) throws IOException { String fieldName = null; Object value = null; Fuzziness fuzziness = FuzzyQueryBuilder.DEFAULT_FUZZINESS; @@ -267,8 +266,6 @@ public class FuzzyQueryBuilder extends AbstractQueryBuilder i while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); - } else if (parseContext.isDeprecatedSetting(currentFieldName)) { - // skip } else if (token == XContentParser.Token.START_OBJECT) { throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName); fieldName = currentFieldName; diff --git a/core/src/main/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilder.java index f006f056f93..a0b60dd30c6 100644 --- a/core/src/main/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilder.java @@ -375,9 +375,7 @@ public class GeoBoundingBoxQueryBuilder extends AbstractQueryBuilder shell = null; @@ -238,8 +236,6 @@ public class GeoPolygonQueryBuilder extends AbstractQueryBuilder(); + shell = new ArrayList<>(); while ((token = parser.nextToken()) != Token.END_ARRAY) { shell.add(GeoUtils.parseGeoPoint(parser)); } diff --git a/core/src/main/java/org/elasticsearch/index/query/GeoShapeQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/GeoShapeQueryBuilder.java index 138f62a92c2..ea6ecc9a532 100644 --- a/core/src/main/java/org/elasticsearch/index/query/GeoShapeQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/GeoShapeQueryBuilder.java @@ -457,9 +457,7 @@ public class GeoShapeQueryBuilder extends AbstractQueryBuilder { declareStandardFields(PARSER); } - public static IdsQueryBuilder fromXContent(QueryParseContext context) { + public static IdsQueryBuilder fromXContent(XContentParser parser) { try { - return PARSER.apply(context.parser(), context); + return PARSER.apply(parser, null); } catch (IllegalArgumentException e) { - throw new ParsingException(context.parser().getTokenLocation(), e.getMessage(), e); + throw new ParsingException(parser.getTokenLocation(), e.getMessage(), e); } } diff --git a/core/src/main/java/org/elasticsearch/index/query/InnerHitBuilder.java b/core/src/main/java/org/elasticsearch/index/query/InnerHitBuilder.java index 7b331dd487b..fb0f2850c1b 100644 --- a/core/src/main/java/org/elasticsearch/index/query/InnerHitBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/InnerHitBuilder.java @@ -75,23 +75,23 @@ public final class InnerHitBuilder extends ToXContentToBytes implements Writeabl try { Set scriptFields = new HashSet<>(); for (XContentParser.Token token = p.nextToken(); token != END_OBJECT; token = p.nextToken()) { - scriptFields.add(new ScriptField(c)); + scriptFields.add(new ScriptField(p)); } i.setScriptFields(scriptFields); } catch (IOException e) { throw new ParsingException(p.getTokenLocation(), "Could not parse inner script definition", e); } }, SearchSourceBuilder.SCRIPT_FIELDS_FIELD, ObjectParser.ValueType.OBJECT); - PARSER.declareField((p, i, c) -> i.setSorts(SortBuilder.fromXContent(c)), SearchSourceBuilder.SORT_FIELD, + PARSER.declareField((p, i, c) -> i.setSorts(SortBuilder.fromXContent(p)), SearchSourceBuilder.SORT_FIELD, ObjectParser.ValueType.OBJECT_ARRAY); PARSER.declareField((p, i, c) -> { try { - i.setFetchSourceContext(FetchSourceContext.fromXContent(c.parser())); + i.setFetchSourceContext(FetchSourceContext.fromXContent(p)); } catch (IOException e) { throw new ParsingException(p.getTokenLocation(), "Could not parse inner _source definition", e); } }, SearchSourceBuilder._SOURCE_FIELD, ObjectParser.ValueType.OBJECT_ARRAY_BOOLEAN_OR_STRING); - PARSER.declareObject(InnerHitBuilder::setHighlightBuilder, (p, c) -> HighlightBuilder.fromXContent(c), + PARSER.declareObject(InnerHitBuilder::setHighlightBuilder, (p, c) -> HighlightBuilder.fromXContent(p), SearchSourceBuilder.HIGHLIGHT_FIELD); } @@ -582,7 +582,7 @@ public final class InnerHitBuilder extends ToXContentToBytes implements Writeabl storedFieldsContext, docValueFields, scriptFields, fetchSourceContext, sorts, highlightBuilder); } - public static InnerHitBuilder fromXContent(QueryParseContext context) throws IOException { - return PARSER.parse(context.parser(), new InnerHitBuilder(), context); + public static InnerHitBuilder fromXContent(XContentParser parser) throws IOException { + return PARSER.parse(parser, new InnerHitBuilder(), null); } } diff --git a/core/src/main/java/org/elasticsearch/index/query/MatchAllQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/MatchAllQueryBuilder.java index 63580eca3e7..2f50aa731fa 100644 --- a/core/src/main/java/org/elasticsearch/index/query/MatchAllQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/MatchAllQueryBuilder.java @@ -26,9 +26,9 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentParser; import java.io.IOException; -import java.util.Optional; /** * A query that matches on all documents. @@ -64,11 +64,11 @@ public class MatchAllQueryBuilder extends AbstractQueryBuilder { return NAME; } - public static MatchQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException { - XContentParser parser = parseContext.parser(); + public static MatchQueryBuilder fromXContent(XContentParser parser) throws IOException { String fieldName = null; MatchQuery.Type type = MatchQuery.Type.BOOLEAN; Object value = null; @@ -515,8 +514,6 @@ public class MatchQueryBuilder extends AbstractQueryBuilder { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); - } else if (parseContext.isDeprecatedSetting(currentFieldName)) { - // skip } else if (token == XContentParser.Token.START_OBJECT) { throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName); fieldName = currentFieldName; diff --git a/core/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java index 7889dee26dd..a4872b1244c 100644 --- a/core/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java @@ -809,9 +809,7 @@ public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder fields = null; List likeTexts = new ArrayList<>(); @@ -846,9 +844,9 @@ public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder texts, List items) throws IOException { - XContentParser parser = parseContext.parser(); + private static void parseLikeField(XContentParser parser, List texts, List items) throws IOException { if (parser.currentToken().isValue()) { texts.add(parser.text()); } else if (parser.currentToken() == XContentParser.Token.START_OBJECT) { diff --git a/core/src/main/java/org/elasticsearch/index/query/MultiMatchQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/MultiMatchQueryBuilder.java index b668b8d9831..1cddb6e36c2 100644 --- a/core/src/main/java/org/elasticsearch/index/query/MultiMatchQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/MultiMatchQueryBuilder.java @@ -554,9 +554,7 @@ public class MultiMatchQueryBuilder extends AbstractQueryBuilder fieldsBoosts = new HashMap<>(); MultiMatchQueryBuilder.Type type = DEFAULT_TYPE; diff --git a/core/src/main/java/org/elasticsearch/index/query/NestedQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/NestedQueryBuilder.java index ae092443f49..b9037110b1c 100644 --- a/core/src/main/java/org/elasticsearch/index/query/NestedQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/NestedQueryBuilder.java @@ -178,8 +178,7 @@ public class NestedQueryBuilder extends AbstractQueryBuilder builder.endObject(); } - public static NestedQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException { - XContentParser parser = parseContext.parser(); + public static NestedQueryBuilder fromXContent(XContentParser parser) throws IOException { float boost = AbstractQueryBuilder.DEFAULT_BOOST; ScoreMode scoreMode = ScoreMode.Avg; String queryName = null; @@ -194,9 +193,9 @@ public class NestedQueryBuilder extends AbstractQueryBuilder currentFieldName = parser.currentName(); } else if (token == XContentParser.Token.START_OBJECT) { if (QUERY_FIELD.match(currentFieldName)) { - query = parseContext.parseInnerQueryBuilder(); + query = parseInnerQueryBuilder(parser); } else if (INNER_HITS_FIELD.match(currentFieldName)) { - innerHitBuilder = InnerHitBuilder.fromXContent(parseContext); + innerHitBuilder = InnerHitBuilder.fromXContent(parser); } else { throw new ParsingException(parser.getTokenLocation(), "[nested] query does not support [" + currentFieldName + "]"); } diff --git a/core/src/main/java/org/elasticsearch/index/query/PrefixQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/PrefixQueryBuilder.java index 7410ca68afc..0392f0e7ff6 100644 --- a/core/src/main/java/org/elasticsearch/index/query/PrefixQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/PrefixQueryBuilder.java @@ -116,9 +116,7 @@ public class PrefixQueryBuilder extends AbstractQueryBuilder builder.endObject(); } - public static PrefixQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException { - XContentParser parser = parseContext.parser(); - + public static PrefixQueryBuilder fromXContent(XContentParser parser) throws IOException { String fieldName = null; String value = null; String rewrite = null; @@ -130,8 +128,6 @@ public class PrefixQueryBuilder extends AbstractQueryBuilder while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); - } else if (parseContext.isDeprecatedSetting(currentFieldName)) { - // skip } else if (token == XContentParser.Token.START_OBJECT) { throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName); fieldName = currentFieldName; diff --git a/core/src/main/java/org/elasticsearch/index/query/QueryParseContext.java b/core/src/main/java/org/elasticsearch/index/query/QueryParseContext.java index 269ee1048d4..fb612028163 100644 --- a/core/src/main/java/org/elasticsearch/index/query/QueryParseContext.java +++ b/core/src/main/java/org/elasticsearch/index/query/QueryParseContext.java @@ -19,20 +19,12 @@ package org.elasticsearch.index.query; -import org.elasticsearch.common.ParseField; -import org.elasticsearch.common.ParsingException; -import org.elasticsearch.common.xcontent.NamedXContentRegistry.UnknownNamedObjectException; -import org.elasticsearch.common.xcontent.XContentLocation; import org.elasticsearch.common.xcontent.XContentParser; -import java.io.IOException; import java.util.Objects; public class QueryParseContext { - private static final ParseField CACHE = new ParseField("_cache").withAllDeprecated("Elasticsearch makes its own caching decisions"); - private static final ParseField CACHE_KEY = new ParseField("_cache_key").withAllDeprecated("Filters are always used as cache keys"); - private final XContentParser parser; public QueryParseContext(XContentParser parser) { @@ -42,75 +34,4 @@ public class QueryParseContext { public XContentParser parser() { return this.parser; } - - public boolean isDeprecatedSetting(String setting) { - return CACHE.match(setting) || CACHE_KEY.match(setting); - } - - /** - * Parses a top level query including the query element that wraps it - */ - public QueryBuilder parseTopLevelQueryBuilder() { - try { - QueryBuilder queryBuilder = null; - for (XContentParser.Token token = parser.nextToken(); token != XContentParser.Token.END_OBJECT; token = parser.nextToken()) { - if (token == XContentParser.Token.FIELD_NAME) { - String fieldName = parser.currentName(); - if ("query".equals(fieldName)) { - queryBuilder = parseInnerQueryBuilder(); - } else { - throw new ParsingException(parser.getTokenLocation(), "request does not support [" + parser.currentName() + "]"); - } - } - } - return queryBuilder; - } catch (ParsingException e) { - throw e; - } catch (Exception e) { - throw new ParsingException(parser == null ? null : parser.getTokenLocation(), "Failed to parse", e); - } - } - - /** - * Parses a query excluding the query element that wraps it - */ - public QueryBuilder parseInnerQueryBuilder() throws IOException { - if (parser.currentToken() != XContentParser.Token.START_OBJECT) { - if (parser.nextToken() != XContentParser.Token.START_OBJECT) { - throw new ParsingException(parser.getTokenLocation(), "[_na] query malformed, must start with start_object"); - } - } - if (parser.nextToken() == XContentParser.Token.END_OBJECT) { - // we encountered '{}' for a query clause, it used to be supported, deprecated in 5.0 and removed in 6.0 - throw new IllegalArgumentException("query malformed, empty clause found at [" + parser.getTokenLocation() +"]"); - } - if (parser.currentToken() != XContentParser.Token.FIELD_NAME) { - throw new ParsingException(parser.getTokenLocation(), "[_na] query malformed, no field after start_object"); - } - String queryName = parser.currentName(); - // move to the next START_OBJECT - if (parser.nextToken() != XContentParser.Token.START_OBJECT) { - throw new ParsingException(parser.getTokenLocation(), "[" + queryName + "] query malformed, no start_object after query name"); - } - QueryBuilder result; - try { - result = parser.namedObject(QueryBuilder.class, queryName, this); - } catch (UnknownNamedObjectException e) { - // Preserve the error message from 5.0 until we have a compellingly better message so we don't break BWC. - // This intentionally doesn't include the causing exception because that'd change the "root_cause" of any unknown query errors - throw new ParsingException(new XContentLocation(e.getLineNumber(), e.getColumnNumber()), - "no [query] registered for [" + e.getName() + "]"); - } - //end_object of the specific query (e.g. match, multi_match etc.) element - if (parser.currentToken() != XContentParser.Token.END_OBJECT) { - throw new ParsingException(parser.getTokenLocation(), - "[" + queryName + "] malformed query, expected [END_OBJECT] but found [" + parser.currentToken() + "]"); - } - //end_object of the query object - if (parser.nextToken() != XContentParser.Token.END_OBJECT) { - throw new ParsingException(parser.getTokenLocation(), - "[" + queryName + "] malformed query, expected [END_OBJECT] but found [" + parser.currentToken() + "]"); - } - return result; - } } diff --git a/core/src/main/java/org/elasticsearch/index/query/QueryParser.java b/core/src/main/java/org/elasticsearch/index/query/QueryParser.java index 069dc86cf8a..fc50a5e31fe 100644 --- a/core/src/main/java/org/elasticsearch/index/query/QueryParser.java +++ b/core/src/main/java/org/elasticsearch/index/query/QueryParser.java @@ -19,6 +19,8 @@ package org.elasticsearch.index.query; +import org.elasticsearch.common.xcontent.XContentParser; + import java.io.IOException; /** @@ -27,14 +29,9 @@ import java.io.IOException; @FunctionalInterface public interface QueryParser { /** - * Creates a new {@link QueryBuilder} from the query held by the {@link QueryParseContext} - * in {@link org.elasticsearch.common.xcontent.XContent} format - * - * @param parseContext - * the input parse context. The state on the parser contained in - * this context will be changed as a side effect of this method - * call - * @return the new QueryBuilder + * Creates a new {@link QueryBuilder} from the query held by the + * {@link XContentParser}. The state on the parser contained in this context + * will be changed as a side effect of this method call */ - QB fromXContent(QueryParseContext parseContext) throws IOException; + QB fromXContent(XContentParser parser) throws IOException; } diff --git a/core/src/main/java/org/elasticsearch/index/query/QueryStringQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/QueryStringQueryBuilder.java index 0269249e825..8e08b3dc7df 100644 --- a/core/src/main/java/org/elasticsearch/index/query/QueryStringQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/QueryStringQueryBuilder.java @@ -683,8 +683,7 @@ public class QueryStringQueryBuilder extends AbstractQueryBuilder i builder.endObject(); } - public static RangeQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException { - XContentParser parser = parseContext.parser(); - + public static RangeQueryBuilder fromXContent(XContentParser parser) throws IOException { String fieldName = null; Object from = null; Object to = null; @@ -362,8 +360,6 @@ public class RangeQueryBuilder extends AbstractQueryBuilder i while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); - } else if (parseContext.isDeprecatedSetting(currentFieldName)) { - // skip } else if (token == XContentParser.Token.START_OBJECT) { throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName); fieldName = currentFieldName; diff --git a/core/src/main/java/org/elasticsearch/index/query/RegexpQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/RegexpQueryBuilder.java index 7957f0de0a1..e6ae1b7d63a 100644 --- a/core/src/main/java/org/elasticsearch/index/query/RegexpQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/RegexpQueryBuilder.java @@ -177,8 +177,7 @@ public class RegexpQueryBuilder extends AbstractQueryBuilder builder.endObject(); } - public static RegexpQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException { - XContentParser parser = parseContext.parser(); + public static RegexpQueryBuilder fromXContent(XContentParser parser) throws IOException { String fieldName = null; String rewrite = null; String value = null; @@ -191,8 +190,6 @@ public class RegexpQueryBuilder extends AbstractQueryBuilder while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); - } else if (parseContext.isDeprecatedSetting(currentFieldName)) { - // skip } else if (token == XContentParser.Token.START_OBJECT) { throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName); fieldName = currentFieldName; diff --git a/core/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java index 0d608fd5f11..7272316499d 100644 --- a/core/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java @@ -84,8 +84,7 @@ public class ScriptQueryBuilder extends AbstractQueryBuilder builder.endObject(); } - public static ScriptQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException { - XContentParser parser = parseContext.parser(); + public static ScriptQueryBuilder fromXContent(XContentParser parser) throws IOException { // also, when caching, since its isCacheable is false, will result in loading all bit set... Script script = null; @@ -97,8 +96,6 @@ public class ScriptQueryBuilder extends AbstractQueryBuilder while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); - } else if (parseContext.isDeprecatedSetting(currentFieldName)) { - // skip } else if (token == XContentParser.Token.START_OBJECT) { if (Script.SCRIPT_PARSE_FIELD.match(currentFieldName)) { script = Script.parse(parser); diff --git a/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringBuilder.java b/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringBuilder.java index 2efa4e815a4..2430534e666 100644 --- a/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringBuilder.java @@ -462,9 +462,7 @@ public class SimpleQueryStringBuilder extends AbstractQueryBuilder builder.endObject(); } - public static SpanOrQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException { - XContentParser parser = parseContext.parser(); - + public static SpanOrQueryBuilder fromXContent(XContentParser parser) throws IOException { float boost = AbstractQueryBuilder.DEFAULT_BOOST; String queryName = null; @@ -113,7 +111,7 @@ public class SpanOrQueryBuilder extends AbstractQueryBuilder } else if (token == XContentParser.Token.START_ARRAY) { if (CLAUSES_FIELD.match(currentFieldName)) { while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { - QueryBuilder query = parseContext.parseInnerQueryBuilder(); + QueryBuilder query = parseInnerQueryBuilder(parser); if (query instanceof SpanQueryBuilder == false) { throw new ParsingException(parser.getTokenLocation(), "spanOr [clauses] must be of type span query"); } diff --git a/core/src/main/java/org/elasticsearch/index/query/SpanTermQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/SpanTermQueryBuilder.java index 67319dfe6e4..8c0e56266fb 100644 --- a/core/src/main/java/org/elasticsearch/index/query/SpanTermQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/SpanTermQueryBuilder.java @@ -91,8 +91,7 @@ public class SpanTermQueryBuilder extends BaseTermQueryBuilder { super(in); } - public static TermQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException { - XContentParser parser = parseContext.parser(); - + public static TermQueryBuilder fromXContent(XContentParser parser) throws IOException { String queryName = null; String fieldName = null; Object value = null; @@ -94,8 +92,6 @@ public class TermQueryBuilder extends BaseTermQueryBuilder { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); - } else if (parseContext.isDeprecatedSetting(currentFieldName)) { - // skip } else if (token == XContentParser.Token.START_OBJECT) { throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName); fieldName = currentFieldName; diff --git a/core/src/main/java/org/elasticsearch/index/query/TermsQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/TermsQueryBuilder.java index 620004e2060..96873bb6006 100644 --- a/core/src/main/java/org/elasticsearch/index/query/TermsQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/TermsQueryBuilder.java @@ -19,8 +19,8 @@ package org.elasticsearch.index.query; -import org.apache.lucene.search.TermInSetQuery; import org.apache.lucene.search.Query; +import org.apache.lucene.search.TermInSetQuery; import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRefBuilder; import org.elasticsearch.action.get.GetRequest; @@ -205,7 +205,7 @@ public class TermsQueryBuilder extends AbstractQueryBuilder { if (values instanceof List) { list = (List) values; } else { - ArrayList arrayList = new ArrayList(); + ArrayList arrayList = new ArrayList<>(); for (Object o : values) { arrayList.add(o); } @@ -266,11 +266,13 @@ public class TermsQueryBuilder extends AbstractQueryBuilder { } final BytesReference bytes = bytesOut.bytes(); return new AbstractList() { + @Override public Object get(int i) { final int startOffset = i == 0 ? 0 : endOffsets[i-1]; final int endOffset = endOffsets[i]; return bytes.slice(startOffset, endOffset - startOffset).toBytesRef(); } + @Override public int size() { return endOffsets.length; } @@ -320,9 +322,7 @@ public class TermsQueryBuilder extends AbstractQueryBuilder { builder.endObject(); } - public static TermsQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException { - XContentParser parser = parseContext.parser(); - + public static TermsQueryBuilder fromXContent(XContentParser parser) throws IOException { String fieldName = null; List values = null; TermsLookup termsLookup = null; @@ -335,8 +335,6 @@ public class TermsQueryBuilder extends AbstractQueryBuilder { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); - } else if (parseContext.isDeprecatedSetting(currentFieldName)) { - // skip } else if (token == XContentParser.Token.START_ARRAY) { if (fieldName != null) { throw new ParsingException(parser.getTokenLocation(), diff --git a/core/src/main/java/org/elasticsearch/index/query/TypeQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/TypeQueryBuilder.java index fc4df8efa81..d53736a3836 100644 --- a/core/src/main/java/org/elasticsearch/index/query/TypeQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/TypeQueryBuilder.java @@ -80,13 +80,10 @@ public class TypeQueryBuilder extends AbstractQueryBuilder { builder.endObject(); } - public static TypeQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException { - XContentParser parser = parseContext.parser(); + public static TypeQueryBuilder fromXContent(XContentParser parser) throws IOException { BytesRef type = null; - String queryName = null; float boost = AbstractQueryBuilder.DEFAULT_BOOST; - String currentFieldName = null; XContentParser.Token token; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { diff --git a/core/src/main/java/org/elasticsearch/index/query/WildcardQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/WildcardQueryBuilder.java index 006f55c8710..83915804211 100644 --- a/core/src/main/java/org/elasticsearch/index/query/WildcardQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/WildcardQueryBuilder.java @@ -132,8 +132,7 @@ public class WildcardQueryBuilder extends AbstractQueryBuilder> im * */ @Override - public DFB fromXContent(QueryParseContext context) throws IOException, ParsingException { - XContentParser parser = context.parser(); + public DFB fromXContent(XContentParser parser) throws IOException, ParsingException { String currentFieldName; XContentParser.Token token; MultiValueMode multiValueMode = DecayFunctionBuilder.DEFAULT_MULTI_VALUE_MODE; diff --git a/core/src/main/java/org/elasticsearch/index/query/functionscore/FieldValueFactorFunctionBuilder.java b/core/src/main/java/org/elasticsearch/index/query/functionscore/FieldValueFactorFunctionBuilder.java index d9cef5522ec..19e35ddc4ee 100644 --- a/core/src/main/java/org/elasticsearch/index/query/functionscore/FieldValueFactorFunctionBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/functionscore/FieldValueFactorFunctionBuilder.java @@ -29,7 +29,6 @@ import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.fielddata.IndexNumericFieldData; import org.elasticsearch.index.mapper.MappedFieldType; -import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.query.QueryShardContext; import java.io.IOException; @@ -157,9 +156,8 @@ public class FieldValueFactorFunctionBuilder extends ScoreFunctionBuilder scoreFunction = parser.namedObject(ScoreFunctionBuilder.class, currentFieldName, - parseContext); + null); filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(scoreFunction)); } } else if (token == XContentParser.Token.START_ARRAY) { @@ -490,7 +487,7 @@ public class FunctionScoreQueryBuilder extends AbstractQueryBuilder filterFunctionBuilders) throws IOException { String currentFieldName = null; XContentParser.Token token; - XContentParser parser = parseContext.parser(); while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { QueryBuilder filter = null; ScoreFunctionBuilder scoreFunction = null; @@ -576,14 +572,14 @@ public class FunctionScoreQueryBuilder extends AbstractQueryBuilder> { - FB fromXContent(QueryParseContext context) throws IOException; + FB fromXContent(XContentParser parser) throws IOException; } diff --git a/core/src/main/java/org/elasticsearch/index/query/functionscore/ScriptScoreFunctionBuilder.java b/core/src/main/java/org/elasticsearch/index/query/functionscore/ScriptScoreFunctionBuilder.java index 7c66a7de99c..60f39188639 100644 --- a/core/src/main/java/org/elasticsearch/index/query/functionscore/ScriptScoreFunctionBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/functionscore/ScriptScoreFunctionBuilder.java @@ -26,11 +26,9 @@ import org.elasticsearch.common.lucene.search.function.ScoreFunction; import org.elasticsearch.common.lucene.search.function.ScriptScoreFunction; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.index.query.QueryShardException; import org.elasticsearch.script.Script; -import org.elasticsearch.script.ScriptContext; import org.elasticsearch.script.SearchScript; import java.io.IOException; @@ -102,9 +100,8 @@ public class ScriptScoreFunctionBuilder extends ScoreFunctionBuilder, IndexService.ShardStoreDeleter { @@ -1261,7 +1261,7 @@ public class IndicesService extends AbstractLifecycleComponent * of dependencies we pass in a function that can perform the parsing. */ CheckedFunction filterParser = bytes -> { try (XContentParser parser = XContentFactory.xContent(bytes).createParser(xContentRegistry, bytes)) { - return new QueryParseContext(parser).parseInnerQueryBuilder(); + return parseInnerQueryBuilder(parser); } }; String[] aliases = indexNameExpressionResolver.filteringAliases(state, index, expressions); diff --git a/core/src/main/java/org/elasticsearch/rest/action/RestActions.java b/core/src/main/java/org/elasticsearch/rest/action/RestActions.java index 15199c5a926..295ae7418c6 100644 --- a/core/src/main/java/org/elasticsearch/rest/action/RestActions.java +++ b/core/src/main/java/org/elasticsearch/rest/action/RestActions.java @@ -26,6 +26,7 @@ import org.elasticsearch.action.support.broadcast.BroadcastResponse; import org.elasticsearch.action.support.nodes.BaseNodeResponse; import org.elasticsearch.action.support.nodes.BaseNodesResponse; import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.lucene.uid.Versions; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContent.Params; @@ -34,7 +35,6 @@ import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.query.Operator; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; -import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.query.QueryStringQueryBuilder; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestChannel; @@ -45,6 +45,8 @@ import org.elasticsearch.rest.RestStatus; import java.io.IOException; import java.util.List; +import static org.elasticsearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder; + public class RestActions { public static final ParseField _SHARDS_FIELD = new ParseField("_shards"); @@ -201,8 +203,7 @@ public class RestActions { } public static QueryBuilder getQueryContent(XContentParser requestParser) { - QueryParseContext context = new QueryParseContext(requestParser); - return context.parseTopLevelQueryBuilder(); + return parseTopLevelQueryBuilder(requestParser); } /** @@ -232,4 +233,28 @@ public class RestActions { } + /** + * Parses a top level query including the query element that wraps it + */ + private static QueryBuilder parseTopLevelQueryBuilder(XContentParser parser) { + try { + QueryBuilder queryBuilder = null; + for (XContentParser.Token token = parser.nextToken(); token != XContentParser.Token.END_OBJECT; token = parser.nextToken()) { + if (token == XContentParser.Token.FIELD_NAME) { + String fieldName = parser.currentName(); + if ("query".equals(fieldName)) { + queryBuilder = parseInnerQueryBuilder(parser); + } else { + throw new ParsingException(parser.getTokenLocation(), "request does not support [" + parser.currentName() + "]"); + } + } + } + return queryBuilder; + } catch (ParsingException e) { + throw e; + } catch (Exception e) { + throw new ParsingException(parser == null ? null : parser.getTokenLocation(), "Failed to parse", e); + } + } + } diff --git a/core/src/main/java/org/elasticsearch/search/SearchModule.java b/core/src/main/java/org/elasticsearch/search/SearchModule.java index 87d937c0995..2a72c1904ed 100644 --- a/core/src/main/java/org/elasticsearch/search/SearchModule.java +++ b/core/src/main/java/org/elasticsearch/search/SearchModule.java @@ -54,7 +54,6 @@ import org.elasticsearch.index.query.MultiMatchQueryBuilder; import org.elasticsearch.index.query.NestedQueryBuilder; import org.elasticsearch.index.query.PrefixQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.query.QueryStringQueryBuilder; import org.elasticsearch.index.query.RangeQueryBuilder; import org.elasticsearch.index.query.RegexpQueryBuilder; @@ -606,7 +605,7 @@ public class SearchModule { // TODO remove funky contexts namedXContents.add(new NamedXContentRegistry.Entry( ScoreFunctionBuilder.class, scoreFunction.getName(), - (XContentParser p, Object c) -> scoreFunction.getParser().fromXContent((QueryParseContext) c))); + (XContentParser p, Object c) -> scoreFunction.getParser().fromXContent(p))); } private void registerValueFormats() { @@ -751,7 +750,7 @@ public class SearchModule { private void registerQuery(QuerySpec spec) { namedWriteables.add(new NamedWriteableRegistry.Entry(QueryBuilder.class, spec.getName().getPreferredName(), spec.getReader())); namedXContents.add(new NamedXContentRegistry.Entry(QueryBuilder.class, spec.getName(), - (p, c) -> spec.getParser().fromXContent((QueryParseContext) c))); + (p, c) -> spec.getParser().fromXContent(p))); } public FetchPhase getFetchPhase() { diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/AggregatorFactories.java b/core/src/main/java/org/elasticsearch/search/aggregations/AggregatorFactories.java index d635952ec15..90b1c7e58c0 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/AggregatorFactories.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/AggregatorFactories.java @@ -343,8 +343,8 @@ public class AggregatorFactories { aggBuildersMap.put(aggBuilder.name, aggBuilder); } List orderedPipelineAggregatorrs = new LinkedList<>(); - List unmarkedBuilders = new ArrayList(pipelineAggregatorBuilders); - Set temporarilyMarked = new HashSet(); + List unmarkedBuilders = new ArrayList<>(pipelineAggregatorBuilders); + Set temporarilyMarked = new HashSet<>(); while (!unmarkedBuilders.isEmpty()) { PipelineAggregationBuilder builder = unmarkedBuilders.get(0); resolvePipelineAggregatorOrder(aggBuildersMap, pipelineAggregatorBuildersMap, orderedPipelineAggregatorrs, unmarkedBuilders, diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/adjacency/AdjacencyMatrixAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/adjacency/AdjacencyMatrixAggregator.java index 6732db9cecf..8e6fffbd474 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/adjacency/AdjacencyMatrixAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/adjacency/AdjacencyMatrixAggregator.java @@ -27,10 +27,10 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.lucene.Lucene; +import org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.search.aggregations.Aggregator; @@ -48,15 +48,17 @@ import java.util.List; import java.util.Map; import java.util.Objects; +import static org.elasticsearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder; + /** * Aggregation for adjacency matrices. - * + * * NOTE! This is an experimental class. - * + * * TODO the aggregation produces a sparse response but in the * computation it uses a non-sparse structure (an array of Bits * objects). This could be changed to a sparse structure in future. - * + * */ public class AdjacencyMatrixAggregator extends BucketsAggregator { @@ -65,11 +67,11 @@ public class AdjacencyMatrixAggregator extends BucketsAggregator { protected static class KeyedFilter implements Writeable, ToXContent { private final String key; private final QueryBuilder filter; - - public static final NamedObjectParser PARSER = - (XContentParser p, QueryParseContext c, String name) -> - new KeyedFilter(name, c.parseInnerQueryBuilder()); - + + public static final NamedObjectParser PARSER = + (XContentParser p, QueryParseContext c, String name) -> + new KeyedFilter(name, parseInnerQueryBuilder(p)); + public KeyedFilter(String key, QueryBuilder filter) { if (key == null) { @@ -134,8 +136,8 @@ public class AdjacencyMatrixAggregator extends BucketsAggregator { private final int totalNumIntersections; private final String separator; - public AdjacencyMatrixAggregator(String name, AggregatorFactories factories, String separator, String[] keys, - Weight[] filters, SearchContext context, Aggregator parent, List pipelineAggregators, + public AdjacencyMatrixAggregator(String name, AggregatorFactories factories, String separator, String[] keys, + Weight[] filters, SearchContext context, Aggregator parent, List pipelineAggregators, Map metaData) throws IOException { super(name, factories, context, parent, pipelineAggregators, metaData); this.separator = separator; @@ -207,9 +209,9 @@ public class AdjacencyMatrixAggregator extends BucketsAggregator { // a date-histogram where we will look for transactions over time and can expect many // empty buckets. if (docCount > 0) { - InternalAdjacencyMatrix.InternalBucket bucket = new InternalAdjacencyMatrix.InternalBucket(keys[i], + InternalAdjacencyMatrix.InternalBucket bucket = new InternalAdjacencyMatrix.InternalBucket(keys[i], docCount, bucketAggregations(bucketOrd)); - buckets.add(bucket); + buckets.add(bucket); } } int pos = keys.length; @@ -220,7 +222,7 @@ public class AdjacencyMatrixAggregator extends BucketsAggregator { // Empty buckets are not returned due to potential for very sparse matrices if (docCount > 0) { String intersectKey = keys[i] + separator + keys[j]; - InternalAdjacencyMatrix.InternalBucket bucket = new InternalAdjacencyMatrix.InternalBucket(intersectKey, + InternalAdjacencyMatrix.InternalBucket bucket = new InternalAdjacencyMatrix.InternalBucket(intersectKey, docCount, bucketAggregations(bucketOrd)); buckets.add(bucket); } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/filter/FilterAggregationBuilder.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/filter/FilterAggregationBuilder.java index 19d3f32f39d..d3f5696bfc6 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/filter/FilterAggregationBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/filter/FilterAggregationBuilder.java @@ -32,6 +32,8 @@ import org.elasticsearch.search.internal.SearchContext; import java.io.IOException; import java.util.Objects; +import static org.elasticsearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder; + public class FilterAggregationBuilder extends AbstractAggregationBuilder { public static final String NAME = "filter"; @@ -83,7 +85,7 @@ public class FilterAggregationBuilder extends AbstractAggregationBuilder { public static final String NAME = "filters"; @@ -171,7 +173,7 @@ public class FiltersAggregationBuilder extends AbstractAggregationBuilder rewrittenFilters = new ArrayList<>(filters.size()); for(KeyedFilter kf : filters) { - rewrittenFilters.add(new KeyedFilter(kf.key(), QueryBuilder.rewriteQuery(kf.filter(), + rewrittenFilters.add(new KeyedFilter(kf.key(), QueryBuilder.rewriteQuery(kf.filter(), context.getQueryShardContext()))); } return new FiltersAggregatorFactory(name, rewrittenFilters, keyed, otherBucket, otherBucketKey, context, parent, @@ -236,7 +238,7 @@ public class FiltersAggregationBuilder extends AbstractAggregationBuilder(); while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { - QueryBuilder filter = context.parseInnerQueryBuilder(); + QueryBuilder filter = parseInnerQueryBuilder(context.parser()); nonKeyedFilters.add(filter); } } else { diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantTermsAggregationBuilder.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantTermsAggregationBuilder.java index 72825dbd989..4f297a73b80 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantTermsAggregationBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantTermsAggregationBuilder.java @@ -49,6 +49,8 @@ import org.elasticsearch.search.internal.SearchContext; import java.io.IOException; import java.util.Objects; +import static org.elasticsearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder; + public class SignificantTermsAggregationBuilder extends ValuesSourceAggregationBuilder { public static final String NAME = "significant_terms"; @@ -75,7 +77,7 @@ public class SignificantTermsAggregationBuilder extends ValuesSourceAggregationB parser.declareString(SignificantTermsAggregationBuilder::executionHint, TermsAggregationBuilder.EXECUTION_HINT_FIELD_NAME); parser.declareObject(SignificantTermsAggregationBuilder::backgroundFilter, - (p, context) -> context.parseInnerQueryBuilder(), + (p, context) -> parseInnerQueryBuilder(p), SignificantTermsAggregationBuilder.BACKGROUND_FILTER); parser.declareField((b, v) -> b.includeExclude(IncludeExclude.merge(v, b.includeExclude())), diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantTextAggregationBuilder.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantTextAggregationBuilder.java index c0fecd8be46..a55d89bbc5e 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantTextAggregationBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantTextAggregationBuilder.java @@ -25,6 +25,7 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.ParseFieldRegistry; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.index.query.AbstractQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.search.aggregations.AbstractAggregationBuilder; @@ -54,7 +55,7 @@ public class SignificantTextAggregationBuilder extends AbstractAggregationBuilde static final ParseField FILTER_DUPLICATE_TEXT_FIELD_NAME = new ParseField( "filter_duplicate_text"); - static final TermsAggregator.BucketCountThresholds DEFAULT_BUCKET_COUNT_THRESHOLDS = + static final TermsAggregator.BucketCountThresholds DEFAULT_BUCKET_COUNT_THRESHOLDS = SignificantTermsAggregationBuilder.DEFAULT_BUCKET_COUNT_THRESHOLDS; static final SignificanceHeuristic DEFAULT_SIGNIFICANCE_HEURISTIC = SignificantTermsAggregationBuilder.DEFAULT_SIGNIFICANCE_HEURISTIC; @@ -85,15 +86,15 @@ public class SignificantTextAggregationBuilder extends AbstractAggregationBuilde TermsAggregationBuilder.REQUIRED_SIZE_FIELD_NAME); parser.declareString(SignificantTextAggregationBuilder::fieldName, FIELD_NAME); - + parser.declareStringArray(SignificantTextAggregationBuilder::sourceFieldNames, SOURCE_FIELDS_NAME); - + parser.declareBoolean(SignificantTextAggregationBuilder::filterDuplicateText, FILTER_DUPLICATE_TEXT_FIELD_NAME); parser.declareObject(SignificantTextAggregationBuilder::backgroundFilter, - (p, context) -> context.parseInnerQueryBuilder(), + (p, context) -> AbstractQueryBuilder.parseInnerQueryBuilder(p), SignificantTermsAggregationBuilder.BACKGROUND_FILTER); parser.declareField((b, v) -> b.includeExclude(IncludeExclude.merge(v, b.includeExclude())), @@ -129,20 +130,20 @@ public class SignificantTextAggregationBuilder extends AbstractAggregationBuilde public TermsAggregator.BucketCountThresholds bucketCountThresholds() { return bucketCountThresholds; } - - + + @Override public SignificantTextAggregationBuilder subAggregations(Builder subFactories) { throw new AggregationInitializationException("Aggregator [" + name + "] of type [" + getType() + "] cannot accept sub-aggregations"); - } + } @Override public SignificantTextAggregationBuilder subAggregation(AggregationBuilder aggregation) { throw new AggregationInitializationException("Aggregator [" + name + "] of type [" + getType() + "] cannot accept sub-aggregations"); - } - + } + public SignificantTextAggregationBuilder bucketCountThresholds( TermsAggregator.BucketCountThresholds bucketCountThresholds) { if (bucketCountThresholds == null) { @@ -190,18 +191,18 @@ public class SignificantTextAggregationBuilder extends AbstractAggregationBuilde return this; } - + /** * Selects the fields to load from _source JSON and analyze. - * If none are specified, the indexed "fieldName" value is assumed + * If none are specified, the indexed "fieldName" value is assumed * to also be the name of the JSON field holding the value */ public SignificantTextAggregationBuilder sourceFieldNames(List names) { this.sourceFieldNames = names.toArray(new String [names.size()]); return this; } - - + + /** * Control if duplicate paragraphs of text should try be filtered from the * statistical text analysis. Can improve results but slows down analysis. @@ -288,7 +289,7 @@ public class SignificantTextAggregationBuilder extends AbstractAggregationBuilde * @param fieldName * the name of the text field that will be the subject of this * aggregation - * + * */ public SignificantTextAggregationBuilder(String name, String fieldName) { super(name); @@ -344,7 +345,7 @@ public class SignificantTextAggregationBuilder extends AbstractAggregationBuilde if (sourceFieldNames != null) { builder.array(SOURCE_FIELDS_NAME.getPreferredName(), sourceFieldNames); } - + if (filterDuplicateText) { builder.field(FILTER_DUPLICATE_TEXT_FIELD_NAME.getPreferredName(), filterDuplicateText); } @@ -356,7 +357,7 @@ public class SignificantTextAggregationBuilder extends AbstractAggregationBuilde includeExclude.toXContent(builder, params); } significanceHeuristic.toXContent(builder, params); - + builder.endObject(); return builder; } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/tophits/TopHitsAggregationBuilder.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/tophits/TopHitsAggregationBuilder.java index 2739427cfe9..c131ecfee17 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/tophits/TopHitsAggregationBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/tophits/TopHitsAggregationBuilder.java @@ -29,7 +29,6 @@ import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.script.Script; -import org.elasticsearch.script.ScriptContext; import org.elasticsearch.script.SearchScript; import org.elasticsearch.search.aggregations.AbstractAggregationBuilder; import org.elasticsearch.search.aggregations.AggregationInitializationException; @@ -671,9 +670,9 @@ public class TopHitsAggregationBuilder extends AbstractAggregationBuilder> sorts = SortBuilder.fromXContent(context); + List> sorts = SortBuilder.fromXContent(parser); factory.sorts(sorts); } else { throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", @@ -696,7 +695,7 @@ public class TopHitsAggregationBuilder extends AbstractAggregationBuilder> sorts = SortBuilder.fromXContent(context); + List> sorts = SortBuilder.fromXContent(parser); factory.sorts(sorts); } else if (SearchSourceBuilder._SOURCE_FIELD.match(currentFieldName)) { factory.fetchSource(FetchSourceContext.fromXContent(context.parser())); diff --git a/core/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java b/core/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java index be39ce3698c..99f74d1f8e0 100644 --- a/core/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java @@ -63,6 +63,8 @@ import java.util.Collections; import java.util.List; import java.util.Objects; +import static org.elasticsearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder; + /** * A search source builder allowing to easily build search source. Simple * construction using @@ -1008,15 +1010,15 @@ public final class SearchSourceBuilder extends ToXContentToBytes implements Writ } } else if (token == XContentParser.Token.START_OBJECT) { if (QUERY_FIELD.match(currentFieldName)) { - queryBuilder = context.parseInnerQueryBuilder(); + queryBuilder = parseInnerQueryBuilder(parser); } else if (POST_FILTER_FIELD.match(currentFieldName)) { - postQueryBuilder = context.parseInnerQueryBuilder(); + postQueryBuilder = parseInnerQueryBuilder(parser); } else if (_SOURCE_FIELD.match(currentFieldName)) { fetchSourceContext = FetchSourceContext.fromXContent(context.parser()); } else if (SCRIPT_FIELDS_FIELD.match(currentFieldName)) { scriptFields = new ArrayList<>(); while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { - scriptFields.add(new ScriptField(context)); + scriptFields.add(new ScriptField(parser)); } } else if (INDICES_BOOST_FIELD.match(currentFieldName)) { DEPRECATION_LOGGER.deprecated( @@ -1035,11 +1037,11 @@ public final class SearchSourceBuilder extends ToXContentToBytes implements Writ || AGGS_FIELD.match(currentFieldName)) { aggregations = AggregatorFactories.parseAggregators(context); } else if (HIGHLIGHT_FIELD.match(currentFieldName)) { - highlightBuilder = HighlightBuilder.fromXContent(context); + highlightBuilder = HighlightBuilder.fromXContent(parser); } else if (SUGGEST_FIELD.match(currentFieldName)) { suggestBuilder = SuggestBuilder.fromXContent(context.parser()); } else if (SORT_FIELD.match(currentFieldName)) { - sorts = new ArrayList<>(SortBuilder.fromXContent(context)); + sorts = new ArrayList<>(SortBuilder.fromXContent(parser)); } else if (RESCORE_FIELD.match(currentFieldName)) { rescoreBuilders = new ArrayList<>(); rescoreBuilders.add(RescoreBuilder.parseFromXContent(context)); @@ -1085,7 +1087,7 @@ public final class SearchSourceBuilder extends ToXContentToBytes implements Writ indexBoosts.add(new IndexBoost(context)); } } else if (SORT_FIELD.match(currentFieldName)) { - sorts = new ArrayList<>(SortBuilder.fromXContent(context)); + sorts = new ArrayList<>(SortBuilder.fromXContent(parser)); } else if (RESCORE_FIELD.match(currentFieldName)) { rescoreBuilders = new ArrayList<>(); while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { @@ -1373,9 +1375,8 @@ public final class SearchSourceBuilder extends ToXContentToBytes implements Writ out.writeBoolean(ignoreFailure); } - public ScriptField(QueryParseContext context) throws IOException { + public ScriptField(XContentParser parser) throws IOException { boolean ignoreFailure = false; - XContentParser parser = context.parser(); String scriptFieldName = parser.currentName(); Script script = null; diff --git a/core/src/main/java/org/elasticsearch/search/collapse/CollapseBuilder.java b/core/src/main/java/org/elasticsearch/search/collapse/CollapseBuilder.java index 93f4b8bf41c..91dd71d8a23 100644 --- a/core/src/main/java/org/elasticsearch/search/collapse/CollapseBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/collapse/CollapseBuilder.java @@ -61,12 +61,12 @@ public class CollapseBuilder implements Writeable, ToXContentObject { PARSER.declareField((parser, builder, context) -> { XContentParser.Token currentToken = parser.currentToken(); if (currentToken == XContentParser.Token.START_OBJECT) { - builder.setInnerHits(InnerHitBuilder.fromXContent(context)); + builder.setInnerHits(InnerHitBuilder.fromXContent(parser)); } else if (currentToken == XContentParser.Token.START_ARRAY) { List innerHitBuilders = new ArrayList<>(); for (currentToken = parser.nextToken(); currentToken != XContentParser.Token.END_ARRAY; currentToken = parser.nextToken()) { if (currentToken == XContentParser.Token.START_OBJECT) { - innerHitBuilders.add(InnerHitBuilder.fromXContent(context)); + innerHitBuilders.add(InnerHitBuilder.fromXContent(parser)); } else { throw new ParsingException(parser.getTokenLocation(), "Invalid token in inner_hits array"); } diff --git a/core/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/AbstractHighlighterBuilder.java b/core/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/AbstractHighlighterBuilder.java index e6e50cc37e6..43e8bf1eb82 100644 --- a/core/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/AbstractHighlighterBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/AbstractHighlighterBuilder.java @@ -44,6 +44,7 @@ import java.util.Objects; import java.util.function.BiFunction; import static org.elasticsearch.common.xcontent.ObjectParser.fromList; +import static org.elasticsearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder; /** * This abstract class holds parameters shared by {@link HighlightBuilder} and {@link HighlightBuilder.Field} @@ -593,7 +594,7 @@ public abstract class AbstractHighlighterBuilder> BiFunction setupParser( + static > BiFunction setupParser( ObjectParser parser) { parser.declareStringArray(fromList(String.class, HB::preTags), PRE_TAGS_FIELD); parser.declareStringArray(fromList(String.class, HB::postTags), POST_TAGS_FIELD); @@ -620,16 +621,16 @@ public abstract class AbstractHighlighterBuilder { try { - return c.parseInnerQueryBuilder(); + return parseInnerQueryBuilder(p); } catch (IOException e) { throw new RuntimeException("Error parsing query", e); } }, HIGHLIGHT_QUERY_FIELD); - return (QueryParseContext c, HB hb) -> { + return (XContentParser p, HB hb) -> { try { - parser.parse(c.parser(), hb, c); + parser.parse(p, hb, null); if (hb.preTags() != null && hb.postTags() == null) { - throw new ParsingException(c.parser().getTokenLocation(), + throw new ParsingException(p.getTokenLocation(), "pre_tags are set but post_tags are not set"); } } catch (IOException e) { diff --git a/core/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightBuilder.java b/core/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightBuilder.java index 5e49aa7395d..55310dafb0c 100644 --- a/core/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightBuilder.java @@ -256,7 +256,7 @@ public class HighlightBuilder extends AbstractHighlighterBuilder PARSER; + private static final BiFunction PARSER; static { ObjectParser parser = new ObjectParser<>("highlight"); parser.declareString(HighlightBuilder::tagsSchema, new ParseField("tags_schema")); @@ -265,8 +265,8 @@ public class HighlightBuilder extends AbstractHighlighterBuilder matchedFields = new HashSet(field.matchedFields.length); + Set matchedFields = new HashSet<>(field.matchedFields.length); Collections.addAll(matchedFields, field.matchedFields); fieldOptionsBuilder.matchedFields(matchedFields); } @@ -422,8 +422,8 @@ public class HighlightBuilder extends AbstractHighlighterBuilder parser = new ObjectParser<>("highlight_field"); parser.declareInt(Field::fragmentOffset, FRAGMENT_OFFSET_FIELD); parser.declareStringArray(fromList(String.class, Field::matchedFields), MATCHED_FIELDS_FIELD); - BiFunction decoratedParser = setupParser(parser); - PARSER = (XContentParser p, QueryParseContext c, String name) -> decoratedParser.apply(c, new Field(name)); + BiFunction decoratedParser = setupParser(parser); + PARSER = (XContentParser p, QueryParseContext c, String name) -> decoratedParser.apply(p, new Field(name)); } private final String name; diff --git a/core/src/main/java/org/elasticsearch/search/internal/AliasFilter.java b/core/src/main/java/org/elasticsearch/search/internal/AliasFilter.java index 7c6bea8ce90..c641a38847e 100644 --- a/core/src/main/java/org/elasticsearch/search/internal/AliasFilter.java +++ b/core/src/main/java/org/elasticsearch/search/internal/AliasFilter.java @@ -35,6 +35,8 @@ import java.io.IOException; import java.util.Arrays; import java.util.Objects; +import static org.elasticsearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder; + /** * Represents a {@link QueryBuilder} and a list of alias names that filters the builder is composed of. */ @@ -71,7 +73,7 @@ public final class AliasFilter implements Writeable { * of dependencies we pass in a function that can perform the parsing. */ CheckedFunction filterParser = bytes -> { try (XContentParser parser = XContentFactory.xContent(bytes).createParser(context.getXContentRegistry(), bytes)) { - return context.newParseContext(parser).parseInnerQueryBuilder(); + return parseInnerQueryBuilder(parser); } }; return ShardSearchRequest.parseAliasFilter(filterParser, indexMetaData, aliases); diff --git a/core/src/main/java/org/elasticsearch/search/rescore/QueryRescorerBuilder.java b/core/src/main/java/org/elasticsearch/search/rescore/QueryRescorerBuilder.java index f4174e48953..3112b0db0a4 100644 --- a/core/src/main/java/org/elasticsearch/search/rescore/QueryRescorerBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/rescore/QueryRescorerBuilder.java @@ -34,6 +34,8 @@ import java.io.IOException; import java.util.Locale; import java.util.Objects; +import static org.elasticsearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder; + public class QueryRescorerBuilder extends RescoreBuilder { public static final String NAME = "query"; @@ -56,7 +58,7 @@ public class QueryRescorerBuilder extends RescoreBuilder { static { QUERY_RESCORE_PARSER.declareObject(InnerBuilder::setQueryBuilder, (p, c) -> { try { - return c.parseInnerQueryBuilder(); + return parseInnerQueryBuilder(p); } catch (IOException e) { throw new ParsingException(p.getTokenLocation(), "Could not parse inner query", e); } diff --git a/core/src/main/java/org/elasticsearch/search/sort/FieldSortBuilder.java b/core/src/main/java/org/elasticsearch/search/sort/FieldSortBuilder.java index db6177ab36f..1ba34d65b22 100644 --- a/core/src/main/java/org/elasticsearch/search/sort/FieldSortBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/sort/FieldSortBuilder.java @@ -26,6 +26,7 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.ObjectParser.ValueType; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.fielddata.IndexFieldData; import org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested; import org.elasticsearch.index.fielddata.IndexNumericFieldData; @@ -315,13 +316,13 @@ public class FieldSortBuilder extends SortBuilder { * Creates a new {@link FieldSortBuilder} from the query held by the {@link QueryParseContext} in * {@link org.elasticsearch.common.xcontent.XContent} format. * - * @param context the input parse context. The state on the parser contained in this context will be changed as a side effect of this + * @param parser the input parser. The state on the parser contained in this context will be changed as a side effect of this * method call * @param fieldName in some sort syntax variations the field name precedes the xContent object that specifies further parameters, e.g. * in '{ "foo": { "order" : "asc"} }'. When parsing the inner object, the field name can be passed in via this argument */ - public static FieldSortBuilder fromXContent(QueryParseContext context, String fieldName) throws IOException { - return PARSER.parse(context.parser(), new FieldSortBuilder(fieldName), context); + public static FieldSortBuilder fromXContent(XContentParser parser, String fieldName) throws IOException { + return PARSER.parse(parser, new FieldSortBuilder(fieldName), null); } private static ObjectParser PARSER = new ObjectParser<>(NAME); @@ -332,6 +333,6 @@ public class FieldSortBuilder extends SortBuilder { PARSER.declareString(FieldSortBuilder::unmappedType , UNMAPPED_TYPE); PARSER.declareString((b, v) -> b.order(SortOrder.fromString(v)) , ORDER_FIELD); PARSER.declareString((b, v) -> b.sortMode(SortMode.fromString(v)), SORT_MODE); - PARSER.declareObject(FieldSortBuilder::setNestedFilter, SortBuilder::parseNestedFilter, NESTED_FILTER_FIELD); + PARSER.declareObject(FieldSortBuilder::setNestedFilter, (p, c) -> SortBuilder.parseNestedFilter(p), NESTED_FILTER_FIELD); } } diff --git a/core/src/main/java/org/elasticsearch/search/sort/GeoDistanceSortBuilder.java b/core/src/main/java/org/elasticsearch/search/sort/GeoDistanceSortBuilder.java index ddb4edec990..cb6425e8b0d 100644 --- a/core/src/main/java/org/elasticsearch/search/sort/GeoDistanceSortBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/sort/GeoDistanceSortBuilder.java @@ -60,6 +60,7 @@ import java.util.List; import java.util.Locale; import java.util.Objects; +import static org.elasticsearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder; /** * A geo distance based sorting on a geo point like field. */ @@ -386,14 +387,13 @@ public class GeoDistanceSortBuilder extends SortBuilder * Creates a new {@link GeoDistanceSortBuilder} from the query held by the {@link QueryParseContext} in * {@link org.elasticsearch.common.xcontent.XContent} format. * - * @param context the input parse context. The state on the parser contained in this context will be changed as a + * @param parser the input parser. The state on the parser contained in this context will be changed as a * side effect of this method call * @param elementName in some sort syntax variations the field name precedes the xContent object that specifies * further parameters, e.g. in '{ "foo": { "order" : "asc"} }'. When parsing the inner object, * the field name can be passed in via this argument */ - public static GeoDistanceSortBuilder fromXContent(QueryParseContext context, String elementName) throws IOException { - XContentParser parser = context.parser(); + public static GeoDistanceSortBuilder fromXContent(XContentParser parser, String elementName) throws IOException { String fieldName = null; List geoPoints = new ArrayList<>(); DistanceUnit unit = DistanceUnit.DEFAULT; @@ -415,7 +415,7 @@ public class GeoDistanceSortBuilder extends SortBuilder fieldName = currentName; } else if (token == XContentParser.Token.START_OBJECT) { if (NESTED_FILTER_FIELD.match(currentName)) { - nestedFilter = context.parseInnerQueryBuilder(); + nestedFilter = parseInnerQueryBuilder(parser); } else { // the json in the format of -> field : { lat : 30, lon : 12 } if (fieldName != null && fieldName.equals(currentName) == false) { diff --git a/core/src/main/java/org/elasticsearch/search/sort/ScoreSortBuilder.java b/core/src/main/java/org/elasticsearch/search/sort/ScoreSortBuilder.java index 52f25301783..fb727da0a2a 100644 --- a/core/src/main/java/org/elasticsearch/search/sort/ScoreSortBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/sort/ScoreSortBuilder.java @@ -24,6 +24,7 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.search.DocValueFormat; @@ -76,13 +77,13 @@ public class ScoreSortBuilder extends SortBuilder { * Creates a new {@link ScoreSortBuilder} from the query held by the {@link QueryParseContext} in * {@link org.elasticsearch.common.xcontent.XContent} format. * - * @param context the input parse context. The state on the parser contained in this context will be changed as a side effect of this + * @param parser the input parser. The state on the parser contained in this context will be changed as a side effect of this * method call * @param fieldName in some sort syntax variations the field name precedes the xContent object that specifies further parameters, e.g. * in '{ "foo": { "order" : "asc"} }'. When parsing the inner object, the field name can be passed in via this argument */ - public static ScoreSortBuilder fromXContent(QueryParseContext context, String fieldName) throws IOException { - return PARSER.apply(context.parser(), context); + public static ScoreSortBuilder fromXContent(XContentParser parser, String fieldName) throws IOException { + return PARSER.apply(parser, null); } private static ObjectParser PARSER = new ObjectParser<>(NAME, ScoreSortBuilder::new); diff --git a/core/src/main/java/org/elasticsearch/search/sort/ScriptSortBuilder.java b/core/src/main/java/org/elasticsearch/search/sort/ScriptSortBuilder.java index 749d2d13ba4..8e743dbbcad 100644 --- a/core/src/main/java/org/elasticsearch/search/sort/ScriptSortBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/sort/ScriptSortBuilder.java @@ -32,6 +32,7 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.xcontent.ConstructingObjectParser; import org.elasticsearch.common.xcontent.ObjectParser.ValueType; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.fielddata.AbstractBinaryDocValues; import org.elasticsearch.index.fielddata.FieldData; import org.elasticsearch.index.fielddata.IndexFieldData; @@ -221,20 +222,20 @@ public class ScriptSortBuilder extends SortBuilder { PARSER.declareString((b, v) -> b.order(SortOrder.fromString(v)), ORDER_FIELD); PARSER.declareString((b, v) -> b.sortMode(SortMode.fromString(v)), SORTMODE_FIELD); PARSER.declareString(ScriptSortBuilder::setNestedPath , NESTED_PATH_FIELD); - PARSER.declareObject(ScriptSortBuilder::setNestedFilter, SortBuilder::parseNestedFilter, NESTED_FILTER_FIELD); + PARSER.declareObject(ScriptSortBuilder::setNestedFilter, (p,c) -> SortBuilder.parseNestedFilter(p), NESTED_FILTER_FIELD); } /** * Creates a new {@link ScriptSortBuilder} from the query held by the {@link QueryParseContext} in * {@link org.elasticsearch.common.xcontent.XContent} format. * - * @param context the input parse context. The state on the parser contained in this context will be changed as a side effect of this + * @param parser the input parser. The state on the parser contained in this context will be changed as a side effect of this * method call * @param elementName in some sort syntax variations the field name precedes the xContent object that specifies further parameters, e.g. * in '{ "foo": { "order" : "asc"} }'. When parsing the inner object, the field name can be passed in via this argument */ - public static ScriptSortBuilder fromXContent(QueryParseContext context, String elementName) throws IOException { - return PARSER.apply(context.parser(), context); + public static ScriptSortBuilder fromXContent(XContentParser parser, String elementName) throws IOException { + return PARSER.apply(parser, null); } diff --git a/core/src/main/java/org/elasticsearch/search/sort/SortBuilder.java b/core/src/main/java/org/elasticsearch/search/sort/SortBuilder.java index 962e8732551..984e6b8a983 100644 --- a/core/src/main/java/org/elasticsearch/search/sort/SortBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/sort/SortBuilder.java @@ -32,7 +32,6 @@ import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested; import org.elasticsearch.index.mapper.ObjectMapper; import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.index.query.QueryShardException; import org.elasticsearch.search.DocValueFormat; @@ -46,6 +45,7 @@ import java.util.Objects; import java.util.Optional; import static java.util.Collections.unmodifiableMap; +import static org.elasticsearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder; public abstract class SortBuilder> extends ToXContentToBytes implements NamedWriteable { @@ -89,14 +89,13 @@ public abstract class SortBuilder> extends ToXContentTo return this.order; } - public static List> fromXContent(QueryParseContext context) throws IOException { + public static List> fromXContent(XContentParser parser) throws IOException { List> sortFields = new ArrayList<>(2); - XContentParser parser = context.parser(); XContentParser.Token token = parser.currentToken(); if (token == XContentParser.Token.START_ARRAY) { while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { if (token == XContentParser.Token.START_OBJECT) { - parseCompoundSortField(context, sortFields); + parseCompoundSortField(parser, sortFields); } else if (token == XContentParser.Token.VALUE_STRING) { String fieldName = parser.text(); sortFields.add(fieldOrScoreSort(fieldName)); @@ -109,7 +108,7 @@ public abstract class SortBuilder> extends ToXContentTo String fieldName = parser.text(); sortFields.add(fieldOrScoreSort(fieldName)); } else if (token == XContentParser.Token.START_OBJECT) { - parseCompoundSortField(context, sortFields); + parseCompoundSortField(parser, sortFields); } else { throw new IllegalArgumentException("malformed sort format, either start with array, object, or an actual string"); } @@ -124,10 +123,9 @@ public abstract class SortBuilder> extends ToXContentTo } } - private static void parseCompoundSortField(QueryParseContext context, List> sortFields) + private static void parseCompoundSortField(XContentParser parser, List> sortFields) throws IOException { XContentParser.Token token; - XContentParser parser = context.parser(); while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { String fieldName = parser.currentName(); @@ -137,9 +135,9 @@ public abstract class SortBuilder> extends ToXContentTo sortFields.add(fieldOrScoreSort(fieldName).order(order)); } else { if (PARSERS.containsKey(fieldName)) { - sortFields.add(PARSERS.get(fieldName).fromXContent(context, fieldName)); + sortFields.add(PARSERS.get(fieldName).fromXContent(parser, fieldName)); } else { - sortFields.add(FieldSortBuilder.fromXContent(context, fieldName)); + sortFields.add(FieldSortBuilder.fromXContent(parser, fieldName)); } } } @@ -201,9 +199,9 @@ public abstract class SortBuilder> extends ToXContentTo return nested; } - protected static QueryBuilder parseNestedFilter(XContentParser parser, QueryParseContext context) { + protected static QueryBuilder parseNestedFilter(XContentParser parser) { try { - return context.parseInnerQueryBuilder(); + return parseInnerQueryBuilder(parser); } catch (Exception e) { throw new ParsingException(parser.getTokenLocation(), "Expected " + NESTED_FILTER_FIELD.getPreferredName() + " element.", e); } @@ -211,6 +209,6 @@ public abstract class SortBuilder> extends ToXContentTo @FunctionalInterface private interface Parser> { - T fromXContent(QueryParseContext context, String elementName) throws IOException; + T fromXContent(XContentParser parser, String elementName) throws IOException; } } diff --git a/core/src/main/java/org/elasticsearch/search/suggest/phrase/PhraseSuggester.java b/core/src/main/java/org/elasticsearch/search/suggest/phrase/PhraseSuggester.java index 2ab60b0d8ec..ff06dfe81a9 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/phrase/PhraseSuggester.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/phrase/PhraseSuggester.java @@ -28,15 +28,14 @@ import org.apache.lucene.search.spell.DirectSpellChecker; import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRefBuilder; import org.apache.lucene.util.CharsRefBuilder; -import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.text.Text; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.index.query.AbstractQueryBuilder; import org.elasticsearch.index.query.ParsedQuery; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryShardContext; -import org.elasticsearch.script.ExecutableScript; import org.elasticsearch.script.TemplateScript; import org.elasticsearch.search.suggest.Suggest.Suggestion; import org.elasticsearch.search.suggest.Suggest.Suggestion.Entry; @@ -49,7 +48,6 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; -import java.util.function.Function; public final class PhraseSuggester extends Suggester { private final BytesRef SEPARATOR = new BytesRef(" "); @@ -119,7 +117,7 @@ public final class PhraseSuggester extends Suggester { final String querySource = scriptFactory.newInstance(vars).execute(); try (XContentParser parser = XContentFactory.xContent(querySource).createParser(shardContext.getXContentRegistry(), querySource)) { - QueryBuilder innerQueryBuilder = shardContext.newParseContext(parser).parseInnerQueryBuilder(); + QueryBuilder innerQueryBuilder = AbstractQueryBuilder.parseInnerQueryBuilder(parser); final ParsedQuery parsedQuery = shardContext.toQuery(innerQueryBuilder); collateMatch = Lucene.exists(searcher, parsedQuery.query()); } diff --git a/core/src/test/java/org/elasticsearch/action/search/MultiSearchRequestTests.java b/core/src/test/java/org/elasticsearch/action/search/MultiSearchRequestTests.java index 35210ec70e7..b2903e24de7 100644 --- a/core/src/test/java/org/elasticsearch/action/search/MultiSearchRequestTests.java +++ b/core/src/test/java/org/elasticsearch/action/search/MultiSearchRequestTests.java @@ -27,7 +27,6 @@ import org.elasticsearch.common.xcontent.NamedXContentRegistry; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.MatchAllQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.search.RestMultiSearchAction; import org.elasticsearch.test.ESTestCase; @@ -182,6 +181,6 @@ public class MultiSearchRequestTests extends ESTestCase { @Override protected NamedXContentRegistry xContentRegistry() { return new NamedXContentRegistry(singletonList(new NamedXContentRegistry.Entry(QueryBuilder.class, - new ParseField(MatchAllQueryBuilder.NAME), (p, c) -> MatchAllQueryBuilder.fromXContent((QueryParseContext) c)))); + new ParseField(MatchAllQueryBuilder.NAME), (p, c) -> MatchAllQueryBuilder.fromXContent(p)))); } } diff --git a/core/src/test/java/org/elasticsearch/index/query/InnerHitBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/InnerHitBuilderTests.java index 20b0fc1e16d..78975bf7b17 100644 --- a/core/src/test/java/org/elasticsearch/index/query/InnerHitBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/InnerHitBuilderTests.java @@ -123,8 +123,7 @@ public class InnerHitBuilderTests extends ESTestCase { //fields is printed out as an object but parsed into a List where order matters, we disable shuffling XContentBuilder shuffled = shuffleXContent(builder, "fields"); XContentParser parser = createParser(shuffled); - QueryParseContext context = new QueryParseContext(parser); - InnerHitBuilder secondInnerHits = InnerHitBuilder.fromXContent(context); + InnerHitBuilder secondInnerHits = InnerHitBuilder.fromXContent(parser); assertThat(innerHit, not(sameInstance(secondInnerHits))); assertThat(innerHit, equalTo(secondInnerHits)); assertThat(innerHit.hashCode(), equalTo(secondInnerHits.hashCode())); diff --git a/core/src/test/java/org/elasticsearch/index/query/QueryParseContextTests.java b/core/src/test/java/org/elasticsearch/index/query/QueryParseContextTests.java index 3f8837fe745..5bd6a8a5826 100644 --- a/core/src/test/java/org/elasticsearch/index/query/QueryParseContextTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/QueryParseContextTests.java @@ -36,6 +36,7 @@ import org.junit.BeforeClass; import java.io.IOException; import static java.util.Collections.emptyList; +import static org.elasticsearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder; public class QueryParseContextTests extends ESTestCase { private static NamedXContentRegistry xContentRegistry; @@ -64,40 +65,11 @@ public class QueryParseContextTests extends ESTestCase { this.threadContext.close(); } - public void testParseTopLevelBuilder() throws IOException { - QueryBuilder query = new MatchQueryBuilder("foo", "bar"); - String requestBody = "{ \"query\" : " + query.toString() + "}"; - try (XContentParser parser = createParser(JsonXContent.jsonXContent, requestBody)) { - QueryParseContext context = new QueryParseContext(parser); - QueryBuilder actual = context.parseTopLevelQueryBuilder(); - assertEquals(query, actual); - } - } - - public void testParseTopLevelBuilderEmptyObject() throws IOException { - String requestBody = "{}"; - try (XContentParser parser = createParser(JsonXContent.jsonXContent, requestBody)) { - QueryParseContext context = new QueryParseContext(parser); - QueryBuilder query = context.parseTopLevelQueryBuilder(); - assertNull(query); - } - } - - public void testParseTopLevelBuilderUnknownParameter() throws IOException { - String requestBody = "{ \"foo\" : \"bar\"}"; - try (XContentParser parser = createParser(JsonXContent.jsonXContent, requestBody)) { - QueryParseContext context = new QueryParseContext(parser); - ParsingException exception = expectThrows(ParsingException.class, () -> context.parseTopLevelQueryBuilder()); - assertEquals("request does not support [foo]", exception.getMessage()); - } - } - public void testParseInnerQueryBuilder() throws IOException { QueryBuilder query = new MatchQueryBuilder("foo", "bar"); String source = query.toString(); try (XContentParser parser = createParser(JsonXContent.jsonXContent, source)) { - QueryParseContext context = new QueryParseContext(parser); - QueryBuilder actual = context.parseInnerQueryBuilder(); + QueryBuilder actual = parseInnerQueryBuilder(parser); assertEquals(query, actual); } } @@ -107,29 +79,26 @@ public class QueryParseContextTests extends ESTestCase { try (XContentParser parser = createParser(JsonXContent.jsonXContent, source)) { parser.nextToken(); parser.nextToken(); // don't start with START_OBJECT to provoke exception - QueryParseContext context = new QueryParseContext(parser); - ParsingException exception = expectThrows(ParsingException.class, () -> context.parseInnerQueryBuilder()); + ParsingException exception = expectThrows(ParsingException.class, () -> parseInnerQueryBuilder(parser)); assertEquals("[_na] query malformed, must start with start_object", exception.getMessage()); } source = "{}"; try (XContentParser parser = createParser(JsonXContent.jsonXContent, source)) { - QueryParseContext context = new QueryParseContext(parser); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> context.parseInnerQueryBuilder()); + IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> parseInnerQueryBuilder(parser)); assertEquals("query malformed, empty clause found at [1:2]", exception.getMessage()); } source = "{ \"foo\" : \"bar\" }"; try (XContentParser parser = createParser(JsonXContent.jsonXContent, source)) { QueryParseContext context = new QueryParseContext(parser); - ParsingException exception = expectThrows(ParsingException.class, () -> context.parseInnerQueryBuilder()); + ParsingException exception = expectThrows(ParsingException.class, () -> parseInnerQueryBuilder(parser)); assertEquals("[foo] query malformed, no start_object after query name", exception.getMessage()); } source = "{ \"foo\" : {} }"; try (XContentParser parser = createParser(JsonXContent.jsonXContent, source)) { - QueryParseContext context = new QueryParseContext(parser); - ParsingException exception = expectThrows(ParsingException.class, () -> context.parseInnerQueryBuilder()); + ParsingException exception = expectThrows(ParsingException.class, () -> parseInnerQueryBuilder(parser)); assertEquals("no [query] registered for [foo]", exception.getMessage()); } } diff --git a/core/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java index 038eadeff7d..c32eb425739 100644 --- a/core/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java @@ -65,6 +65,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import static org.elasticsearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder; import static org.elasticsearch.index.query.QueryBuilders.queryStringQuery; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertBooleanSubQuery; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertDisjunctionSubQuery; @@ -521,7 +522,7 @@ public class QueryStringQueryBuilderTests extends AbstractQueryTestCase queryBuilder.toQuery(createShardContext())); assertThat(e.getMessage(), containsString("Determinizing [ac]*")); @@ -545,8 +546,7 @@ public class QueryStringQueryBuilderTests extends AbstractQueryTestCase { builder.startObject(NAME).endObject(); } - public static DummyQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException { - XContentParser.Token token = parseContext.parser().nextToken(); + public static DummyQueryBuilder fromXContent(XContentParser parser) throws IOException { + XContentParser.Token token = parser.nextToken(); assert token == XContentParser.Token.END_OBJECT; return new DummyQueryBuilder(); } diff --git a/core/src/test/java/org/elasticsearch/rest/action/RestActionsTests.java b/core/src/test/java/org/elasticsearch/rest/action/RestActionsTests.java new file mode 100644 index 00000000000..7272243e3cf --- /dev/null +++ b/core/src/test/java/org/elasticsearch/rest/action/RestActionsTests.java @@ -0,0 +1,82 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.rest.action; + +import org.elasticsearch.common.ParsingException; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.NamedXContentRegistry; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.json.JsonXContent; +import org.elasticsearch.index.query.MatchQueryBuilder; +import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.search.SearchModule; +import org.elasticsearch.test.ESTestCase; +import org.junit.AfterClass; +import org.junit.BeforeClass; + +import java.io.IOException; + +import static java.util.Collections.emptyList; + +public class RestActionsTests extends ESTestCase { + + private static NamedXContentRegistry xContentRegistry; + + @BeforeClass + public static void init() { + xContentRegistry = new NamedXContentRegistry(new SearchModule(Settings.EMPTY, false, emptyList()).getNamedXContents()); + } + + @AfterClass + public static void cleanup() { + xContentRegistry = null; + } + + public void testParseTopLevelBuilder() throws IOException { + QueryBuilder query = new MatchQueryBuilder("foo", "bar"); + String requestBody = "{ \"query\" : " + query.toString() + "}"; + try (XContentParser parser = createParser(JsonXContent.jsonXContent, requestBody)) { + QueryBuilder actual = RestActions.getQueryContent(parser); + assertEquals(query, actual); + } + } + + public void testParseTopLevelBuilderEmptyObject() throws IOException { + String requestBody = "{}"; + try (XContentParser parser = createParser(JsonXContent.jsonXContent, requestBody)) { + QueryBuilder query = RestActions.getQueryContent(parser); + assertNull(query); + } + } + + public void testParseTopLevelBuilderUnknownParameter() throws IOException { + String requestBody = "{ \"foo\" : \"bar\"}"; + try (XContentParser parser = createParser(JsonXContent.jsonXContent, requestBody)) { + ParsingException exception = expectThrows(ParsingException.class, () -> RestActions.getQueryContent(parser)); + assertEquals("request does not support [foo]", exception.getMessage()); + } + } + + @Override + protected NamedXContentRegistry xContentRegistry() { + return xContentRegistry; + } + +} diff --git a/core/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightBuilderTests.java b/core/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightBuilderTests.java index 2d1fb3d986c..b49a211a1c8 100644 --- a/core/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightBuilderTests.java @@ -43,7 +43,6 @@ import org.elasticsearch.index.mapper.TextFieldMapper; import org.elasticsearch.index.query.IdsQueryBuilder; import org.elasticsearch.index.query.MatchAllQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.index.query.TermQueryBuilder; import org.elasticsearch.search.SearchModule; @@ -138,11 +137,10 @@ public class HighlightBuilderTests extends ESTestCase { } XContentParser parser = createParser(shuffled); - QueryParseContext context = new QueryParseContext(parser); parser.nextToken(); HighlightBuilder secondHighlightBuilder; try { - secondHighlightBuilder = HighlightBuilder.fromXContent(context); + secondHighlightBuilder = HighlightBuilder.fromXContent(parser); } catch (RuntimeException e) { throw new RuntimeException("Error parsing " + highlightBuilder, e); } @@ -179,8 +177,7 @@ public class HighlightBuilderTests extends ESTestCase { private T expectParseThrows(Class exceptionClass, String highlightElement) throws IOException { XContentParser parser = createParser(JsonXContent.jsonXContent, highlightElement); - QueryParseContext context = new QueryParseContext(parser); - return expectThrows(exceptionClass, () -> HighlightBuilder.fromXContent(context)); + return expectThrows(exceptionClass, () -> HighlightBuilder.fromXContent(parser)); } /** @@ -390,8 +387,7 @@ public class HighlightBuilderTests extends ESTestCase { "}\n"; XContentParser parser = createParser(JsonXContent.jsonXContent, highlightElement); - QueryParseContext context = new QueryParseContext(parser); - HighlightBuilder highlightBuilder = HighlightBuilder.fromXContent(context); + HighlightBuilder highlightBuilder = HighlightBuilder.fromXContent(parser); assertArrayEquals("setting tags_schema 'styled' should alter pre_tags", HighlightBuilder.DEFAULT_STYLED_PRE_TAG, highlightBuilder.preTags()); assertArrayEquals("setting tags_schema 'styled' should alter post_tags", HighlightBuilder.DEFAULT_STYLED_POST_TAGS, @@ -402,8 +398,7 @@ public class HighlightBuilderTests extends ESTestCase { "}\n"; parser = createParser(JsonXContent.jsonXContent, highlightElement); - context = new QueryParseContext(parser); - highlightBuilder = HighlightBuilder.fromXContent(context); + highlightBuilder = HighlightBuilder.fromXContent(parser); assertArrayEquals("setting tags_schema 'default' should alter pre_tags", HighlightBuilder.DEFAULT_PRE_TAGS, highlightBuilder.preTags()); assertArrayEquals("setting tags_schema 'default' should alter post_tags", HighlightBuilder.DEFAULT_POST_TAGS, @@ -423,22 +418,19 @@ public class HighlightBuilderTests extends ESTestCase { String highlightElement = "{ }"; XContentParser parser = createParser(JsonXContent.jsonXContent, highlightElement); - QueryParseContext context = new QueryParseContext(parser); - HighlightBuilder highlightBuilder = HighlightBuilder.fromXContent(context); + HighlightBuilder highlightBuilder = HighlightBuilder.fromXContent(parser); assertEquals("expected plain HighlightBuilder", new HighlightBuilder(), highlightBuilder); highlightElement = "{ \"fields\" : { } }"; parser = createParser(JsonXContent.jsonXContent, highlightElement); - context = new QueryParseContext(parser); - highlightBuilder = HighlightBuilder.fromXContent(context); + highlightBuilder = HighlightBuilder.fromXContent(parser); assertEquals("defining no field should return plain HighlightBuilder", new HighlightBuilder(), highlightBuilder); highlightElement = "{ \"fields\" : { \"foo\" : { } } }"; parser = createParser(JsonXContent.jsonXContent, highlightElement); - context = new QueryParseContext(parser); - highlightBuilder = HighlightBuilder.fromXContent(context); + highlightBuilder = HighlightBuilder.fromXContent(parser); assertEquals("expected HighlightBuilder with field", new HighlightBuilder().field(new Field("foo")), highlightBuilder); } diff --git a/core/src/test/java/org/elasticsearch/search/internal/ShardSearchTransportRequestTests.java b/core/src/test/java/org/elasticsearch/search/internal/ShardSearchTransportRequestTests.java index 7a0e10af99c..f1a890f7860 100644 --- a/core/src/test/java/org/elasticsearch/search/internal/ShardSearchTransportRequestTests.java +++ b/core/src/test/java/org/elasticsearch/search/internal/ShardSearchTransportRequestTests.java @@ -40,7 +40,6 @@ import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; -import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.index.query.RandomQueryBuilder; import org.elasticsearch.index.shard.ShardId; @@ -50,6 +49,7 @@ import org.elasticsearch.search.AbstractSearchTestCase; import java.io.IOException; import java.util.Base64; +import static org.elasticsearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder; import static org.elasticsearch.index.query.QueryBuilders.termQuery; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; @@ -165,7 +165,7 @@ public class ShardSearchTransportRequestTests extends AbstractSearchTestCase { public QueryBuilder aliasFilter(IndexMetaData indexMetaData, String... aliasNames) { CheckedFunction filterParser = bytes -> { try (XContentParser parser = XContentFactory.xContent(bytes).createParser(xContentRegistry(), bytes)) { - return new QueryParseContext(parser).parseInnerQueryBuilder(); + return parseInnerQueryBuilder(parser); } }; return ShardSearchRequest.parseAliasFilter(filterParser, indexMetaData, aliasNames); diff --git a/core/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java b/core/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java index 8e6b9f45cc6..106c3eb05ad 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java +++ b/core/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java @@ -45,13 +45,11 @@ import org.elasticsearch.index.mapper.ObjectMapper.Nested; import org.elasticsearch.index.query.IdsQueryBuilder; import org.elasticsearch.index.query.MatchAllQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.index.query.TermQueryBuilder; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.indices.fielddata.cache.IndicesFieldDataCache; import org.elasticsearch.script.MockScriptEngine; -import org.elasticsearch.script.ScriptContext; import org.elasticsearch.script.ScriptEngine; import org.elasticsearch.script.ScriptModule; import org.elasticsearch.script.ScriptService; @@ -63,7 +61,6 @@ import org.junit.AfterClass; import org.junit.BeforeClass; import java.io.IOException; -import java.nio.file.Path; import java.util.Collections; import java.util.Map; import java.util.function.Function; @@ -106,7 +103,7 @@ public abstract class AbstractSortTestCase> extends EST protected abstract T mutate(T original) throws IOException; /** Parse the sort from xContent. Just delegate to the SortBuilder's static fromXContent method. */ - protected abstract T fromXContent(QueryParseContext context, String fieldName) throws IOException; + protected abstract T fromXContent(XContentParser parser, String fieldName) throws IOException; /** * Test that creates new sort from a random test sort and checks both for equality @@ -131,8 +128,7 @@ public abstract class AbstractSortTestCase> extends EST String elementName = itemParser.currentName(); itemParser.nextToken(); - QueryParseContext context = new QueryParseContext(itemParser); - T parsedItem = fromXContent(context, elementName); + T parsedItem = fromXContent(itemParser, elementName); assertNotSame(testItem, parsedItem); assertEquals(testItem, parsedItem); assertEquals(testItem.hashCode(), parsedItem.hashCode()); @@ -241,7 +237,7 @@ public abstract class AbstractSortTestCase> extends EST private T copy(T original) throws IOException { /* The cast below is required to make Java 9 happy. Java 8 infers the T in copyWriterable to be the same as AbstractSortTestCase's * T but Java 9 infers it to be SortBuilder. */ - return (T) copyWriteable(original, namedWriteableRegistry, + return copyWriteable(original, namedWriteableRegistry, namedWriteableRegistry.getReader(SortBuilder.class, original.getWriteableName())); } } diff --git a/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java b/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java index 57e79ce98c3..eda187d916f 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java @@ -22,7 +22,6 @@ package org.elasticsearch.search.sort; import org.apache.lucene.search.SortField; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.json.JsonXContent; -import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.search.DocValueFormat; import java.io.IOException; @@ -133,10 +132,8 @@ public class FieldSortBuilderTests extends AbstractSortTestCase GeoDistanceSortBuilder.fromXContent(context, "")); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, + () -> GeoDistanceSortBuilder.fromXContent(itemParser, "")); assertEquals("sort_mode [sum] isn't supported for sorting by geo distance", e.getMessage()); } @@ -231,9 +229,7 @@ public class GeoDistanceSortBuilderTests extends AbstractSortTestCase ScriptSortBuilder.fromXContent(context, null)); + Exception e = expectThrows(IllegalArgumentException.class, () -> ScriptSortBuilder.fromXContent(parser, null)); assertEquals("[_script] unknown field [bad_field], parser not found", e.getMessage()); } @@ -230,8 +226,7 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase ScriptSortBuilder.fromXContent(context, null)); + Exception e = expectThrows(IllegalArgumentException.class, () -> ScriptSortBuilder.fromXContent(parser, null)); assertEquals("[_script] unknown field [bad_field], parser not found", e.getMessage()); } @@ -242,8 +237,7 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase ScriptSortBuilder.fromXContent(context, null)); + Exception e = expectThrows(IllegalArgumentException.class, () -> ScriptSortBuilder.fromXContent(parser, null)); assertEquals("[_script] script doesn't support values of type: START_ARRAY", e.getMessage()); } @@ -258,7 +252,7 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase> parseSort(String jsonString) throws IOException { XContentParser itemParser = createParser(JsonXContent.jsonXContent, jsonString); - QueryParseContext context = new QueryParseContext(itemParser); assertEquals(XContentParser.Token.START_OBJECT, itemParser.nextToken()); assertEquals(XContentParser.Token.FIELD_NAME, itemParser.nextToken()); assertEquals("sort", itemParser.currentName()); itemParser.nextToken(); - return SortBuilder.fromXContent(context); + return SortBuilder.fromXContent(itemParser); } } diff --git a/docs/reference/migration/migrate_6_0/rest.asciidoc b/docs/reference/migration/migrate_6_0/rest.asciidoc index 5f0c88f13cf..3be865ffd9a 100644 --- a/docs/reference/migration/migrate_6_0/rest.asciidoc +++ b/docs/reference/migration/migrate_6_0/rest.asciidoc @@ -68,3 +68,9 @@ This comma-separation for retrieving multiple pieces of information has been removed.. `GET /_all` can be used to retrieve all aliases, settings, and mappings for all indices. In order to retrieve only the mappings for an index, `GET /myindex/_mappings` (or `_aliases`, or `_settings`). + +==== Dissallow using `_cache` and `_cache_key` + +The `_cache` and `_cache_key` options in queries have been deprecated since version 2.0.0 and +have been ignored since then, issuing a deprecation warning. These options have now been completely +removed, so using them now will throw an error. \ No newline at end of file diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasChildQueryBuilder.java b/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasChildQueryBuilder.java index 0e0e1f1748c..ee362b9bca7 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasChildQueryBuilder.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasChildQueryBuilder.java @@ -45,7 +45,6 @@ import org.elasticsearch.index.query.InnerHitBuilder; import org.elasticsearch.index.query.InnerHitContextBuilder; import org.elasticsearch.index.query.NestedQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.query.QueryRewriteContext; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.index.query.QueryShardException; @@ -242,8 +241,7 @@ public class HasChildQueryBuilder extends AbstractQueryBuilder { throw new UnsupportedOperationException(); }) - .newParseContext(sourceParser); - assertThat(qsc.parseInnerQueryBuilder(), equalTo(expected)); + assertThat(parseInnerQueryBuilder(sourceParser), equalTo(expected)); } public void testEmptyName() throws Exception { diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/TestDeprecatedQueryBuilder.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/TestDeprecatedQueryBuilder.java index cf8b2539f07..5966ae08ac4 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/TestDeprecatedQueryBuilder.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/TestDeprecatedQueryBuilder.java @@ -29,7 +29,6 @@ import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.query.AbstractQueryBuilder; -import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.query.QueryShardContext; import java.io.IOException; @@ -65,9 +64,7 @@ public class TestDeprecatedQueryBuilder extends AbstractQueryBuilder> extends ESTestCase { public static final String STRING_FIELD_NAME = "mapped_string"; @@ -545,8 +547,7 @@ public abstract class AbstractQueryTestCase> } protected static QueryBuilder parseQuery(XContentParser parser) throws IOException { - QueryParseContext context = createParseContext(parser); - QueryBuilder parseInnerQueryBuilder = context.parseInnerQueryBuilder(); + QueryBuilder parseInnerQueryBuilder = parseInnerQueryBuilder(parser); assertNull(parser.nextToken()); return parseInnerQueryBuilder; }