From 38e1ad5596874a472bd5d29bdfba098b8ba52b52 Mon Sep 17 00:00:00 2001 From: Isabel Drost-Fromm Date: Wed, 11 Nov 2015 12:55:38 +0100 Subject: [PATCH] Switches from manual field parsing to ParseField This switches query parsing from manual field parsing to using ParseField. Also adds unit tests for each query that check original json can be parsed into query builders. Relates to #8964 --- .../index/query/AbstractQueryBuilder.java | 8 +- .../index/query/BaseTermQueryBuilder.java | 5 +- .../index/query/BoolQueryBuilder.java | 14 +-- .../index/query/BoolQueryParser.java | 43 +++++---- .../index/query/BoostingQueryBuilder.java | 6 +- .../index/query/BoostingQueryParser.java | 15 ++-- .../index/query/CommonTermsQueryBuilder.java | 18 ++-- .../index/query/CommonTermsQueryParser.java | 35 +++++--- .../query/ConstantScoreQueryBuilder.java | 2 +- .../index/query/ConstantScoreQueryParser.java | 6 +- .../index/query/DisMaxQueryBuilder.java | 4 +- .../index/query/DisMaxQueryParser.java | 14 +-- .../index/query/ExistsQueryBuilder.java | 2 +- .../index/query/ExistsQueryParser.java | 9 +- .../query/FieldMaskingSpanQueryBuilder.java | 4 +- .../query/FieldMaskingSpanQueryParser.java | 12 ++- .../index/query/FuzzyQueryBuilder.java | 22 ++--- .../index/query/FuzzyQueryParser.java | 24 +++-- .../query/GeoBoundingBoxQueryBuilder.java | 16 ++-- .../query/GeoBoundingBoxQueryParser.java | 72 ++++++--------- .../index/query/GeoDistanceQueryBuilder.java | 12 +-- .../index/query/GeoDistanceQueryParser.java | 27 ++++-- .../index/query/HasChildQueryBuilder.java | 10 +-- .../index/query/HasChildQueryParser.java | 21 +++-- .../index/query/HasParentQueryBuilder.java | 6 +- .../index/query/HasParentQueryParser.java | 17 ++-- .../index/query/IdsQueryBuilder.java | 4 +- .../index/query/IdsQueryParser.java | 15 ++-- .../index/query/IndicesQueryBuilder.java | 6 +- .../index/query/IndicesQueryParser.java | 15 ++-- .../index/query/MatchAllQueryParser.java | 4 +- .../index/query/MatchNoneQueryParser.java | 4 +- .../index/query/MatchQueryBuilder.java | 31 ++++--- .../index/query/MatchQueryParser.java | 55 +++++++----- .../index/query/MissingQueryBuilder.java | 6 +- .../index/query/MissingQueryParser.java | 15 ++-- .../index/query/MultiMatchQueryBuilder.java | 30 +++---- .../index/query/MultiMatchQueryParser.java | 52 +++++++---- .../index/query/NestedQueryBuilder.java | 6 +- .../index/query/NestedQueryParser.java | 17 ++-- .../index/query/PrefixQueryBuilder.java | 4 +- .../index/query/PrefixQueryParser.java | 15 ++-- .../index/query/QueryStringQueryBuilder.java | 50 +++++------ .../index/query/QueryStringQueryParser.java | 81 +++++++++++------ .../index/query/RangeQueryBuilder.java | 12 +-- .../index/query/RangeQueryParser.java | 38 +++++--- .../index/query/RegexpQueryBuilder.java | 20 ++--- .../index/query/RegexpQueryParser.java | 21 +++-- .../index/query/ScriptQueryParser.java | 9 +- .../index/query/SimpleQueryStringBuilder.java | 20 ++--- .../index/query/SimpleQueryStringParser.java | 36 +++++--- .../query/SpanContainingQueryBuilder.java | 4 +- .../query/SpanContainingQueryParser.java | 12 ++- .../index/query/SpanFirstQueryBuilder.java | 4 +- .../index/query/SpanFirstQueryParser.java | 12 ++- .../query/SpanMultiTermQueryBuilder.java | 2 +- .../index/query/SpanMultiTermQueryParser.java | 13 +-- .../index/query/SpanNearQueryBuilder.java | 8 +- .../index/query/SpanNearQueryParser.java | 18 ++-- .../index/query/SpanNotQueryBuilder.java | 8 +- .../index/query/SpanNotQueryParser.java | 21 +++-- .../index/query/SpanOrQueryBuilder.java | 2 +- .../index/query/SpanOrQueryParser.java | 9 +- .../index/query/SpanTermQueryBuilder.java | 2 +- .../index/query/SpanTermQueryParser.java | 11 ++- .../index/query/SpanWithinQueryBuilder.java | 4 +- .../index/query/SpanWithinQueryParser.java | 12 ++- .../index/query/TermQueryBuilder.java | 2 +- .../index/query/TermQueryParser.java | 26 +++--- .../index/query/TermsQueryBuilder.java | 2 +- .../index/query/TermsQueryParser.java | 4 +- .../index/query/TypeQueryBuilder.java | 2 +- .../index/query/TypeQueryParser.java | 9 +- .../index/query/WildcardQueryBuilder.java | 6 +- .../index/query/WildcardQueryParser.java | 15 ++-- .../index/query/WrapperQueryBuilder.java | 2 +- .../index/query/WrapperQueryParser.java | 5 +- .../index/query/AbstractQueryTestCase.java | 52 ++++++++++- .../index/query/BoolQueryBuilderTests.java | 62 +++++++++++++ .../query/BoostingQueryBuilderTests.java | 34 +++++++ .../query/CommonTermsQueryBuilderTests.java | 29 ++++++ .../query/ConstantScoreQueryBuilderTests.java | 22 +++++ .../index/query/DisMaxQueryBuilderTests.java | 32 +++++++ .../index/query/ExistsQueryBuilderTests.java | 16 ++++ .../FieldMaskingSpanQueryBuilderTests.java | 25 ++++++ .../index/query/FuzzyQueryBuilderTests.java | 20 +++++ .../GeoBoundingBoxQueryBuilderTests.java | 27 +++++- .../query/GeoDistanceQueryBuilderTests.java | 19 ++++ .../query/GeoDistanceRangeQueryTests.java | 21 +++++ .../query/GeoPolygonQueryBuilderTests.java | 17 ++++ .../query/GeoShapeQueryBuilderTests.java | 19 ++++ .../query/GeohashCellQueryBuilderTests.java | 15 ++++ .../query/HasChildQueryBuilderTests.java | 39 ++------ .../query/HasParentQueryBuilderTests.java | 23 +++++ .../index/query/IdsQueryBuilderTests.java | 15 ++++ .../index/query/IndicesQueryBuilderTests.java | 31 +++++++ .../query/MatchAllQueryBuilderTests.java | 12 +++ .../query/MatchNoneQueryBuilderTests.java | 12 +++ .../index/query/MatchQueryBuilderTests.java | 89 +++++++++++++++++++ .../index/query/MissingQueryBuilderTests.java | 19 ++++ .../query/MoreLikeThisQueryBuilderTests.java | 35 ++++++++ .../query/MultiMatchQueryBuilderTests.java | 26 ++++++ .../index/query/NestedQueryBuilderTests.java | 49 ++++++++++ .../index/query/PrefixQueryBuilderTests.java | 12 +++ .../query/QueryStringQueryBuilderTests.java | 31 +++++++ .../index/query/RangeQueryBuilderTests.java | 22 +++++ .../index/query/RegexpQueryBuilderTests.java | 20 +++++ .../index/query/ScriptQueryBuilderTests.java | 20 +++++ .../query/SimpleQueryStringBuilderTests.java | 26 +++++- .../SpanContainingQueryBuilderTests.java | 46 ++++++++++ .../query/SpanFirstQueryBuilderTests.java | 24 +++++ .../query/SpanMultiTermQueryBuilderTests.java | 23 +++++ .../query/SpanNearQueryBuilderTests.java | 42 +++++++++ .../index/query/SpanNotQueryBuilderTests.java | 48 ++++++++++ .../index/query/SpanOrQueryBuilderTests.java | 36 ++++++++ .../query/SpanTermQueryBuilderTests.java | 11 +++ .../query/SpanWithinQueryBuilderTests.java | 46 ++++++++++ .../index/query/TermQueryBuilderTests.java | 17 ++++ .../index/query/TermsQueryBuilderTests.java | 15 ++++ .../index/query/TypeQueryBuilderTests.java | 15 ++++ .../query/WildcardQueryBuilderTests.java | 11 +++ .../index/query/WrapperQueryBuilderTests.java | 23 +++++ .../FunctionScoreQueryBuilderTests.java | 31 +++++++ 123 files changed, 1942 insertions(+), 542 deletions(-) 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 1f92f53ec07..79ba3804ecd 100644 --- a/core/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java @@ -67,9 +67,9 @@ public abstract class AbstractQueryBuilder exte protected abstract void doXContent(XContentBuilder builder, Params params) throws IOException; protected void printBoostAndQueryName(XContentBuilder builder) throws IOException { - builder.field("boost", boost); + builder.field(BOOST_FIELD.getPreferredName(), boost); if (queryName != null) { - builder.field("_name", queryName); + builder.field(NAME_FIELD.getPreferredName(), queryName); } } @@ -107,7 +107,7 @@ public abstract class AbstractQueryBuilder exte protected abstract Query doToQuery(QueryShardContext context) throws IOException; /** - * Returns the query name for the query. + * Sets the query name for the query. */ @SuppressWarnings("unchecked") @Override @@ -117,7 +117,7 @@ public abstract class AbstractQueryBuilder exte } /** - * Sets the query name for the query. + * Returns the query name for the query. */ @Override public final String queryName() { diff --git a/core/src/main/java/org/elasticsearch/index/query/BaseTermQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/BaseTermQueryBuilder.java index 06666b74120..fd5f93f6f7f 100644 --- a/core/src/main/java/org/elasticsearch/index/query/BaseTermQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/BaseTermQueryBuilder.java @@ -20,6 +20,7 @@ package org.elasticsearch.index.query; import org.apache.lucene.util.BytesRef; +import org.elasticsearch.common.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; @@ -30,6 +31,8 @@ import java.util.Objects; public abstract class BaseTermQueryBuilder> extends AbstractQueryBuilder { + public static final ParseField VALUE_FIELD = new ParseField("value"); + /** Name of field to match against. */ protected final String fieldName; @@ -133,7 +136,7 @@ public abstract class BaseTermQueryBuilder> protected void doXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(getName()); builder.startObject(fieldName); - builder.field("value", convertToStringIfBytesRef(this.value)); + builder.field(VALUE_FIELD.getPreferredName(), convertToStringIfBytesRef(this.value)); printBoostAndQueryName(builder); builder.endObject(); builder.endObject(); 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 9ed1623f66a..b8170a3195a 100644 --- a/core/src/main/java/org/elasticsearch/index/query/BoolQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/BoolQueryBuilder.java @@ -231,14 +231,14 @@ public class BoolQueryBuilder extends AbstractQueryBuilder { @Override protected void doXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(NAME); - doXArrayContent("must", mustClauses, builder, params); - doXArrayContent("filter", filterClauses, builder, params); - doXArrayContent("must_not", mustNotClauses, builder, params); - doXArrayContent("should", shouldClauses, builder, params); - builder.field("disable_coord", disableCoord); - builder.field("adjust_pure_negative", adjustPureNegative); + doXArrayContent(BoolQueryParser.MUST, mustClauses, builder, params); + doXArrayContent(BoolQueryParser.FILTER, filterClauses, builder, params); + doXArrayContent(BoolQueryParser.MUST_NOT, mustNotClauses, builder, params); + doXArrayContent(BoolQueryParser.SHOULD, shouldClauses, builder, params); + builder.field(BoolQueryParser.DISABLE_COORD_FIELD.getPreferredName(), disableCoord); + builder.field(BoolQueryParser.ADJUST_PURE_NEGATIVE.getPreferredName(), adjustPureNegative); if (minimumShouldMatch != null) { - builder.field("minimum_should_match", minimumShouldMatch); + builder.field(BoolQueryParser.MINIMUM_SHOULD_MATCH.getPreferredName(), minimumShouldMatch); } printBoostAndQueryName(builder); builder.endObject(); diff --git a/core/src/main/java/org/elasticsearch/index/query/BoolQueryParser.java b/core/src/main/java/org/elasticsearch/index/query/BoolQueryParser.java index 13b5f509084..d0d130f9774 100644 --- a/core/src/main/java/org/elasticsearch/index/query/BoolQueryParser.java +++ b/core/src/main/java/org/elasticsearch/index/query/BoolQueryParser.java @@ -20,6 +20,7 @@ package org.elasticsearch.index.query; import org.apache.lucene.search.BooleanQuery; +import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; @@ -34,6 +35,16 @@ import java.util.List; */ public class BoolQueryParser implements QueryParser { + public static final String MUSTNOT = "mustNot"; + public static final String MUST_NOT = "must_not"; + public static final String FILTER = "filter"; + public static final String SHOULD = "should"; + public static final String MUST = "must"; + public static final ParseField DISABLE_COORD_FIELD = new ParseField("disable_coord"); + public static final ParseField MINIMUM_SHOULD_MATCH = new ParseField("minimum_should_match"); + public static final ParseField MINIMUM_NUMBER_SHOULD_MATCH = new ParseField("minimum_number_should_match"); + public static final ParseField ADJUST_PURE_NEGATIVE = new ParseField("adjust_pure_negative"); + @Inject public BoolQueryParser(Settings settings) { BooleanQuery.setMaxClauseCount(settings.getAsInt("index.query.bool.max_clause_count", settings.getAsInt("indices.query.bool.max_clause_count", BooleanQuery.getMaxClauseCount()))); @@ -69,20 +80,20 @@ public class BoolQueryParser implements QueryParser { // skip } else if (token == XContentParser.Token.START_OBJECT) { switch (currentFieldName) { - case "must": + case MUST: query = parseContext.parseInnerQueryBuilder(); mustClauses.add(query); break; - case "should": + case SHOULD: query = parseContext.parseInnerQueryBuilder(); shouldClauses.add(query); break; - case "filter": + case FILTER: query = parseContext.parseInnerQueryBuilder(); filterClauses.add(query); break; - case "must_not": - case "mustNot": + case MUST_NOT: + case MUSTNOT: query = parseContext.parseInnerQueryBuilder(); mustNotClauses.add(query); break; @@ -92,20 +103,20 @@ public class BoolQueryParser implements QueryParser { } else if (token == XContentParser.Token.START_ARRAY) { while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { switch (currentFieldName) { - case "must": + case MUST: query = parseContext.parseInnerQueryBuilder(); mustClauses.add(query); break; - case "should": + case SHOULD: query = parseContext.parseInnerQueryBuilder(); shouldClauses.add(query); break; - case "filter": + case FILTER: query = parseContext.parseInnerQueryBuilder(); filterClauses.add(query); break; - case "must_not": - case "mustNot": + case MUST_NOT: + case MUSTNOT: query = parseContext.parseInnerQueryBuilder(); mustNotClauses.add(query); break; @@ -114,17 +125,17 @@ public class BoolQueryParser implements QueryParser { } } } else if (token.isValue()) { - if ("disable_coord".equals(currentFieldName) || "disableCoord".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, DISABLE_COORD_FIELD)) { disableCoord = parser.booleanValue(); - } else if ("minimum_should_match".equals(currentFieldName) || "minimumShouldMatch".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, MINIMUM_SHOULD_MATCH)) { minimumShouldMatch = parser.textOrNull(); - } else if ("boost".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) { boost = parser.floatValue(); - } else if ("minimum_number_should_match".equals(currentFieldName) || "minimumNumberShouldMatch".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, MINIMUM_NUMBER_SHOULD_MATCH)) { minimumShouldMatch = parser.textOrNull(); - } else if ("adjust_pure_negative".equals(currentFieldName) || "adjustPureNegative".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, ADJUST_PURE_NEGATIVE)) { adjustPureNegative = parser.booleanValue(); - } else if ("_name".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) { 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 c1994a6033e..c7349cca3e6 100644 --- a/core/src/main/java/org/elasticsearch/index/query/BoostingQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/BoostingQueryBuilder.java @@ -104,11 +104,11 @@ public class BoostingQueryBuilder extends AbstractQueryBuilder { + public static final ParseField POSITIVE_FIELD = new ParseField("positive"); + public static final ParseField NEGATIVE_FIELD = new ParseField("negative"); + public static final ParseField NEGATIVE_BOOST_FIELD = new ParseField("negative_boost"); + @Override public String[] names() { return new String[]{BoostingQueryBuilder.NAME}; @@ -52,21 +57,21 @@ public class BoostingQueryParser implements QueryParser { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token == XContentParser.Token.START_OBJECT) { - if ("positive".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, POSITIVE_FIELD)) { positiveQuery = parseContext.parseInnerQueryBuilder(); positiveQueryFound = true; - } else if ("negative".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, NEGATIVE_FIELD)) { negativeQuery = parseContext.parseInnerQueryBuilder(); negativeQueryFound = true; } else { throw new ParsingException(parser.getTokenLocation(), "[boosting] query does not support [" + currentFieldName + "]"); } } else if (token.isValue()) { - if ("negative_boost".equals(currentFieldName) || "negativeBoost".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, NEGATIVE_BOOST_FIELD)) { negativeBoost = parser.floatValue(); - } else if ("_name".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) { queryName = parser.text(); - } else if ("boost".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) { boost = parser.floatValue(); } else { throw new ParsingException(parser.getTokenLocation(), "[boosting] query does not support [" + currentFieldName + "]"); diff --git a/core/src/main/java/org/elasticsearch/index/query/CommonTermsQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/CommonTermsQueryBuilder.java index 1767f5528f1..20d0b62b725 100644 --- a/core/src/main/java/org/elasticsearch/index/query/CommonTermsQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/CommonTermsQueryBuilder.java @@ -202,21 +202,21 @@ public class CommonTermsQueryBuilder extends AbstractQueryBuilder { + public static final ParseField CUTOFF_FREQUENCY_FIELD = new ParseField("cutoff_frequency"); + public static final ParseField MINIMUM_SHOULD_MATCH_FIELD = new ParseField("minimum_should_match"); + public static final ParseField LOW_FREQ_OPERATOR_FIELD = new ParseField("low_freq_operator"); + public static final ParseField HIGH_FREQ_OPERATOR_FIELD = new ParseField("high_freq_operator"); + public static final ParseField DISABLE_COORD_FIELD = new ParseField("disable_coord"); + public static final ParseField ANALYZER_FIELD = new ParseField("analyzer"); + public static final ParseField QUERY_FIELD = new ParseField("query"); + public static final ParseField HIGH_FREQ_FIELD = new ParseField("high_freq"); + public static final ParseField LOW_FREQ_FIELD = new ParseField("low_freq"); + @Override public String[] names() { return new String[] { CommonTermsQueryBuilder.NAME }; @@ -59,15 +70,15 @@ public class CommonTermsQueryParser implements QueryParser { - private static final ParseField INNER_QUERY_FIELD = new ParseField("filter", "query"); + public static final ParseField INNER_QUERY_FIELD = new ParseField("filter", "query"); @Override public String[] names() { @@ -62,9 +62,9 @@ public class ConstantScoreQueryParser implements QueryParser @Override protected void doXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(NAME); - builder.field("tie_breaker", tieBreaker); - builder.startArray("queries"); + builder.field(DisMaxQueryParser.TIE_BREAKER_FIELD.getPreferredName(), tieBreaker); + builder.startArray(DisMaxQueryParser.QUERIES_FIELD.getPreferredName()); for (QueryBuilder queryBuilder : queries) { queryBuilder.toXContent(builder, params); } diff --git a/core/src/main/java/org/elasticsearch/index/query/DisMaxQueryParser.java b/core/src/main/java/org/elasticsearch/index/query/DisMaxQueryParser.java index a280cdfd837..660790e21ae 100644 --- a/core/src/main/java/org/elasticsearch/index/query/DisMaxQueryParser.java +++ b/core/src/main/java/org/elasticsearch/index/query/DisMaxQueryParser.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.query; +import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.xcontent.XContentParser; @@ -32,6 +33,9 @@ import java.util.List; */ public class DisMaxQueryParser implements QueryParser { + public static final ParseField TIE_BREAKER_FIELD = new ParseField("tie_breaker"); + public static final ParseField QUERIES_FIELD = new ParseField("queries"); + @Override public String[] names() { return new String[]{DisMaxQueryBuilder.NAME, Strings.toCamelCase(DisMaxQueryBuilder.NAME)}; @@ -54,7 +58,7 @@ public class DisMaxQueryParser implements QueryParser { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token == XContentParser.Token.START_OBJECT) { - if ("queries".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, QUERIES_FIELD)) { queriesFound = true; QueryBuilder query = parseContext.parseInnerQueryBuilder(); queries.add(query); @@ -62,7 +66,7 @@ public class DisMaxQueryParser implements QueryParser { throw new ParsingException(parser.getTokenLocation(), "[dis_max] query does not support [" + currentFieldName + "]"); } } else if (token == XContentParser.Token.START_ARRAY) { - if ("queries".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, QUERIES_FIELD)) { queriesFound = true; while (token != XContentParser.Token.END_ARRAY) { QueryBuilder query = parseContext.parseInnerQueryBuilder(); @@ -73,11 +77,11 @@ public class DisMaxQueryParser implements QueryParser { throw new ParsingException(parser.getTokenLocation(), "[dis_max] query does not support [" + currentFieldName + "]"); } } else { - if ("boost".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) { boost = parser.floatValue(); - } else if ("tie_breaker".equals(currentFieldName) || "tieBreaker".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, TIE_BREAKER_FIELD)) { tieBreaker = parser.floatValue(); - } else if ("_name".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) { queryName = parser.text(); } else { throw new ParsingException(parser.getTokenLocation(), "[dis_max] query does not support [" + currentFieldName + "]"); 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 8bc8e3c7be3..02af9b7675a 100644 --- a/core/src/main/java/org/elasticsearch/index/query/ExistsQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/ExistsQueryBuilder.java @@ -61,7 +61,7 @@ public class ExistsQueryBuilder extends AbstractQueryBuilder @Override protected void doXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(NAME); - builder.field("field", fieldName); + builder.field(ExistsQueryParser.FIELD_FIELD.getPreferredName(), fieldName); printBoostAndQueryName(builder); builder.endObject(); } diff --git a/core/src/main/java/org/elasticsearch/index/query/ExistsQueryParser.java b/core/src/main/java/org/elasticsearch/index/query/ExistsQueryParser.java index 35f96360aea..86a5311ff40 100644 --- a/core/src/main/java/org/elasticsearch/index/query/ExistsQueryParser.java +++ b/core/src/main/java/org/elasticsearch/index/query/ExistsQueryParser.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.query; +import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.xcontent.XContentParser; @@ -29,6 +30,8 @@ import java.io.IOException; */ public class ExistsQueryParser implements QueryParser { + public static final ParseField FIELD_FIELD = new ParseField("field"); + @Override public String[] names() { return new String[]{ExistsQueryBuilder.NAME}; @@ -48,11 +51,11 @@ public class ExistsQueryParser implements QueryParser { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token.isValue()) { - if ("field".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, FIELD_FIELD)) { fieldPattern = parser.text(); - } else if ("_name".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) { queryName = parser.text(); - } else if ("boost".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) { boost = parser.floatValue(); } else { throw new ParsingException(parser.getTokenLocation(), "[" + ExistsQueryBuilder.NAME + "] query does not support [" + currentFieldName + "]"); 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 671cfda3e7a..e9258d7cfc1 100644 --- a/core/src/main/java/org/elasticsearch/index/query/FieldMaskingSpanQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/FieldMaskingSpanQueryBuilder.java @@ -75,9 +75,9 @@ public class FieldMaskingSpanQueryBuilder extends AbstractQueryBuilder { + public static final ParseField FIELD_FIELD = new ParseField("field"); + public static final ParseField QUERY_FIELD = new ParseField("query"); + @Override public String[] names() { return new String[]{FieldMaskingSpanQueryBuilder.NAME, Strings.toCamelCase(FieldMaskingSpanQueryBuilder.NAME)}; @@ -50,7 +54,7 @@ public class FieldMaskingSpanQueryParser implements QueryParser i } @Override - public void doXContent(XContentBuilder builder, Params params) throws IOException { + protected void doXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(NAME); builder.startObject(fieldName); - builder.field("value", convertToStringIfBytesRef(this.value)); + builder.field(FuzzyQueryParser.VALUE_FIELD.getPreferredName(), convertToStringIfBytesRef(this.value)); fuzziness.toXContent(builder, params); - builder.field("prefix_length", prefixLength); - builder.field("max_expansions", maxExpansions); - builder.field("transpositions", transpositions); + builder.field(FuzzyQueryParser.PREFIX_LENGTH_FIELD.getPreferredName(), prefixLength); + builder.field(FuzzyQueryParser.MAX_EXPANSIONS_FIELD.getPreferredName(), maxExpansions); + builder.field(FuzzyQueryParser.TRANSPOSITIONS_FIELD.getPreferredName(), transpositions); if (rewrite != null) { - builder.field("rewrite", rewrite); + builder.field(FuzzyQueryParser.REWRITE_FIELD.getPreferredName(), rewrite); } printBoostAndQueryName(builder); builder.endObject(); @@ -231,7 +231,7 @@ public class FuzzyQueryBuilder extends AbstractQueryBuilder i } @Override - public Query doToQuery(QueryShardContext context) throws IOException { + protected Query doToQuery(QueryShardContext context) throws IOException { Query query = null; String rewrite = this.rewrite; if (rewrite == null && context.isFilter()) { @@ -253,7 +253,7 @@ public class FuzzyQueryBuilder extends AbstractQueryBuilder i } @Override - public FuzzyQueryBuilder doReadFrom(StreamInput in) throws IOException { + protected FuzzyQueryBuilder doReadFrom(StreamInput in) throws IOException { FuzzyQueryBuilder fuzzyQueryBuilder = new FuzzyQueryBuilder(in.readString(), in.readGenericValue()); fuzzyQueryBuilder.fuzziness = Fuzziness.readFuzzinessFrom(in); fuzzyQueryBuilder.prefixLength = in.readVInt(); @@ -264,7 +264,7 @@ public class FuzzyQueryBuilder extends AbstractQueryBuilder i } @Override - public void doWriteTo(StreamOutput out) throws IOException { + protected void doWriteTo(StreamOutput out) throws IOException { out.writeString(this.fieldName); out.writeGenericValue(this.value); this.fuzziness.writeTo(out); @@ -275,12 +275,12 @@ public class FuzzyQueryBuilder extends AbstractQueryBuilder i } @Override - public int doHashCode() { + protected int doHashCode() { return Objects.hash(fieldName, value, fuzziness, prefixLength, maxExpansions, transpositions, rewrite); } @Override - public boolean doEquals(FuzzyQueryBuilder other) { + protected boolean doEquals(FuzzyQueryBuilder other) { return Objects.equals(fieldName, other.fieldName) && Objects.equals(value, other.value) && Objects.equals(fuzziness, other.fuzziness) && diff --git a/core/src/main/java/org/elasticsearch/index/query/FuzzyQueryParser.java b/core/src/main/java/org/elasticsearch/index/query/FuzzyQueryParser.java index d0094344b0b..85365f84fb9 100644 --- a/core/src/main/java/org/elasticsearch/index/query/FuzzyQueryParser.java +++ b/core/src/main/java/org/elasticsearch/index/query/FuzzyQueryParser.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.query; +import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.common.xcontent.XContentParser; @@ -27,6 +28,13 @@ import java.io.IOException; public class FuzzyQueryParser implements QueryParser { + public static final ParseField TERM_FIELD = new ParseField("term"); + public static final ParseField VALUE_FIELD = new ParseField("value"); + public static final ParseField PREFIX_LENGTH_FIELD = new ParseField("prefix_length"); + public static final ParseField MAX_EXPANSIONS_FIELD = new ParseField("max_expansions"); + public static final ParseField TRANSPOSITIONS_FIELD = new ParseField("transpositions"); + public static final ParseField REWRITE_FIELD = new ParseField("rewrite"); + @Override public String[] names() { return new String[]{ FuzzyQueryBuilder.NAME }; @@ -60,23 +68,23 @@ public class FuzzyQueryParser implements QueryParser { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else { - if ("term".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, TERM_FIELD)) { value = parser.objectBytes(); - } else if ("value".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, VALUE_FIELD)) { value = parser.objectBytes(); - } else if ("boost".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) { boost = parser.floatValue(); } else if (parseContext.parseFieldMatcher().match(currentFieldName, Fuzziness.FIELD)) { fuzziness = Fuzziness.parse(parser); - } else if ("prefix_length".equals(currentFieldName) || "prefixLength".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, PREFIX_LENGTH_FIELD)) { prefixLength = parser.intValue(); - } else if ("max_expansions".equals(currentFieldName) || "maxExpansions".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, MAX_EXPANSIONS_FIELD)) { maxExpansions = parser.intValue(); - } else if ("transpositions".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, TRANSPOSITIONS_FIELD)) { transpositions = parser.booleanValue(); - } else if ("rewrite".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, REWRITE_FIELD)) { rewrite = parser.textOrNull(); - } else if ("_name".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) { queryName = parser.text(); } else { throw new ParsingException(parser.getTokenLocation(), "[fuzzy] query does not support [" + 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 13ce524a2dd..dec14f59bf8 100644 --- a/core/src/main/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilder.java @@ -292,11 +292,11 @@ public class GeoBoundingBoxQueryBuilder extends AbstractQueryBuilder { + public static final ParseField VALIDATION_METHOD_FIELD = new ParseField("validation_method"); + public static final ParseField IGNORE_MALFORMED_FIELD = new ParseField("ignore_malformed"); + public static final ParseField COERCE_FIELD = new ParseField("coerce", "normalize"); + public static final ParseField OPTIMIZE_BBOX_FIELD = new ParseField("optimize_bbox"); + public static final ParseField DISTANCE_TYPE_FIELD = new ParseField("distance_type"); + public static final ParseField UNIT_FIELD = new ParseField("unit"); + public static final ParseField DISTANCE_FIELD = new ParseField("distance"); + @Override public String[] names() { return new String[]{GeoDistanceQueryBuilder.NAME, "geoDistance"}; @@ -95,15 +104,15 @@ public class GeoDistanceQueryParser implements QueryParser { - private static final ParseField QUERY_FIELD = new ParseField("query", "filter"); + public static final ParseField QUERY_FIELD = new ParseField("query", "filter"); + public static final ParseField TYPE_FIELD = new ParseField("type", "child_type"); + public static final ParseField MAX_CHILDREN_FIELD = new ParseField("max_children"); + public static final ParseField MIN_CHILDREN_FIELD = new ParseField("min_children"); + public static final ParseField SCORE_MODE_FIELD = new ParseField("score_mode"); + public static final ParseField INNER_HITS_FIELD = new ParseField("inner_hits"); @Override public String[] names() { @@ -61,23 +66,23 @@ public class HasChildQueryParser implements QueryParser { } else if (token == XContentParser.Token.START_OBJECT) { if (parseContext.parseFieldMatcher().match(currentFieldName, QUERY_FIELD)) { iqb = parseContext.parseInnerQueryBuilder(); - } else if ("inner_hits".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, INNER_HITS_FIELD)) { queryInnerHits = new QueryInnerHits(parser); } else { throw new ParsingException(parser.getTokenLocation(), "[has_child] query does not support [" + currentFieldName + "]"); } } else if (token.isValue()) { - if ("type".equals(currentFieldName) || "child_type".equals(currentFieldName) || "childType".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, TYPE_FIELD)) { childType = parser.text(); - } else if ("score_mode".equals(currentFieldName) || "scoreMode".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, SCORE_MODE_FIELD)) { scoreMode = parseScoreMode(parser.text()); - } else if ("boost".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) { boost = parser.floatValue(); - } else if ("min_children".equals(currentFieldName) || "minChildren".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, MIN_CHILDREN_FIELD)) { minChildren = parser.intValue(true); - } else if ("max_children".equals(currentFieldName) || "maxChildren".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, MAX_CHILDREN_FIELD)) { maxChildren = parser.intValue(true); - } else if ("_name".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) { queryName = parser.text(); } else { throw new ParsingException(parser.getTokenLocation(), "[has_child] query does not support [" + currentFieldName + "]"); diff --git a/core/src/main/java/org/elasticsearch/index/query/HasParentQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/HasParentQueryBuilder.java index 5ac64163dd9..0ad734ec552 100644 --- a/core/src/main/java/org/elasticsearch/index/query/HasParentQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/HasParentQueryBuilder.java @@ -199,10 +199,10 @@ public class HasParentQueryBuilder extends AbstractQueryBuilder { private static final HasParentQueryBuilder PROTOTYPE = new HasParentQueryBuilder("", EmptyQueryBuilder.PROTOTYPE); - private static final ParseField QUERY_FIELD = new ParseField("query", "filter"); - private static final ParseField SCORE_FIELD = new ParseField("score_mode").withAllDeprecated("score"); - private static final ParseField TYPE_FIELD = new ParseField("parent_type", "type"); + public static final ParseField QUERY_FIELD = new ParseField("query", "filter"); + //public static final ParseField SCORE_MODE_FIELD = new ParseField("score_mode").withAllDeprecated("score"); + public static final ParseField SCORE_MODE_FIELD = new ParseField("score_mode").withAllDeprecated("score"); + public static final ParseField TYPE_FIELD = new ParseField("parent_type", "type"); + public static final ParseField SCORE_FIELD = new ParseField("score"); @Override public String[] names() { @@ -42,7 +44,6 @@ public class HasParentQueryParser implements QueryParser @Override public HasParentQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException { XContentParser parser = parseContext.parser(); - float boost = AbstractQueryBuilder.DEFAULT_BOOST; String parentType = null; boolean score = HasParentQueryBuilder.DEFAULT_SCORE; @@ -66,7 +67,7 @@ public class HasParentQueryParser implements QueryParser } else if (token.isValue()) { if (parseContext.parseFieldMatcher().match(currentFieldName, TYPE_FIELD)) { parentType = parser.text(); - } else if (parseContext.parseFieldMatcher().match(currentFieldName, SCORE_FIELD)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, SCORE_MODE_FIELD)) { String scoreModeValue = parser.text(); if ("score".equals(scoreModeValue)) { score = true; @@ -75,11 +76,11 @@ public class HasParentQueryParser implements QueryParser } else { throw new ParsingException(parser.getTokenLocation(), "[has_parent] query does not support [" + scoreModeValue + "] as an option for score_mode"); } - } else if ("score".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, SCORE_FIELD)) { score = parser.booleanValue(); - } else if ("boost".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) { boost = parser.floatValue(); - } else if ("_name".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) { queryName = parser.text(); } else { throw new ParsingException(parser.getTokenLocation(), "[has_parent] query does not support [" + currentFieldName + "]"); diff --git a/core/src/main/java/org/elasticsearch/index/query/IdsQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/IdsQueryBuilder.java index 6987fe1d1da..4f9574f2981 100644 --- a/core/src/main/java/org/elasticsearch/index/query/IdsQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/IdsQueryBuilder.java @@ -90,8 +90,8 @@ public class IdsQueryBuilder extends AbstractQueryBuilder { @Override protected void doXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(NAME); - builder.array("types", types); - builder.startArray("values"); + builder.array(IdsQueryParser.TYPE_FIELD.getPreferredName(), types); + builder.startArray(IdsQueryParser.VALUES_FIELD.getPreferredName()); for (String value : ids) { builder.value(value); } diff --git a/core/src/main/java/org/elasticsearch/index/query/IdsQueryParser.java b/core/src/main/java/org/elasticsearch/index/query/IdsQueryParser.java index 0496a690f5f..46058d98eb5 100644 --- a/core/src/main/java/org/elasticsearch/index/query/IdsQueryParser.java +++ b/core/src/main/java/org/elasticsearch/index/query/IdsQueryParser.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.query; +import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.xcontent.XContentParser; @@ -32,6 +33,10 @@ import java.util.List; */ public class IdsQueryParser implements QueryParser { + public static final ParseField TYPE_FIELD = new ParseField("type", "types", "_type"); + + public static final ParseField VALUES_FIELD = new ParseField("values"); + @Override public String[] names() { return new String[]{IdsQueryBuilder.NAME}; @@ -55,7 +60,7 @@ public class IdsQueryParser implements QueryParser { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token == XContentParser.Token.START_ARRAY) { - if ("values".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, VALUES_FIELD)) { idsProvided = true; while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { if ((token == XContentParser.Token.VALUE_STRING) || @@ -70,7 +75,7 @@ public class IdsQueryParser implements QueryParser { + token); } } - } else if ("types".equals(currentFieldName) || "type".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, TYPE_FIELD)) { while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { String value = parser.textOrNull(); if (value == null) { @@ -82,11 +87,11 @@ public class IdsQueryParser implements QueryParser { throw new ParsingException(parser.getTokenLocation(), "[" + IdsQueryBuilder.NAME + "] query does not support [" + currentFieldName + "]"); } } else if (token.isValue()) { - if ("type".equals(currentFieldName) || "_type".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, TYPE_FIELD)) { types = Collections.singletonList(parser.text()); - } else if ("boost".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) { boost = parser.floatValue(); - } else if ("_name".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) { queryName = parser.text(); } else { throw new ParsingException(parser.getTokenLocation(), "[" + IdsQueryBuilder.NAME + "] query does not support [" + currentFieldName + "]"); diff --git a/core/src/main/java/org/elasticsearch/index/query/IndicesQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/IndicesQueryBuilder.java index 1445823ac47..5185dfda3b0 100644 --- a/core/src/main/java/org/elasticsearch/index/query/IndicesQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/IndicesQueryBuilder.java @@ -93,10 +93,10 @@ public class IndicesQueryBuilder extends AbstractQueryBuilder { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token.isValue()) { - if ("_name".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) { queryName = parser.text(); - } else if ("boost".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) { boost = parser.floatValue(); } else { throw new ParsingException(parser.getTokenLocation(), "[" + MatchAllQueryBuilder.NAME + "] query does not support [" + currentFieldName + "]"); diff --git a/core/src/main/java/org/elasticsearch/index/query/MatchNoneQueryParser.java b/core/src/main/java/org/elasticsearch/index/query/MatchNoneQueryParser.java index 449824e72c9..70fc74d66e5 100644 --- a/core/src/main/java/org/elasticsearch/index/query/MatchNoneQueryParser.java +++ b/core/src/main/java/org/elasticsearch/index/query/MatchNoneQueryParser.java @@ -44,9 +44,9 @@ public class MatchNoneQueryParser implements QueryParser if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token.isValue()) { - if ("_name".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) { queryName = parser.text(); - } else if ("boost".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) { boost = parser.floatValue(); } else { throw new ParsingException(parser.getTokenLocation(), "["+MatchNoneQueryBuilder.NAME+"] query does not support [" + currentFieldName + "]"); diff --git a/core/src/main/java/org/elasticsearch/index/query/MatchQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/MatchQueryBuilder.java index e66c1cf3dfd..d9a99cc50cb 100644 --- a/core/src/main/java/org/elasticsearch/index/query/MatchQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/MatchQueryBuilder.java @@ -129,6 +129,11 @@ public class MatchQueryBuilder extends AbstractQueryBuilder { return this; } + /** Returns the operator to use in a boolean query.*/ + public Operator operator() { + return this.operator; + } + /** * Explicitly set the analyzer to use. Defaults to use explicit mapping config for the field, or, if not * set, the default search analyzer. @@ -312,30 +317,30 @@ public class MatchQueryBuilder extends AbstractQueryBuilder { builder.startObject(NAME); builder.startObject(fieldName); - builder.field("query", value); - builder.field("type", type.toString().toLowerCase(Locale.ENGLISH)); - builder.field("operator", operator.toString()); + builder.field(MatchQueryParser.QUERY_FIELD.getPreferredName(), value); + builder.field(MatchQueryParser.TYPE_FIELD.getPreferredName(), type.toString().toLowerCase(Locale.ENGLISH)); + builder.field(MatchQueryParser.OPERATOR_FIELD.getPreferredName(), operator.toString()); if (analyzer != null) { - builder.field("analyzer", analyzer); + builder.field(MatchQueryParser.ANALYZER_FIELD.getPreferredName(), analyzer); } - builder.field("slop", slop); + builder.field(MatchQueryParser.SLOP_FIELD.getPreferredName(), slop); if (fuzziness != null) { fuzziness.toXContent(builder, params); } - builder.field("prefix_length", prefixLength); - builder.field("max_expansions", maxExpansions); + builder.field(MatchQueryParser.PREFIX_LENGTH_FIELD.getPreferredName(), prefixLength); + builder.field(MatchQueryParser.MAX_EXPANSIONS_FIELD.getPreferredName(), maxExpansions); if (minimumShouldMatch != null) { - builder.field("minimum_should_match", minimumShouldMatch); + builder.field(MatchQueryParser.MINIMUM_SHOULD_MATCH_FIELD.getPreferredName(), minimumShouldMatch); } if (fuzzyRewrite != null) { - builder.field("fuzzy_rewrite", fuzzyRewrite); + builder.field(MatchQueryParser.FUZZY_REWRITE_FIELD.getPreferredName(), fuzzyRewrite); } // LUCENE 4 UPGRADE we need to document this & test this - builder.field("fuzzy_transpositions", fuzzyTranspositions); - builder.field("lenient", lenient); - builder.field("zero_terms_query", zeroTermsQuery.toString()); + builder.field(MatchQueryParser.FUZZY_TRANSPOSITIONS_FIELD.getPreferredName(), fuzzyTranspositions); + builder.field(MatchQueryParser.LENIENT_FIELD.getPreferredName(), lenient); + builder.field(MatchQueryParser.ZERO_TERMS_QUERY_FIELD.getPreferredName(), zeroTermsQuery.toString()); if (cutoffFrequency != null) { - builder.field("cutoff_frequency", cutoffFrequency); + builder.field(MatchQueryParser.CUTOFF_FREQUENCY_FIELD.getPreferredName(), cutoffFrequency); } printBoostAndQueryName(builder); builder.endObject(); diff --git a/core/src/main/java/org/elasticsearch/index/query/MatchQueryParser.java b/core/src/main/java/org/elasticsearch/index/query/MatchQueryParser.java index c50256480a0..4b149dd6be3 100644 --- a/core/src/main/java/org/elasticsearch/index/query/MatchQueryParser.java +++ b/core/src/main/java/org/elasticsearch/index/query/MatchQueryParser.java @@ -20,6 +20,7 @@ package org.elasticsearch.index.query; import org.apache.lucene.search.FuzzyQuery; +import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.common.xcontent.XContentParser; @@ -33,6 +34,22 @@ import java.io.IOException; */ public class MatchQueryParser implements QueryParser { + public static final ParseField MATCH_PHRASE_FIELD = new ParseField("match_phrase", "text_phrase"); + public static final ParseField MATCH_PHRASE_PREFIX_FIELD = new ParseField("match_phrase_prefix", "text_phrase_prefix"); + public static final ParseField SLOP_FIELD = new ParseField("slop", "phrase_slop"); + public static final ParseField ZERO_TERMS_QUERY_FIELD = new ParseField("zero_terms_query"); + public static final ParseField CUTOFF_FREQUENCY_FIELD = new ParseField("cutoff_frequency"); + public static final ParseField LENIENT_FIELD = new ParseField("lenient"); + public static final ParseField FUZZY_TRANSPOSITIONS_FIELD = new ParseField("fuzzy_transpositions"); + public static final ParseField FUZZY_REWRITE_FIELD = new ParseField("fuzzy_rewrite"); + public static final ParseField MINIMUM_SHOULD_MATCH_FIELD = new ParseField("minimum_should_match"); + public static final ParseField OPERATOR_FIELD = new ParseField("operator"); + public static final ParseField MAX_EXPANSIONS_FIELD = new ParseField("max_expansions"); + public static final ParseField PREFIX_LENGTH_FIELD = new ParseField("prefix_length"); + public static final ParseField ANALYZER_FIELD = new ParseField("analyzer"); + public static final ParseField TYPE_FIELD = new ParseField("type"); + public static final ParseField QUERY_FIELD = new ParseField("query"); + @Override public String[] names() { return new String[]{ @@ -45,11 +62,9 @@ public class MatchQueryParser implements QueryParser { XContentParser parser = parseContext.parser(); MatchQuery.Type type = MatchQuery.Type.BOOLEAN; - if ("match_phrase".equals(parser.currentName()) || "matchPhrase".equals(parser.currentName()) || - "text_phrase".equals(parser.currentName()) || "textPhrase".equals(parser.currentName())) { + if (parseContext.parseFieldMatcher().match(parser.currentName(), MATCH_PHRASE_FIELD)) { type = MatchQuery.Type.PHRASE; - } else if ("match_phrase_prefix".equals(parser.currentName()) || "matchPhrasePrefix".equals(parser.currentName()) || - "text_phrase_prefix".equals(parser.currentName()) || "textPhrasePrefix".equals(parser.currentName())) { + } else if (parseContext.parseFieldMatcher().match(parser.currentName(), MATCH_PHRASE_PREFIX_FIELD)) { type = MatchQuery.Type.PHRASE_PREFIX; } @@ -82,44 +97,44 @@ public class MatchQueryParser implements QueryParser { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token.isValue()) { - if ("query".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, QUERY_FIELD)) { value = parser.objectText(); - } else if ("type".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, TYPE_FIELD)) { String tStr = parser.text(); if ("boolean".equals(tStr)) { type = MatchQuery.Type.BOOLEAN; } else if ("phrase".equals(tStr)) { type = MatchQuery.Type.PHRASE; - } else if ("phrase_prefix".equals(tStr) || "phrasePrefix".equals(currentFieldName)) { + } else if ("phrase_prefix".equals(tStr) || ("phrasePrefix".equals(tStr))) { type = MatchQuery.Type.PHRASE_PREFIX; } else { throw new ParsingException(parser.getTokenLocation(), "[" + MatchQueryBuilder.NAME + "] query does not support type " + tStr); } - } else if ("analyzer".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, ANALYZER_FIELD)) { analyzer = parser.text(); - } else if ("boost".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) { boost = parser.floatValue(); - } else if ("slop".equals(currentFieldName) || "phrase_slop".equals(currentFieldName) || "phraseSlop".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, SLOP_FIELD)) { slop = parser.intValue(); } else if (parseContext.parseFieldMatcher().match(currentFieldName, Fuzziness.FIELD)) { fuzziness = Fuzziness.parse(parser); - } else if ("prefix_length".equals(currentFieldName) || "prefixLength".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, PREFIX_LENGTH_FIELD)) { prefixLength = parser.intValue(); - } else if ("max_expansions".equals(currentFieldName) || "maxExpansions".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, MAX_EXPANSIONS_FIELD)) { maxExpansion = parser.intValue(); - } else if ("operator".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, OPERATOR_FIELD)) { operator = Operator.fromString(parser.text()); - } else if ("minimum_should_match".equals(currentFieldName) || "minimumShouldMatch".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, MINIMUM_SHOULD_MATCH_FIELD)) { minimumShouldMatch = parser.textOrNull(); - } else if ("fuzzy_rewrite".equals(currentFieldName) || "fuzzyRewrite".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, FUZZY_REWRITE_FIELD)) { fuzzyRewrite = parser.textOrNull(); - } else if ("fuzzy_transpositions".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, FUZZY_TRANSPOSITIONS_FIELD)) { fuzzyTranspositions = parser.booleanValue(); - } else if ("lenient".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, LENIENT_FIELD)) { lenient = parser.booleanValue(); - } else if ("cutoff_frequency".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, CUTOFF_FREQUENCY_FIELD)) { cutOffFrequency = parser.floatValue(); - } else if ("zero_terms_query".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, ZERO_TERMS_QUERY_FIELD)) { String zeroTermsDocs = parser.text(); if ("none".equalsIgnoreCase(zeroTermsDocs)) { zeroTermsQuery = MatchQuery.ZeroTermsQuery.NONE; @@ -128,7 +143,7 @@ public class MatchQueryParser implements QueryParser { } else { throw new ParsingException(parser.getTokenLocation(), "Unsupported zero_terms_docs value [" + zeroTermsDocs + "]"); } - } else if ("_name".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) { queryName = parser.text(); } else { throw new ParsingException(parser.getTokenLocation(), "[" + MatchQueryBuilder.NAME + "] query does not support [" + currentFieldName + "]"); diff --git a/core/src/main/java/org/elasticsearch/index/query/MissingQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/MissingQueryBuilder.java index ad355f98598..70d0bb9350f 100644 --- a/core/src/main/java/org/elasticsearch/index/query/MissingQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/MissingQueryBuilder.java @@ -104,9 +104,9 @@ public class MissingQueryBuilder extends AbstractQueryBuilder { + public static final ParseField FIELD_FIELD = new ParseField("field"); + public static final ParseField NULL_VALUE_FIELD = new ParseField("null_value"); + public static final ParseField EXISTENCE_FIELD = new ParseField("existence"); + @Override public String[] names() { return new String[]{MissingQueryBuilder.NAME}; @@ -50,15 +55,15 @@ public class MissingQueryParser implements QueryParser { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token.isValue()) { - if ("field".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, FIELD_FIELD)) { fieldPattern = parser.text(); - } else if ("null_value".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, NULL_VALUE_FIELD)) { nullValue = parser.booleanValue(); - } else if ("existence".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, EXISTENCE_FIELD)) { existence = parser.booleanValue(); - } else if ("_name".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) { queryName = parser.text(); - } else if ("boost".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) { boost = parser.floatValue(); } else { throw new ParsingException(parser.getTokenLocation(), "[" + MissingQueryBuilder.NAME + "] query does not support [" + currentFieldName + "]"); 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 be05333a8eb..de5d4172630 100644 --- a/core/src/main/java/org/elasticsearch/index/query/MultiMatchQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/MultiMatchQueryBuilder.java @@ -457,40 +457,40 @@ public class MultiMatchQueryBuilder extends AbstractQueryBuilder fieldEntry : this.fieldsBoosts.entrySet()) { builder.value(fieldEntry.getKey() + "^" + fieldEntry.getValue()); } builder.endArray(); - builder.field("type", type.toString().toLowerCase(Locale.ENGLISH)); - builder.field("operator", operator.toString()); + builder.field(MultiMatchQueryParser.TYPE_FIELD.getPreferredName(), type.toString().toLowerCase(Locale.ENGLISH)); + builder.field(MultiMatchQueryParser.OPERATOR_FIELD.getPreferredName(), operator.toString()); if (analyzer != null) { - builder.field("analyzer", analyzer); + builder.field(MultiMatchQueryParser.ANALYZER_FIELD.getPreferredName(), analyzer); } - builder.field("slop", slop); + builder.field(MultiMatchQueryParser.SLOP_FIELD.getPreferredName(), slop); if (fuzziness != null) { fuzziness.toXContent(builder, params); } - builder.field("prefix_length", prefixLength); - builder.field("max_expansions", maxExpansions); + builder.field(MultiMatchQueryParser.PREFIX_LENGTH_FIELD.getPreferredName(), prefixLength); + builder.field(MultiMatchQueryParser.MAX_EXPANSIONS_FIELD.getPreferredName(), maxExpansions); if (minimumShouldMatch != null) { - builder.field("minimum_should_match", minimumShouldMatch); + builder.field(MultiMatchQueryParser.MINIMUM_SHOULD_MATCH_FIELD.getPreferredName(), minimumShouldMatch); } if (fuzzyRewrite != null) { - builder.field("fuzzy_rewrite", fuzzyRewrite); + builder.field(MultiMatchQueryParser.FUZZY_REWRITE_FIELD.getPreferredName(), fuzzyRewrite); } if (useDisMax != null) { - builder.field("use_dis_max", useDisMax); + builder.field(MultiMatchQueryParser.USE_DIS_MAX_FIELD.getPreferredName(), useDisMax); } if (tieBreaker != null) { - builder.field("tie_breaker", tieBreaker); + builder.field(MultiMatchQueryParser.TIE_BREAKER_FIELD.getPreferredName(), tieBreaker); } - builder.field("lenient", lenient); + builder.field(MultiMatchQueryParser.LENIENT_FIELD.getPreferredName(), lenient); if (cutoffFrequency != null) { - builder.field("cutoff_frequency", cutoffFrequency); + builder.field(MultiMatchQueryParser.CUTOFF_FREQUENCY_FIELD.getPreferredName(), cutoffFrequency); } - builder.field("zero_terms_query", zeroTermsQuery.toString()); + builder.field(MultiMatchQueryParser.ZERO_TERMS_QUERY_FIELD.getPreferredName(), zeroTermsQuery.toString()); printBoostAndQueryName(builder); builder.endObject(); } diff --git a/core/src/main/java/org/elasticsearch/index/query/MultiMatchQueryParser.java b/core/src/main/java/org/elasticsearch/index/query/MultiMatchQueryParser.java index d52f6707aa1..af212e7aedf 100644 --- a/core/src/main/java/org/elasticsearch/index/query/MultiMatchQueryParser.java +++ b/core/src/main/java/org/elasticsearch/index/query/MultiMatchQueryParser.java @@ -19,9 +19,11 @@ package org.elasticsearch.index.query; +import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.index.query.MoreLikeThisQueryParser.Field; import org.elasticsearch.index.search.MatchQuery; import java.io.IOException; @@ -33,6 +35,22 @@ import java.util.Map; */ public class MultiMatchQueryParser implements QueryParser { + public static final ParseField SLOP_FIELD = new ParseField("slop", "phrase_slop"); + public static final ParseField ZERO_TERMS_QUERY_FIELD = new ParseField("zero_terms_query"); + public static final ParseField LENIENT_FIELD = new ParseField("lenient"); + public static final ParseField CUTOFF_FREQUENCY_FIELD = new ParseField("cutoff_frequency"); + public static final ParseField TIE_BREAKER_FIELD = new ParseField("tie_breaker"); + public static final ParseField USE_DIS_MAX_FIELD = new ParseField("use_dis_max"); + public static final ParseField FUZZY_REWRITE_FIELD = new ParseField("fuzzy_rewrite"); + public static final ParseField MINIMUM_SHOULD_MATCH_FIELD = new ParseField("minimum_should_match"); + public static final ParseField OPERATOR_FIELD = new ParseField("operator"); + public static final ParseField MAX_EXPANSIONS_FIELD = new ParseField("max_expansions"); + public static final ParseField PREFIX_LENGTH_FIELD = new ParseField("prefix_length"); + public static final ParseField ANALYZER_FIELD = new ParseField("analyzer"); + public static final ParseField TYPE_FIELD = new ParseField("type"); + public static final ParseField QUERY_FIELD = new ParseField("query"); + public static final ParseField FIELDS_FIELD = new ParseField("fields"); + @Override public String[] names() { return new String[]{ @@ -69,7 +87,7 @@ public class MultiMatchQueryParser implements QueryParser @Override protected void doXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(NAME); - builder.field("query"); + builder.field(NestedQueryParser.QUERY_FIELD.getPreferredName()); query.toXContent(builder, params); - builder.field("path", path); + builder.field(NestedQueryParser.PATH_FIELD.getPreferredName(), path); if (scoreMode != null) { - builder.field("score_mode", scoreMode.name().toLowerCase(Locale.ROOT)); + builder.field(NestedQueryParser.SCORE_MODE_FIELD.getPreferredName(), scoreMode.name().toLowerCase(Locale.ROOT)); } printBoostAndQueryName(builder); if (queryInnerHits != null) { diff --git a/core/src/main/java/org/elasticsearch/index/query/NestedQueryParser.java b/core/src/main/java/org/elasticsearch/index/query/NestedQueryParser.java index 044a49d23d7..7cdb66bd126 100644 --- a/core/src/main/java/org/elasticsearch/index/query/NestedQueryParser.java +++ b/core/src/main/java/org/elasticsearch/index/query/NestedQueryParser.java @@ -20,6 +20,7 @@ package org.elasticsearch.index.query; import org.apache.lucene.search.join.ScoreMode; +import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.xcontent.XContentParser; @@ -30,6 +31,10 @@ import java.io.IOException; public class NestedQueryParser implements QueryParser { private static final NestedQueryBuilder PROTOTYPE = new NestedQueryBuilder("", EmptyQueryBuilder.PROTOTYPE); + public static final ParseField SCORE_MODE_FIELD = new ParseField("score_mode"); + public static final ParseField PATH_FIELD = new ParseField("path"); + public static final ParseField QUERY_FIELD = new ParseField("query"); + public static final ParseField INNER_HITS_FIELD = new ParseField("inner_hits"); @Override public String[] names() { @@ -51,19 +56,19 @@ public class NestedQueryParser implements QueryParser { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token == XContentParser.Token.START_OBJECT) { - if ("query".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, QUERY_FIELD)) { query = parseContext.parseInnerQueryBuilder(); - } else if ("inner_hits".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, INNER_HITS_FIELD)) { queryInnerHits = new QueryInnerHits(parser); } else { throw new ParsingException(parser.getTokenLocation(), "[nested] query does not support [" + currentFieldName + "]"); } } else if (token.isValue()) { - if ("path".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, PATH_FIELD)) { path = parser.text(); - } else if ("boost".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) { boost = parser.floatValue(); - } else if ("score_mode".equals(currentFieldName) || "scoreMode".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, SCORE_MODE_FIELD)) { String sScoreMode = parser.text(); if ("avg".equals(sScoreMode)) { scoreMode = ScoreMode.Avg; @@ -78,7 +83,7 @@ public class NestedQueryParser implements QueryParser { } else { throw new ParsingException(parser.getTokenLocation(), "illegal score_mode for nested query [" + sScoreMode + "]"); } - } else if ("_name".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) { queryName = parser.text(); } 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 f5ca1360268..fbd1bbd05a6 100644 --- a/core/src/main/java/org/elasticsearch/index/query/PrefixQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/PrefixQueryBuilder.java @@ -87,9 +87,9 @@ public class PrefixQueryBuilder extends AbstractQueryBuilder public void doXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(NAME); builder.startObject(fieldName); - builder.field("prefix", this.value); + builder.field(PrefixQueryParser.PREFIX_FIELD.getPreferredName(), this.value); if (rewrite != null) { - builder.field("rewrite", rewrite); + builder.field(PrefixQueryParser.REWRITE_FIELD.getPreferredName(), rewrite); } printBoostAndQueryName(builder); builder.endObject(); diff --git a/core/src/main/java/org/elasticsearch/index/query/PrefixQueryParser.java b/core/src/main/java/org/elasticsearch/index/query/PrefixQueryParser.java index a8dca4c7816..e13d937d847 100644 --- a/core/src/main/java/org/elasticsearch/index/query/PrefixQueryParser.java +++ b/core/src/main/java/org/elasticsearch/index/query/PrefixQueryParser.java @@ -30,7 +30,8 @@ import java.io.IOException; */ public class PrefixQueryParser implements QueryParser { - private static final ParseField NAME_FIELD = new ParseField("_name").withAllDeprecated("query name is not supported in short version of prefix query"); + public static final ParseField PREFIX_FIELD = new ParseField("value", "prefix"); + public static final ParseField REWRITE_FIELD = new ParseField("rewrite"); @Override public String[] names() { @@ -60,13 +61,13 @@ public class PrefixQueryParser implements QueryParser { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else { - if ("_name".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) { queryName = parser.text(); - } else if ("value".equals(currentFieldName) || "prefix".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, PREFIX_FIELD)) { value = parser.textOrNull(); - } else if ("boost".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) { boost = parser.floatValue(); - } else if ("rewrite".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, REWRITE_FIELD)) { rewrite = parser.textOrNull(); } else { throw new ParsingException(parser.getTokenLocation(), "[regexp] query does not support [" + currentFieldName + "]"); @@ -74,12 +75,8 @@ public class PrefixQueryParser implements QueryParser { } } } else { - if (parseContext.parseFieldMatcher().match(currentFieldName, NAME_FIELD)) { - queryName = parser.text(); - } else { fieldName = currentFieldName; value = parser.textOrNull(); - } } } 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 393ff1e59a7..16107d4ec97 100644 --- a/core/src/main/java/org/elasticsearch/index/query/QueryStringQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/QueryStringQueryBuilder.java @@ -468,58 +468,58 @@ public class QueryStringQueryBuilder extends AbstractQueryBuilder fieldEntry : this.fieldsAndWeights.entrySet()) { builder.value(fieldEntry.getKey() + "^" + fieldEntry.getValue()); } builder.endArray(); - builder.field("use_dis_max", this.useDisMax); - builder.field("tie_breaker", this.tieBreaker); - builder.field("default_operator", this.defaultOperator.name().toLowerCase(Locale.ROOT)); + builder.field(QueryStringQueryParser.USE_DIS_MAX_FIELD.getPreferredName(), this.useDisMax); + builder.field(QueryStringQueryParser.TIE_BREAKER_FIELD.getPreferredName(), this.tieBreaker); + builder.field(QueryStringQueryParser.DEFAULT_OPERATOR_FIELD.getPreferredName(), this.defaultOperator.name().toLowerCase(Locale.ROOT)); if (this.analyzer != null) { - builder.field("analyzer", this.analyzer); + builder.field(QueryStringQueryParser.ANALYZER_FIELD.getPreferredName(), this.analyzer); } if (this.quoteAnalyzer != null) { - builder.field("quote_analyzer", this.quoteAnalyzer); + builder.field(QueryStringQueryParser.QUOTE_ANALYZER_FIELD.getPreferredName(), this.quoteAnalyzer); } - builder.field("auto_generate_phrase_queries", this.autoGeneratePhraseQueries); - builder.field("max_determinized_states", this.maxDeterminizedStates); + builder.field(QueryStringQueryParser.AUTO_GENERATED_PHRASE_QUERIES_FIELD.getPreferredName(), this.autoGeneratePhraseQueries); + builder.field(QueryStringQueryParser.MAX_DETERMINED_STATES_FIELD.getPreferredName(), this.maxDeterminizedStates); if (this.allowLeadingWildcard != null) { - builder.field("allow_leading_wildcard", this.allowLeadingWildcard); + builder.field(QueryStringQueryParser.ALLOW_LEADING_WILDCARD_FIELD.getPreferredName(), this.allowLeadingWildcard); } - builder.field("lowercase_expanded_terms", this.lowercaseExpandedTerms); - builder.field("enable_position_increments", this.enablePositionIncrements); + builder.field(QueryStringQueryParser.LOWERCASE_EXPANDED_TERMS_FIELD.getPreferredName(), this.lowercaseExpandedTerms); + builder.field(QueryStringQueryParser.ENABLE_POSITION_INCREMENTS_FIELD.getPreferredName(), this.enablePositionIncrements); this.fuzziness.toXContent(builder, params); - builder.field("fuzzy_prefix_length", this.fuzzyPrefixLength); - builder.field("fuzzy_max_expansions", this.fuzzyMaxExpansions); + builder.field(QueryStringQueryParser.FUZZY_PREFIX_LENGTH_FIELD.getPreferredName(), this.fuzzyPrefixLength); + builder.field(QueryStringQueryParser.FUZZY_MAX_EXPANSIONS_FIELD.getPreferredName(), this.fuzzyMaxExpansions); if (this.fuzzyRewrite != null) { - builder.field("fuzzy_rewrite", this.fuzzyRewrite); + builder.field(QueryStringQueryParser.FUZZY_REWRITE_FIELD.getPreferredName(), this.fuzzyRewrite); } - builder.field("phrase_slop", this.phraseSlop); + builder.field(QueryStringQueryParser.PHRASE_SLOP_FIELD.getPreferredName(), this.phraseSlop); if (this.analyzeWildcard != null) { - builder.field("analyze_wildcard", this.analyzeWildcard); + builder.field(QueryStringQueryParser.ANALYZE_WILDCARD_FIELD.getPreferredName(), this.analyzeWildcard); } if (this.rewrite != null) { - builder.field("rewrite", this.rewrite); + builder.field(QueryStringQueryParser.REWRITE_FIELD.getPreferredName(), this.rewrite); } if (this.minimumShouldMatch != null) { - builder.field("minimum_should_match", this.minimumShouldMatch); + builder.field(QueryStringQueryParser.MINIMUM_SHOULD_MATCH_FIELD.getPreferredName(), this.minimumShouldMatch); } if (this.quoteFieldSuffix != null) { - builder.field("quote_field_suffix", this.quoteFieldSuffix); + builder.field(QueryStringQueryParser.QUOTE_FIELD_SUFFIX_FIELD.getPreferredName(), this.quoteFieldSuffix); } if (this.lenient != null) { - builder.field("lenient", this.lenient); + builder.field(QueryStringQueryParser.LENIENT_FIELD.getPreferredName(), this.lenient); } - builder.field("locale", this.locale.toLanguageTag()); + builder.field(QueryStringQueryParser.LOCALE_FIELD.getPreferredName(), this.locale.toLanguageTag()); if (this.timeZone != null) { - builder.field("time_zone", this.timeZone.getID()); + builder.field(QueryStringQueryParser.TIME_ZONE_FIELD.getPreferredName(), this.timeZone.getID()); } - builder.field("escape", this.escape); + builder.field(QueryStringQueryParser.ESCAPE_FIELD.getPreferredName(), this.escape); printBoostAndQueryName(builder); builder.endObject(); } diff --git a/core/src/main/java/org/elasticsearch/index/query/QueryStringQueryParser.java b/core/src/main/java/org/elasticsearch/index/query/QueryStringQueryParser.java index 624cf6d731c..f7d9d2989dd 100644 --- a/core/src/main/java/org/elasticsearch/index/query/QueryStringQueryParser.java +++ b/core/src/main/java/org/elasticsearch/index/query/QueryStringQueryParser.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.query; +import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.unit.Fuzziness; @@ -34,6 +35,32 @@ import java.util.Map; */ public class QueryStringQueryParser implements QueryParser { + public static final ParseField QUERY_FIELD = new ParseField("query"); + public static final ParseField FIELDS_FIELD = new ParseField("fields"); + public static final ParseField DEFAULT_FIELD_FIELD = new ParseField("default_field"); + public static final ParseField DEFAULT_OPERATOR_FIELD = new ParseField("default_operator"); + public static final ParseField ANALYZER_FIELD = new ParseField("analyzer"); + public static final ParseField QUOTE_ANALYZER_FIELD = new ParseField("quote_analyzer"); + public static final ParseField ALLOW_LEADING_WILDCARD_FIELD = new ParseField("allow_leading_wildcard"); + public static final ParseField AUTO_GENERATED_PHRASE_QUERIES_FIELD = new ParseField("auto_generated_phrase_queries"); + public static final ParseField MAX_DETERMINED_STATES_FIELD = new ParseField("max_determined_states"); + public static final ParseField LOWERCASE_EXPANDED_TERMS_FIELD = new ParseField("lowercase_expanded_terms"); + public static final ParseField ENABLE_POSITION_INCREMENTS_FIELD = new ParseField("enable_position_increment"); + public static final ParseField ESCAPE_FIELD = new ParseField("escape"); + public static final ParseField USE_DIS_MAX_FIELD = new ParseField("use_dis_max"); + public static final ParseField FUZZY_PREFIX_LENGTH_FIELD = new ParseField("fuzzy_prefix_length"); + public static final ParseField FUZZY_MAX_EXPANSIONS_FIELD = new ParseField("fuzzy_max_expansions"); + public static final ParseField FUZZY_REWRITE_FIELD = new ParseField("fuzzy_rewrite"); + public static final ParseField PHRASE_SLOP_FIELD = new ParseField("phrase_slop"); + public static final ParseField TIE_BREAKER_FIELD = new ParseField("tie_breaker"); + public static final ParseField ANALYZE_WILDCARD_FIELD = new ParseField("analyze_wildcard"); + public static final ParseField REWRITE_FIELD = new ParseField("rewrite"); + public static final ParseField MINIMUM_SHOULD_MATCH_FIELD = new ParseField("minimum_should_match"); + public static final ParseField QUOTE_FIELD_SUFFIX_FIELD = new ParseField("quote_field_suffix"); + public static final ParseField LENIENT_FIELD = new ParseField("lenient"); + public static final ParseField LOCALE_FIELD = new ParseField("locale"); + public static final ParseField TIME_ZONE_FIELD = new ParseField("time_zone"); + @Override public String[] names() { return new String[]{QueryStringQueryBuilder.NAME, Strings.toCamelCase(QueryStringQueryBuilder.NAME)}; @@ -76,7 +103,7 @@ public class QueryStringQueryParser implements QueryParser { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token == XContentParser.Token.START_ARRAY) { - if ("fields".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, FIELDS_FIELD)) { while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { String fField = null; float fBoost = AbstractQueryBuilder.DEFAULT_BOOST; @@ -99,64 +126,64 @@ public class QueryStringQueryParser implements QueryParser { throw new ParsingException(parser.getTokenLocation(), "[" + QueryStringQueryBuilder.NAME + "] query does not support [" + currentFieldName + "]"); } } else if (token.isValue()) { - if ("query".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, QUERY_FIELD)) { queryString = parser.text(); - } else if ("default_field".equals(currentFieldName) || "defaultField".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, DEFAULT_FIELD_FIELD)) { defaultField = parser.text(); - } else if ("default_operator".equals(currentFieldName) || "defaultOperator".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, DEFAULT_OPERATOR_FIELD)) { defaultOperator = Operator.fromString(parser.text()); - } else if ("analyzer".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, ANALYZER_FIELD)) { analyzer = parser.text(); - } else if ("quote_analyzer".equals(currentFieldName) || "quoteAnalyzer".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, QUOTE_ANALYZER_FIELD)) { quoteAnalyzer = parser.text(); - } else if ("allow_leading_wildcard".equals(currentFieldName) || "allowLeadingWildcard".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, ALLOW_LEADING_WILDCARD_FIELD)) { allowLeadingWildcard = parser.booleanValue(); - } else if ("auto_generate_phrase_queries".equals(currentFieldName) || "autoGeneratePhraseQueries".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AUTO_GENERATED_PHRASE_QUERIES_FIELD)) { autoGeneratePhraseQueries = parser.booleanValue(); - } else if ("max_determinized_states".equals(currentFieldName) || "maxDeterminizedStates".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, MAX_DETERMINED_STATES_FIELD)) { maxDeterminizedStates = parser.intValue(); - } else if ("lowercase_expanded_terms".equals(currentFieldName) || "lowercaseExpandedTerms".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, LOWERCASE_EXPANDED_TERMS_FIELD)) { lowercaseExpandedTerms = parser.booleanValue(); - } else if ("enable_position_increments".equals(currentFieldName) || "enablePositionIncrements".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, ENABLE_POSITION_INCREMENTS_FIELD)) { enablePositionIncrements = parser.booleanValue(); - } else if ("escape".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, ESCAPE_FIELD)) { escape = parser.booleanValue(); - } else if ("use_dis_max".equals(currentFieldName) || "useDisMax".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, USE_DIS_MAX_FIELD)) { useDisMax = parser.booleanValue(); - } else if ("fuzzy_prefix_length".equals(currentFieldName) || "fuzzyPrefixLength".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, FUZZY_PREFIX_LENGTH_FIELD)) { fuzzyPrefixLength = parser.intValue(); - } else if ("fuzzy_max_expansions".equals(currentFieldName) || "fuzzyMaxExpansions".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, FUZZY_MAX_EXPANSIONS_FIELD)) { fuzzyMaxExpansions = parser.intValue(); - } else if ("fuzzy_rewrite".equals(currentFieldName) || "fuzzyRewrite".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, FUZZY_REWRITE_FIELD)) { fuzzyRewrite = parser.textOrNull(); - } else if ("phrase_slop".equals(currentFieldName) || "phraseSlop".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, PHRASE_SLOP_FIELD)) { phraseSlop = parser.intValue(); } else if (parseContext.parseFieldMatcher().match(currentFieldName, Fuzziness.FIELD)) { fuzziness = Fuzziness.parse(parser); - } else if ("boost".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) { boost = parser.floatValue(); - } else if ("tie_breaker".equals(currentFieldName) || "tieBreaker".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, TIE_BREAKER_FIELD)) { tieBreaker = parser.floatValue(); - } else if ("analyze_wildcard".equals(currentFieldName) || "analyzeWildcard".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, ANALYZE_WILDCARD_FIELD)) { analyzeWildcard = parser.booleanValue(); - } else if ("rewrite".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, REWRITE_FIELD)) { rewrite = parser.textOrNull(); - } else if ("minimum_should_match".equals(currentFieldName) || "minimumShouldMatch".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, MINIMUM_SHOULD_MATCH_FIELD)) { minimumShouldMatch = parser.textOrNull(); - } else if ("quote_field_suffix".equals(currentFieldName) || "quoteFieldSuffix".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, QUOTE_FIELD_SUFFIX_FIELD)) { quoteFieldSuffix = parser.textOrNull(); - } else if ("lenient".equalsIgnoreCase(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, LENIENT_FIELD)) { lenient = parser.booleanValue(); - } else if ("locale".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, LOCALE_FIELD)) { String localeStr = parser.text(); locale = Locale.forLanguageTag(localeStr); - } else if ("time_zone".equals(currentFieldName) || "timeZone".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, TIME_ZONE_FIELD)) { try { timeZone = parser.text(); } catch (IllegalArgumentException e) { throw new ParsingException(parser.getTokenLocation(), "[" + QueryStringQueryBuilder.NAME + "] time_zone [" + parser.text() + "] is unknown"); } - } else if ("_name".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) { queryName = parser.text(); } else { throw new ParsingException(parser.getTokenLocation(), "[" + QueryStringQueryBuilder.NAME + "] query does not support [" + currentFieldName + "]"); diff --git a/core/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java index 1c8b57c3879..cd99bec0f74 100644 --- a/core/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java @@ -233,15 +233,15 @@ public class RangeQueryBuilder extends AbstractQueryBuilder i protected void doXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(NAME); builder.startObject(fieldName); - builder.field("from", convertToStringIfBytesRef(this.from)); - builder.field("to", convertToStringIfBytesRef(this.to)); - builder.field("include_lower", includeLower); - builder.field("include_upper", includeUpper); + builder.field(RangeQueryParser.FROM_FIELD.getPreferredName(), convertToStringIfBytesRef(this.from)); + builder.field(RangeQueryParser.TO_FIELD.getPreferredName(), convertToStringIfBytesRef(this.to)); + builder.field(RangeQueryParser.INCLUDE_LOWER_FIELD.getPreferredName(), includeLower); + builder.field(RangeQueryParser.INCLUDE_UPPER_FIELD.getPreferredName(), includeUpper); if (timeZone != null) { - builder.field("time_zone", timeZone.getID()); + builder.field(RangeQueryParser.TIME_ZONE_FIELD.getPreferredName(), timeZone.getID()); } if (format != null) { - builder.field("format", format.format()); + builder.field(RangeQueryParser.FORMAT_FIELD.getPreferredName(), format.format()); } printBoostAndQueryName(builder); builder.endObject(); diff --git a/core/src/main/java/org/elasticsearch/index/query/RangeQueryParser.java b/core/src/main/java/org/elasticsearch/index/query/RangeQueryParser.java index dcd07b3e4eb..10a13dd52f5 100644 --- a/core/src/main/java/org/elasticsearch/index/query/RangeQueryParser.java +++ b/core/src/main/java/org/elasticsearch/index/query/RangeQueryParser.java @@ -30,8 +30,18 @@ import java.io.IOException; */ public class RangeQueryParser implements QueryParser { - private static final ParseField FIELDDATA_FIELD = new ParseField("fielddata").withAllDeprecated("[no replacement]"); - private static final ParseField NAME_FIELD = new ParseField("_name").withAllDeprecated("query name is not supported in short version of range query"); + public static final ParseField FIELDDATA_FIELD = new ParseField("fielddata").withAllDeprecated("[no replacement]"); + public static final ParseField NAME_FIELD = new ParseField("_name").withAllDeprecated("query name is not supported in short version of range query"); + public static final ParseField LTE_FIELD = new ParseField("lte", "le"); + public static final ParseField GTE_FIELD = new ParseField("gte", "ge"); + public static final ParseField FROM_FIELD = new ParseField("from"); + public static final ParseField TO_FIELD = new ParseField("to"); + public static final ParseField INCLUDE_LOWER_FIELD = new ParseField("include_lower"); + public static final ParseField INCLUDE_UPPER_FIELD = new ParseField("include_upper"); + public static final ParseField GT_FIELD = new ParseField("gt"); + public static final ParseField LT_FIELD = new ParseField("lt"); + public static final ParseField TIME_ZONE_FIELD = new ParseField("time_zone"); + public static final ParseField FORMAT_FIELD = new ParseField("format"); @Override public String[] names() { @@ -65,33 +75,33 @@ public class RangeQueryParser implements QueryParser { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else { - if ("from".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, FROM_FIELD)) { from = parser.objectBytes(); - } else if ("to".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, TO_FIELD)) { to = parser.objectBytes(); - } else if ("include_lower".equals(currentFieldName) || "includeLower".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, INCLUDE_LOWER_FIELD)) { includeLower = parser.booleanValue(); - } else if ("include_upper".equals(currentFieldName) || "includeUpper".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, INCLUDE_UPPER_FIELD)) { includeUpper = parser.booleanValue(); - } else if ("boost".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) { boost = parser.floatValue(); - } else if ("gt".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, GT_FIELD)) { from = parser.objectBytes(); includeLower = false; - } else if ("gte".equals(currentFieldName) || "ge".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, GTE_FIELD)) { from = parser.objectBytes(); includeLower = true; - } else if ("lt".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, LT_FIELD)) { to = parser.objectBytes(); includeUpper = false; - } else if ("lte".equals(currentFieldName) || "le".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, LTE_FIELD)) { to = parser.objectBytes(); includeUpper = true; - } else if ("time_zone".equals(currentFieldName) || "timeZone".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, TIME_ZONE_FIELD)) { timeZone = parser.text(); - } else if ("format".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, FORMAT_FIELD)) { format = parser.text(); - } else if ("_name".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) { queryName = parser.text(); } else { throw new ParsingException(parser.getTokenLocation(), "[range] query does not support [" + 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 f596bf84d5b..6f78a91a02a 100644 --- a/core/src/main/java/org/elasticsearch/index/query/RegexpQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/RegexpQueryBuilder.java @@ -135,14 +135,14 @@ public class RegexpQueryBuilder extends AbstractQueryBuilder } @Override - public void doXContent(XContentBuilder builder, Params params) throws IOException { + protected void doXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(NAME); builder.startObject(fieldName); - builder.field("value", this.value); - builder.field("flags_value", flagsValue); - builder.field("max_determinized_states", maxDeterminizedStates); + builder.field(RegexpQueryParser.VALUE_FIELD.getPreferredName(), this.value); + builder.field(RegexpQueryParser.FLAGS_VALUE_FIELD.getPreferredName(), flagsValue); + builder.field(RegexpQueryParser.MAX_DETERMINIZED_STATES_FIELD.getPreferredName(), maxDeterminizedStates); if (rewrite != null) { - builder.field("rewrite", rewrite); + builder.field(RegexpQueryParser.REWRITE_FIELD.getPreferredName(), rewrite); } printBoostAndQueryName(builder); builder.endObject(); @@ -155,7 +155,7 @@ public class RegexpQueryBuilder extends AbstractQueryBuilder } @Override - public Query doToQuery(QueryShardContext context) throws QueryShardException, IOException { + protected Query doToQuery(QueryShardContext context) throws QueryShardException, IOException { MultiTermQuery.RewriteMethod method = QueryParsers.parseRewriteMethod(context.parseFieldMatcher(), rewrite, null); Query query = null; @@ -174,7 +174,7 @@ public class RegexpQueryBuilder extends AbstractQueryBuilder } @Override - public RegexpQueryBuilder doReadFrom(StreamInput in) throws IOException { + protected RegexpQueryBuilder doReadFrom(StreamInput in) throws IOException { RegexpQueryBuilder regexpQueryBuilder = new RegexpQueryBuilder(in.readString(), in.readString()); regexpQueryBuilder.flagsValue = in.readVInt(); regexpQueryBuilder.maxDeterminizedStates = in.readVInt(); @@ -183,7 +183,7 @@ public class RegexpQueryBuilder extends AbstractQueryBuilder } @Override - public void doWriteTo(StreamOutput out) throws IOException { + protected void doWriteTo(StreamOutput out) throws IOException { out.writeString(fieldName); out.writeString(value); out.writeVInt(flagsValue); @@ -192,12 +192,12 @@ public class RegexpQueryBuilder extends AbstractQueryBuilder } @Override - public int doHashCode() { + protected int doHashCode() { return Objects.hash(fieldName, value, flagsValue, maxDeterminizedStates, rewrite); } @Override - public boolean doEquals(RegexpQueryBuilder other) { + protected boolean doEquals(RegexpQueryBuilder other) { return Objects.equals(fieldName, other.fieldName) && Objects.equals(value, other.value) && Objects.equals(flagsValue, other.flagsValue) && diff --git a/core/src/main/java/org/elasticsearch/index/query/RegexpQueryParser.java b/core/src/main/java/org/elasticsearch/index/query/RegexpQueryParser.java index d07c23da171..92305abc1df 100644 --- a/core/src/main/java/org/elasticsearch/index/query/RegexpQueryParser.java +++ b/core/src/main/java/org/elasticsearch/index/query/RegexpQueryParser.java @@ -30,7 +30,12 @@ import java.io.IOException; */ public class RegexpQueryParser implements QueryParser { - private static final ParseField NAME_FIELD = new ParseField("_name").withAllDeprecated("query name is not supported in short version of regexp query"); + public static final ParseField NAME_FIELD = new ParseField("_name").withAllDeprecated("query name is not supported in short version of regexp query"); + public static final ParseField FLAGS_VALUE_FIELD = new ParseField("flags_value"); + public static final ParseField MAX_DETERMINIZED_STATES_FIELD = new ParseField("max_determinized_states"); + public static final ParseField FLAGS_FIELD = new ParseField("flags"); + public static final ParseField REWRITE_FIELD = new ParseField("rewrite"); + public static final ParseField VALUE_FIELD = new ParseField("value"); @Override public String[] names() { @@ -62,20 +67,20 @@ public class RegexpQueryParser implements QueryParser { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else { - if ("value".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, VALUE_FIELD)) { value = parser.textOrNull(); - } else if ("boost".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) { boost = parser.floatValue(); - } else if ("rewrite".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, REWRITE_FIELD)) { rewrite = parser.textOrNull(); - } else if ("flags".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, FLAGS_FIELD)) { String flags = parser.textOrNull(); flagsValue = RegexpFlag.resolveValue(flags); - } else if ("max_determinized_states".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, MAX_DETERMINIZED_STATES_FIELD)) { maxDeterminizedStates = parser.intValue(); - } else if ("flags_value".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, FLAGS_VALUE_FIELD)) { flagsValue = parser.intValue(); - } else if ("_name".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) { queryName = parser.text(); } else { throw new ParsingException(parser.getTokenLocation(), "[regexp] query does not support [" + currentFieldName + "]"); diff --git a/core/src/main/java/org/elasticsearch/index/query/ScriptQueryParser.java b/core/src/main/java/org/elasticsearch/index/query/ScriptQueryParser.java index 97ad0a21873..51e299815bc 100644 --- a/core/src/main/java/org/elasticsearch/index/query/ScriptQueryParser.java +++ b/core/src/main/java/org/elasticsearch/index/query/ScriptQueryParser.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.query; +import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.script.Script; @@ -35,6 +36,8 @@ import java.util.Map; */ public class ScriptQueryParser implements QueryParser { + public static final ParseField PARAMS_FIELD = new ParseField("params"); + @Override public String[] names() { return new String[]{ScriptQueryBuilder.NAME}; @@ -62,15 +65,15 @@ public class ScriptQueryParser implements QueryParser { } else if (token == XContentParser.Token.START_OBJECT) { if (parseContext.parseFieldMatcher().match(currentFieldName, ScriptField.SCRIPT)) { script = Script.parse(parser, parseContext.parseFieldMatcher()); - } else if ("params".equals(currentFieldName)) { // TODO remove in 3.0 (here to support old script APIs) + } else if (parseContext.parseFieldMatcher().match(currentFieldName, PARAMS_FIELD)) { // TODO remove in 3.0 (here to support old script APIs) params = parser.map(); } else { throw new ParsingException(parser.getTokenLocation(), "[script] query does not support [" + currentFieldName + "]"); } } else if (token.isValue()) { - if ("_name".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) { queryName = parser.text(); - } else if ("boost".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) { boost = parser.floatValue(); } else if (!scriptParameterParser.token(currentFieldName, token, parser, parseContext.parseFieldMatcher())) { throw new ParsingException(parser.getTokenLocation(), "[script] query does not support [" + currentFieldName + "]"); 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 29720195c83..092f966d8d7 100644 --- a/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringBuilder.java @@ -303,10 +303,10 @@ public class SimpleQueryStringBuilder extends AbstractQueryBuilder 0) { - builder.startArray("fields"); + builder.startArray(SimpleQueryStringParser.FIELDS_FIELD.getPreferredName()); for (Map.Entry entry : fieldsAndWeights.entrySet()) { builder.value(entry.getKey() + "^" + entry.getValue()); } @@ -314,18 +314,18 @@ public class SimpleQueryStringBuilder extends AbstractQueryBuilder { + public static final ParseField MINIMUM_SHOULD_MATCH_FIELD = new ParseField("minimum_should_match"); + public static final ParseField ANALYZE_WILDCARD_FIELD = new ParseField("analyze_wildcard"); + public static final ParseField LENIENT_FIELD = new ParseField("lenient"); + public static final ParseField LOWERCASE_EXPANDED_TERMS_FIELD = new ParseField("lowercase_expanded_terms"); + public static final ParseField LOCALE_FIELD = new ParseField("locale"); + public static final ParseField FLAGS_FIELD = new ParseField("flags"); + public static final ParseField DEFAULT_OPERATOR_FIELD = new ParseField("default_operator"); + public static final ParseField ANALYZER_FIELD = new ParseField("analyzer"); + public static final ParseField QUERY_FIELD = new ParseField("query"); + public static final ParseField FIELDS_FIELD = new ParseField("fields"); + @Override public String[] names() { return new String[]{SimpleQueryStringBuilder.NAME, Strings.toCamelCase(SimpleQueryStringBuilder.NAME)}; @@ -88,7 +100,7 @@ public class SimpleQueryStringParser implements QueryParser { + public static final ParseField BIG_FIELD = new ParseField("big"); + public static final ParseField LITTLE_FIELD = new ParseField("little"); + @Override public String[] names() { return new String[]{SpanContainingQueryBuilder.NAME, Strings.toCamelCase(SpanContainingQueryBuilder.NAME)}; @@ -49,13 +53,13 @@ public class SpanContainingQueryParser implements QueryParser)) { throw new ParsingException(parser.getTokenLocation(), "span_containing [big] must be of type span query"); } big = (SpanQueryBuilder) query; - } else if ("little".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, LITTLE_FIELD)) { QueryBuilder query = parseContext.parseInnerQueryBuilder(); if (!(query instanceof SpanQueryBuilder)) { throw new ParsingException(parser.getTokenLocation(), "span_containing [little] must be of type span query"); @@ -64,9 +68,9 @@ public class SpanContainingQueryParser implements QueryParser { + public static final ParseField MATCH_FIELD = new ParseField("match"); + public static final ParseField END_FIELD = new ParseField("end"); + @Override public String[] names() { return new String[]{SpanFirstQueryBuilder.NAME, Strings.toCamelCase(SpanFirstQueryBuilder.NAME)}; @@ -51,7 +55,7 @@ public class SpanFirstQueryParser implements QueryParser if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token == XContentParser.Token.START_OBJECT) { - if ("match".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, MATCH_FIELD)) { QueryBuilder query = parseContext.parseInnerQueryBuilder(); if (!(query instanceof SpanQueryBuilder)) { throw new ParsingException(parser.getTokenLocation(), "spanFirst [match] must be of type span query"); @@ -61,11 +65,11 @@ public class SpanFirstQueryParser implements QueryParser throw new ParsingException(parser.getTokenLocation(), "[span_first] query does not support [" + currentFieldName + "]"); } } else { - if ("boost".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) { boost = parser.floatValue(); - } else if ("end".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, END_FIELD)) { end = parser.intValue(); - } else if ("_name".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) { queryName = parser.text(); } else { throw new ParsingException(parser.getTokenLocation(), "[span_first] query does not support [" + currentFieldName + "]"); diff --git a/core/src/main/java/org/elasticsearch/index/query/SpanMultiTermQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/SpanMultiTermQueryBuilder.java index 1d7a5c7e412..21c9c615551 100644 --- a/core/src/main/java/org/elasticsearch/index/query/SpanMultiTermQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/SpanMultiTermQueryBuilder.java @@ -56,7 +56,7 @@ public class SpanMultiTermQueryBuilder extends AbstractQueryBuilder { - public static final String MATCH_NAME = "match"; + public static final ParseField MATCH_FIELD = new ParseField("match"); @Override public String[] names() { @@ -48,19 +49,19 @@ public class SpanMultiTermQueryParser implements QueryParser { + public static final ParseField SLOP_FIELD = new ParseField("slop"); + public static final ParseField COLLECT_PAYLOADS_FIELD = new ParseField("collect_payloads"); + public static final ParseField CLAUSES_FIELD = new ParseField("clauses"); + public static final ParseField IN_ORDER_FIELD = new ParseField("in_order"); + @Override public String[] names() { return new String[]{SpanNearQueryBuilder.NAME, Strings.toCamelCase(SpanNearQueryBuilder.NAME)}; @@ -55,7 +61,7 @@ public class SpanNearQueryParser implements QueryParser { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token == XContentParser.Token.START_ARRAY) { - if ("clauses".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, CLAUSES_FIELD)) { while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { QueryBuilder query = parseContext.parseInnerQueryBuilder(); if (!(query instanceof SpanQueryBuilder)) { @@ -67,15 +73,15 @@ public class SpanNearQueryParser implements QueryParser { throw new ParsingException(parser.getTokenLocation(), "[span_near] query does not support [" + currentFieldName + "]"); } } else if (token.isValue()) { - if ("in_order".equals(currentFieldName) || "inOrder".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, IN_ORDER_FIELD)) { inOrder = parser.booleanValue(); - } else if ("collect_payloads".equals(currentFieldName) || "collectPayloads".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, COLLECT_PAYLOADS_FIELD)) { collectPayloads = parser.booleanValue(); - } else if ("slop".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, SLOP_FIELD)) { slop = parser.intValue(); - } else if ("boost".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) { boost = parser.floatValue(); - } else if ("_name".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) { queryName = parser.text(); } else { throw new ParsingException(parser.getTokenLocation(), "[span_near] query does not support [" + currentFieldName + "]"); diff --git a/core/src/main/java/org/elasticsearch/index/query/SpanNotQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/SpanNotQueryBuilder.java index ffe3cecf412..780344b70b2 100644 --- a/core/src/main/java/org/elasticsearch/index/query/SpanNotQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/SpanNotQueryBuilder.java @@ -125,12 +125,12 @@ public class SpanNotQueryBuilder extends AbstractQueryBuilder { + public static final ParseField POST_FIELD = new ParseField("post"); + public static final ParseField PRE_FIELD = new ParseField("pre"); + public static final ParseField DIST_FIELD = new ParseField("dist"); + public static final ParseField EXCLUDE_FIELD = new ParseField("exclude"); + public static final ParseField INCLUDE_FIELD = new ParseField("include"); + @Override public String[] names() { return new String[]{SpanNotQueryBuilder.NAME, Strings.toCamelCase(SpanNotQueryBuilder.NAME)}; @@ -56,13 +63,13 @@ public class SpanNotQueryParser implements QueryParser { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token == XContentParser.Token.START_OBJECT) { - if ("include".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, INCLUDE_FIELD)) { QueryBuilder query = parseContext.parseInnerQueryBuilder(); if (!(query instanceof SpanQueryBuilder)) { throw new ParsingException(parser.getTokenLocation(), "spanNot [include] must be of type span query"); } include = (SpanQueryBuilder) query; - } else if ("exclude".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, EXCLUDE_FIELD)) { QueryBuilder query = parseContext.parseInnerQueryBuilder(); if (!(query instanceof SpanQueryBuilder)) { throw new ParsingException(parser.getTokenLocation(), "spanNot [exclude] must be of type span query"); @@ -72,15 +79,15 @@ public class SpanNotQueryParser implements QueryParser { throw new ParsingException(parser.getTokenLocation(), "[span_not] query does not support [" + currentFieldName + "]"); } } else { - if ("dist".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, DIST_FIELD)) { dist = parser.intValue(); - } else if ("pre".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, PRE_FIELD)) { pre = parser.intValue(); - } else if ("post".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, POST_FIELD)) { post = parser.intValue(); - } else if ("boost".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) { boost = parser.floatValue(); - } else if ("_name".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) { queryName = parser.text(); } else { throw new ParsingException(parser.getTokenLocation(), "[span_not] query does not support [" + currentFieldName + "]"); diff --git a/core/src/main/java/org/elasticsearch/index/query/SpanOrQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/SpanOrQueryBuilder.java index a46bef4e520..3b8681c685b 100644 --- a/core/src/main/java/org/elasticsearch/index/query/SpanOrQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/SpanOrQueryBuilder.java @@ -67,7 +67,7 @@ public class SpanOrQueryBuilder extends AbstractQueryBuilder @Override protected void doXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(NAME); - builder.startArray("clauses"); + builder.startArray(SpanOrQueryParser.CLAUSES_FIELD.getPreferredName()); for (SpanQueryBuilder clause : clauses) { clause.toXContent(builder, params); } diff --git a/core/src/main/java/org/elasticsearch/index/query/SpanOrQueryParser.java b/core/src/main/java/org/elasticsearch/index/query/SpanOrQueryParser.java index a0dabbdad06..50500def865 100644 --- a/core/src/main/java/org/elasticsearch/index/query/SpanOrQueryParser.java +++ b/core/src/main/java/org/elasticsearch/index/query/SpanOrQueryParser.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.query; +import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.xcontent.XContentParser; @@ -32,6 +33,8 @@ import java.util.List; */ public class SpanOrQueryParser implements QueryParser { + public static final ParseField CLAUSES_FIELD = new ParseField("clauses"); + @Override public String[] names() { return new String[]{SpanOrQueryBuilder.NAME, Strings.toCamelCase(SpanOrQueryBuilder.NAME)}; @@ -52,7 +55,7 @@ public class SpanOrQueryParser implements QueryParser { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token == XContentParser.Token.START_ARRAY) { - if ("clauses".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, CLAUSES_FIELD)) { while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { QueryBuilder query = parseContext.parseInnerQueryBuilder(); if (!(query instanceof SpanQueryBuilder)) { @@ -64,9 +67,9 @@ public class SpanOrQueryParser implements QueryParser { throw new ParsingException(parser.getTokenLocation(), "[span_or] query does not support [" + currentFieldName + "]"); } } else { - if ("boost".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) { boost = parser.floatValue(); - } else if ("_name".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) { queryName = parser.text(); } else { throw new ParsingException(parser.getTokenLocation(), "[span_or] query does not support [" + currentFieldName + "]"); 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 fc41dc4ba0d..7e234e551c3 100644 --- a/core/src/main/java/org/elasticsearch/index/query/SpanTermQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/SpanTermQueryBuilder.java @@ -68,7 +68,7 @@ public class SpanTermQueryBuilder extends BaseTermQueryBuilder { + public static final ParseField TERM_FIELD = new ParseField("term"); + @Override public String[] names() { return new String[]{SpanTermQueryBuilder.NAME, Strings.toCamelCase(SpanTermQueryBuilder.NAME)}; @@ -58,13 +61,13 @@ public class SpanTermQueryParser implements QueryParser { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else { - if ("term".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, TERM_FIELD)) { value = parser.objectBytes(); - } else if ("value".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, BaseTermQueryBuilder.VALUE_FIELD)) { value = parser.objectBytes(); - } else if ("boost".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) { boost = parser.floatValue(); - } else if ("_name".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) { queryName = parser.text(); } else { throw new ParsingException(parser.getTokenLocation(), "[span_term] query does not support [" + currentFieldName + "]"); diff --git a/core/src/main/java/org/elasticsearch/index/query/SpanWithinQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/SpanWithinQueryBuilder.java index c3a11c8f325..440e1797dfa 100644 --- a/core/src/main/java/org/elasticsearch/index/query/SpanWithinQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/SpanWithinQueryBuilder.java @@ -73,10 +73,10 @@ public class SpanWithinQueryBuilder extends AbstractQueryBuilder { + public static final ParseField BIG_FIELD = new ParseField("big"); + public static final ParseField LITTLE_FIELD = new ParseField("little"); + @Override public String[] names() { return new String[]{SpanWithinQueryBuilder.NAME, Strings.toCamelCase(SpanWithinQueryBuilder.NAME)}; @@ -50,13 +54,13 @@ public class SpanWithinQueryParser implements QueryParser { } @Override - public Query doToQuery(QueryShardContext context) throws IOException { + protected Query doToQuery(QueryShardContext context) throws IOException { Query query = null; MappedFieldType mapper = context.fieldMapper(this.fieldName); if (mapper != null) { diff --git a/core/src/main/java/org/elasticsearch/index/query/TermQueryParser.java b/core/src/main/java/org/elasticsearch/index/query/TermQueryParser.java index 0591497a3c8..5ac083d6217 100644 --- a/core/src/main/java/org/elasticsearch/index/query/TermQueryParser.java +++ b/core/src/main/java/org/elasticsearch/index/query/TermQueryParser.java @@ -30,8 +30,8 @@ import java.io.IOException; */ public class TermQueryParser implements QueryParser { - private static final ParseField NAME_FIELD = new ParseField("_name").withAllDeprecated("query name is not supported in short version of term query"); - private static final ParseField BOOST_FIELD = new ParseField("boost").withAllDeprecated("boost is not supported in short version of term query"); + public static final ParseField TERM_FIELD = new ParseField("term"); + public static final ParseField VALUE_FIELD = new ParseField("value"); @Override public String[] names() { @@ -63,13 +63,13 @@ public class TermQueryParser implements QueryParser { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else { - if ("term".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, TERM_FIELD)) { value = parser.objectBytes(); - } else if ("value".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, VALUE_FIELD)) { value = parser.objectBytes(); - } else if ("_name".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) { queryName = parser.text(); - } else if ("boost".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) { boost = parser.floatValue(); } else { throw new ParsingException(parser.getTokenLocation(), "[term] query does not support [" + currentFieldName + "]"); @@ -77,17 +77,11 @@ public class TermQueryParser implements QueryParser { } } } else if (token.isValue()) { - if (parseContext.parseFieldMatcher().match(currentFieldName, NAME_FIELD)) { - queryName = parser.text(); - } else if (parseContext.parseFieldMatcher().match(currentFieldName, BOOST_FIELD)) { - boost = parser.floatValue(); - } else { - if (fieldName != null) { - throw new ParsingException(parser.getTokenLocation(), "[term] query does not support different field names, use [bool] query instead"); - } - fieldName = currentFieldName; - value = parser.objectBytes(); + if (fieldName != null) { + throw new ParsingException(parser.getTokenLocation(), "[term] query does not support different field names, use [bool] query instead"); } + fieldName = currentFieldName; + value = parser.objectBytes(); } else if (token == XContentParser.Token.START_ARRAY) { throw new ParsingException(parser.getTokenLocation(), "[term] query does not support array of values"); } 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 c90034cb04d..b2bcce4dfaf 100644 --- a/core/src/main/java/org/elasticsearch/index/query/TermsQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/TermsQueryBuilder.java @@ -207,7 +207,7 @@ public class TermsQueryBuilder extends AbstractQueryBuilder { } @Override - public void doXContent(XContentBuilder builder, Params params) throws IOException { + protected void doXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(NAME); if (this.termsLookup != null) { builder.startObject(fieldName); diff --git a/core/src/main/java/org/elasticsearch/index/query/TermsQueryParser.java b/core/src/main/java/org/elasticsearch/index/query/TermsQueryParser.java index b773f449535..310256556c8 100644 --- a/core/src/main/java/org/elasticsearch/index/query/TermsQueryParser.java +++ b/core/src/main/java/org/elasticsearch/index/query/TermsQueryParser.java @@ -74,9 +74,9 @@ public class TermsQueryParser implements QueryParser { fieldName = currentFieldName; termsLookup = TermsLookup.parseTermsLookup(parser); } else if (token.isValue()) { - if ("boost".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) { boost = parser.floatValue(); - } else if ("_name".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) { queryName = parser.text(); } else { throw new ParsingException(parser.getTokenLocation(), "[" + TermsQueryBuilder.NAME + "] query does not support [" + currentFieldName + "]"); 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 a0019795767..975736e842a 100644 --- a/core/src/main/java/org/elasticsearch/index/query/TypeQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/TypeQueryBuilder.java @@ -62,7 +62,7 @@ public class TypeQueryBuilder extends AbstractQueryBuilder { @Override protected void doXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(NAME); - builder.field("value", type.utf8ToString()); + builder.field(TypeQueryParser.VALUE_FIELD.getPreferredName(), type.utf8ToString()); printBoostAndQueryName(builder); builder.endObject(); } diff --git a/core/src/main/java/org/elasticsearch/index/query/TypeQueryParser.java b/core/src/main/java/org/elasticsearch/index/query/TypeQueryParser.java index 10adc0c7390..d746b4656a0 100644 --- a/core/src/main/java/org/elasticsearch/index/query/TypeQueryParser.java +++ b/core/src/main/java/org/elasticsearch/index/query/TypeQueryParser.java @@ -20,6 +20,7 @@ package org.elasticsearch.index.query; import org.apache.lucene.util.BytesRef; +import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.xcontent.XContentParser; @@ -30,6 +31,8 @@ import java.io.IOException; */ public class TypeQueryParser implements QueryParser { + public static final ParseField VALUE_FIELD = new ParseField("value"); + @Override public String[] names() { return new String[]{TypeQueryBuilder.NAME}; @@ -49,11 +52,11 @@ public class TypeQueryParser implements QueryParser { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token.isValue()) { - if ("_name".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) { queryName = parser.text(); - } else if ("boost".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) { boost = parser.floatValue(); - } else if ("value".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, VALUE_FIELD)) { type = parser.utf8Bytes(); } else { throw new ParsingException(parser.getTokenLocation(), "[" + TypeQueryBuilder.NAME + "] filter doesn't support [" + currentFieldName + "]"); 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 44775926400..7c3cc1c30a3 100644 --- a/core/src/main/java/org/elasticsearch/index/query/WildcardQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/WildcardQueryBuilder.java @@ -99,12 +99,12 @@ public class WildcardQueryBuilder extends AbstractQueryBuilder { + public static final ParseField WILDCARD_FIELD = new ParseField("wildcard"); + public static final ParseField VALUE_FIELD = new ParseField("value"); + public static final ParseField REWRITE_FIELD = new ParseField("rewrite"); + @Override public String[] names() { return new String[]{WildcardQueryBuilder.NAME}; @@ -55,15 +60,15 @@ public class WildcardQueryParser implements QueryParser { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else { - if ("wildcard".equals(currentFieldName)) { + if (parseContext.parseFieldMatcher().match(currentFieldName, WILDCARD_FIELD)) { value = parser.text(); - } else if ("value".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, VALUE_FIELD)) { value = parser.text(); - } else if ("boost".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) { boost = parser.floatValue(); - } else if ("rewrite".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, REWRITE_FIELD)) { rewrite = parser.textOrNull(); - } else if ("_name".equals(currentFieldName)) { + } else if (parseContext.parseFieldMatcher().match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) { queryName = parser.text(); } else { throw new ParsingException(parser.getTokenLocation(), "[wildcard] query does not support [" + currentFieldName + "]"); diff --git a/core/src/main/java/org/elasticsearch/index/query/WrapperQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/WrapperQueryBuilder.java index e87b17831d1..e908d763311 100644 --- a/core/src/main/java/org/elasticsearch/index/query/WrapperQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/WrapperQueryBuilder.java @@ -94,7 +94,7 @@ public class WrapperQueryBuilder extends AbstractQueryBuilder> assertParsedQuery(builder.bytes(), testQuery); for (Map.Entry alternateVersion : getAlternateVersions().entrySet()) { String queryAsString = alternateVersion.getKey(); - assertParsedQuery(new BytesArray(queryAsString), alternateVersion.getValue()); + assertParsedQuery(new BytesArray(queryAsString), alternateVersion.getValue(), ParseFieldMatcher.EMPTY); } } } @@ -869,4 +869,54 @@ public abstract class AbstractQueryTestCase> throw new UnsupportedOperationException("this test can't handle MultiTermVector requests"); } + /** + * Call this method to check a valid json string representing the query under test against + * it's generated json. + * + * Note: By the time of this writing (Nov 2015) all queries are taken from the query dsl + * reference docs mirroring examples there. Here's how the queries were generated: + * + *
    + *
  • Take a reference documentation example. + *
  • Stick it into the createParseableQueryJson method of the respective query test. + *
  • Manually check that what the QueryBuilder generates equals the input json ignoring default options. + *
  • Put the manual checks into the asserQueryParsedFromJson method. + *
  • Now copy the generated json including default options into createParseableQueryJso + *
  • By now the roundtrip check for the json should be happy. + *
+ **/ + public static void checkGeneratedJson(String expected, QueryBuilder source) throws IOException { + // now assert that we actually generate the same JSON + XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint(); + source.toXContent(builder, ToXContent.EMPTY_PARAMS); + assertEquals( + msg(expected, builder.string()), + expected.replaceAll("\\s+",""), + builder.string().replaceAll("\\s+","")); + } + + private static String msg(String left, String right) { + int size = Math.min(left.length(), right.length()); + StringBuilder builder = new StringBuilder("size: " + left.length() + " vs. " + right.length()); + builder.append(" content: <<"); + for (int i = 0; i < size; i++) { + if (left.charAt(i) == right.charAt(i)) { + builder.append(left.charAt(i)); + } else { + builder.append(">> ").append("until offset: ").append(i) + .append(" [").append(left.charAt(i)).append(" vs.").append(right.charAt(i)) + .append("] [").append((int)left.charAt(i) ).append(" vs.").append((int)right.charAt(i)).append(']'); + return builder.toString(); + } + } + if (left.length() != right.length()) { + int leftEnd = Math.max(size, left.length()) - 1; + int rightEnd = Math.max(size, right.length()) - 1; + builder.append(">> ").append("until offset: ").append(size) + .append(" [").append(left.charAt(leftEnd)).append(" vs.").append(right.charAt(rightEnd)) + .append("] [").append((int)left.charAt(leftEnd)).append(" vs.").append((int)right.charAt(rightEnd)).append(']'); + return builder.toString(); + } + return ""; + } } diff --git a/core/src/test/java/org/elasticsearch/index/query/BoolQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/BoolQueryBuilderTests.java index 7bd8ffeefd3..5d94a9a8bca 100644 --- a/core/src/test/java/org/elasticsearch/index/query/BoolQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/BoolQueryBuilderTests.java @@ -24,6 +24,7 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; + import org.hamcrest.Matchers; import java.io.IOException; @@ -243,4 +244,65 @@ public class BoolQueryBuilderTests extends AbstractQueryTestCase String pointTest3 = "{\"geohash_cell\": {\"pin\": [" + point.getX() + "," + point.getY() + "]}}"; assertParsedQuery(pointTest3, pointTestBuilder); } + + public void testFromJson() throws IOException { + String json = + "{\n" + + " \"geohash_cell\" : {\n" + + " \"neighbors\" : true,\n" + + " \"precision\" : 3,\n" + + " \"pin\" : \"t4mk70fgk067\",\n" + + " \"boost\" : 1.0\n" + + " }\n" + + "}"; + GeohashCellQuery.Builder parsed = (GeohashCellQuery.Builder) parseQuery(json); + checkGeneratedJson(json, parsed); + assertEquals(json, 3, parsed.precision().intValue()); + } } diff --git a/core/src/test/java/org/elasticsearch/index/query/HasChildQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/HasChildQueryBuilderTests.java index 422f779620a..51da0fc3996 100644 --- a/core/src/test/java/org/elasticsearch/index/query/HasChildQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/HasChildQueryBuilderTests.java @@ -28,6 +28,7 @@ import org.apache.lucene.search.join.ScoreMode; import org.apache.lucene.util.BytesRef; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; +import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.xcontent.ToXContent; @@ -197,8 +198,9 @@ public class HasChildQueryBuilderTests extends AbstractQueryTestCase> ").append("until offset: ").append(i) - .append(" [").append(left.charAt(i)).append(" vs.").append(right.charAt(i)) - .append("] [").append((int)left.charAt(i) ).append(" vs.").append((int)right.charAt(i)).append(']'); - return builder.toString(); - } - } - if (left.length() != right.length()) { - int leftEnd = Math.max(size, left.length()) - 1; - int rightEnd = Math.max(size, right.length()) - 1; - builder.append(">> ").append("until offset: ").append(size) - .append(" [").append(left.charAt(leftEnd)).append(" vs.").append(right.charAt(rightEnd)) - .append("] [").append((int)left.charAt(leftEnd)).append(" vs.").append((int)right.charAt(rightEnd)).append(']'); - return builder.toString(); - } - return ""; } - public void testToQueryInnerQueryType() throws IOException { String[] searchTypes = new String[]{PARENT_TYPE}; QueryShardContext.setTypes(searchTypes); diff --git a/core/src/test/java/org/elasticsearch/index/query/HasParentQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/HasParentQueryBuilderTests.java index b47c277eb06..b391930c32c 100644 --- a/core/src/test/java/org/elasticsearch/index/query/HasParentQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/HasParentQueryBuilderTests.java @@ -242,4 +242,27 @@ public class HasParentQueryBuilderTests extends AbstractQueryTestCase assertThat(e.getMessage(), is("Illegal value for id, expecting a string or number, got: START_ARRAY")); } } + + public void testFromJson() throws IOException { + String json = + "{\n" + + " \"ids\" : {\n" + + " \"type\" : [ \"my_type\" ],\n" + + " \"values\" : [ \"1\", \"100\", \"4\" ],\n" + + " \"boost\" : 1.0\n" + + " }\n" + + "}"; + IdsQueryBuilder parsed = (IdsQueryBuilder) parseQuery(json); + checkGeneratedJson(json, parsed); + assertEquals(json, 3, parsed.ids().size()); + assertEquals(json, "my_type", parsed.types()[0]); + } } diff --git a/core/src/test/java/org/elasticsearch/index/query/IndicesQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/IndicesQueryBuilderTests.java index 6b7318d7273..834dce857a6 100644 --- a/core/src/test/java/org/elasticsearch/index/query/IndicesQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/IndicesQueryBuilderTests.java @@ -96,4 +96,35 @@ public class IndicesQueryBuilderTests extends AbstractQueryTestCase