From 1db3c67e31047be06d471498aa5517c0306e3527 Mon Sep 17 00:00:00 2001 From: javanna Date: Mon, 8 Aug 2016 19:40:23 +0200 Subject: [PATCH 01/12] Throw parsing error if common terms query contains multiple fields in its short version --- .../index/query/CommonTermsQueryBuilder.java | 4 ++++ .../index/query/CommonTermsQueryBuilderTests.java | 9 +++++++++ 2 files changed, 13 insertions(+) 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 c2ad9983290..6d4715e912c 100644 --- a/core/src/main/java/org/elasticsearch/index/query/CommonTermsQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/CommonTermsQueryBuilder.java @@ -345,6 +345,10 @@ public class CommonTermsQueryBuilder extends AbstractQueryBuilder parseQuery(json)); assertEquals("[common] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage()); + + String shortJson = "{\n" + + " \"common\" : {\n" + + " \"message1\" : \"nelly the elephant not as a cartoon\",\n" + + " \"message2\" : \"nelly the elephant not as a cartoon\"\n" + + " }\n" + + "}"; + e = expectThrows(ParsingException.class, () -> parseQuery(shortJson)); + assertEquals("[common] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage()); } } From 5d238e86f6c83df2661997ffb508a22d7c5ab22c Mon Sep 17 00:00:00 2001 From: javanna Date: Mon, 8 Aug 2016 19:40:54 +0200 Subject: [PATCH 02/12] Throw parsing error if fuzzy query contains multiple fields in its short version --- .../elasticsearch/index/query/FuzzyQueryBuilder.java | 4 ++++ .../index/query/FuzzyQueryBuilderTests.java | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/core/src/main/java/org/elasticsearch/index/query/FuzzyQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/FuzzyQueryBuilder.java index 31cce0885a1..e7505e33170 100644 --- a/core/src/main/java/org/elasticsearch/index/query/FuzzyQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/FuzzyQueryBuilder.java @@ -309,6 +309,10 @@ public class FuzzyQueryBuilder extends AbstractQueryBuilder i } } } else { + if (fieldName != null) { + throw new ParsingException(parser.getTokenLocation(), "[fuzzy] query doesn't support multiple fields, found [" + + fieldName + "] and [" + parser.currentName() + "]"); + } fieldName = parser.currentName(); value = parser.objectBytes(); } diff --git a/core/src/test/java/org/elasticsearch/index/query/FuzzyQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/FuzzyQueryBuilderTests.java index 6e02e157f07..205aaa24484 100644 --- a/core/src/test/java/org/elasticsearch/index/query/FuzzyQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/FuzzyQueryBuilderTests.java @@ -169,5 +169,15 @@ public class FuzzyQueryBuilderTests extends AbstractQueryTestCase parseQuery(json)); assertEquals("[fuzzy] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage()); + + String shortJson = "{\n" + + " \"fuzzy\" : {\n" + + " \"message1\" : \"this is a test\",\n" + + " \"message2\" : \"value\" : \"this is a test\"\n" + + " }\n" + + "}"; + + e = expectThrows(ParsingException.class, () -> parseQuery(shortJson)); + assertEquals("[fuzzy] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage()); } } From cb41f304f29ff6270d8204f20eb7724e93ee015f Mon Sep 17 00:00:00 2001 From: javanna Date: Mon, 8 Aug 2016 19:41:45 +0200 Subject: [PATCH 03/12] Throw parsing error if match phrase prefix query contains multiple fields in its short version --- .../index/query/MatchPhrasePrefixQueryBuilder.java | 4 ++++ .../index/query/MatchPhrasePrefixQueryBuilderTests.java | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/core/src/main/java/org/elasticsearch/index/query/MatchPhrasePrefixQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/MatchPhrasePrefixQueryBuilder.java index ecce9e66b10..ef8f860a285 100644 --- a/core/src/main/java/org/elasticsearch/index/query/MatchPhrasePrefixQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/MatchPhrasePrefixQueryBuilder.java @@ -238,6 +238,10 @@ public class MatchPhrasePrefixQueryBuilder extends AbstractQueryBuilder parseQuery(json)); assertEquals("[match_phrase_prefix] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage()); + + String shortJson = "{\n" + + " \"match_phrase_prefix\" : {\n" + + " \"message1\" : \"this is a test\",\n" + + " \"message2\" : \"this is a test\"\n" + + " }\n" + + "}"; + e = expectThrows(ParsingException.class, () -> parseQuery(shortJson)); + assertEquals("[match_phrase_prefix] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage()); } } From d5316b2783b68305b342565fec531cd15e5772c1 Mon Sep 17 00:00:00 2001 From: javanna Date: Mon, 8 Aug 2016 19:42:01 +0200 Subject: [PATCH 04/12] Throw parsing error if match phrase query contains multiple fields in its short version --- .../index/query/MatchPhraseQueryBuilder.java | 4 ++++ .../index/query/MatchPhraseQueryBuilderTests.java | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/core/src/main/java/org/elasticsearch/index/query/MatchPhraseQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/MatchPhraseQueryBuilder.java index c9667c98778..9f081d95ec2 100644 --- a/core/src/main/java/org/elasticsearch/index/query/MatchPhraseQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/MatchPhraseQueryBuilder.java @@ -207,6 +207,10 @@ public class MatchPhraseQueryBuilder extends AbstractQueryBuilder parseQuery(json)); assertEquals("[match_phrase] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage()); + + String shortJson = "{\n" + + " \"match_phrase\" : {\n" + + " \"message1\" : \"this is a test\",\n" + + " \"message2\" : \"this is a test\"\n" + + " }\n" + + "}"; + e = expectThrows(ParsingException.class, () -> parseQuery(shortJson)); + assertEquals("[match_phrase] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage()); } } From 040f9c6be692d72a51791bc89e2f70e06dec4722 Mon Sep 17 00:00:00 2001 From: javanna Date: Mon, 8 Aug 2016 19:42:14 +0200 Subject: [PATCH 05/12] Throw parsing error if match query contains multiple fields in its short version --- .../org/elasticsearch/index/query/MatchQueryBuilder.java | 4 ++++ .../index/query/MatchQueryBuilderTests.java | 9 +++++++++ 2 files changed, 13 insertions(+) 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 64d025adf71..57bbdac1af8 100644 --- a/core/src/main/java/org/elasticsearch/index/query/MatchQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/MatchQueryBuilder.java @@ -603,6 +603,10 @@ public class MatchQueryBuilder extends AbstractQueryBuilder { } } } else { + if (fieldName != null) { + throw new ParsingException(parser.getTokenLocation(), "[match] query doesn't support multiple fields, found [" + + fieldName + "] and [" + parser.currentName() + "]"); + } fieldName = parser.currentName(); value = parser.objectText(); } diff --git a/core/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java index a5cb2fc61c7..ea6fa65dddb 100644 --- a/core/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java @@ -421,5 +421,14 @@ public class MatchQueryBuilderTests extends AbstractQueryTestCase parseQuery(json)); assertEquals("[match] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage()); + + String shortJson = "{\n" + + " \"match\" : {\n" + + " \"message1\" : \"this is a test\",\n" + + " \"message2\" : \"this is a test\"\n" + + " }\n" + + "}"; + e = expectThrows(ParsingException.class, () -> parseQuery(shortJson)); + assertEquals("[match] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage()); } } From 8f485b361416fff6653fdd55f4c255ac9843d0c5 Mon Sep 17 00:00:00 2001 From: javanna Date: Mon, 8 Aug 2016 19:42:26 +0200 Subject: [PATCH 06/12] Throw parsing error if prefix query contains multiple fields in its short version --- .../elasticsearch/index/query/PrefixQueryBuilder.java | 4 ++++ .../index/query/PrefixQueryBuilderTests.java | 10 ++++++++++ 2 files changed, 14 insertions(+) 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 2ebd0dfc6b1..ff3b0af9591 100644 --- a/core/src/main/java/org/elasticsearch/index/query/PrefixQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/PrefixQueryBuilder.java @@ -158,6 +158,10 @@ public class PrefixQueryBuilder extends AbstractQueryBuilder } } } else { + if (fieldName != null) { + throw new ParsingException(parser.getTokenLocation(), "[prefix] query doesn't support multiple fields, found [" + + fieldName + "] and [" + parser.currentName() + "]"); + } fieldName = currentFieldName; value = parser.textOrNull(); } diff --git a/core/src/test/java/org/elasticsearch/index/query/PrefixQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/PrefixQueryBuilderTests.java index 87d3bb2ae83..0c4c59d1100 100644 --- a/core/src/test/java/org/elasticsearch/index/query/PrefixQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/PrefixQueryBuilderTests.java @@ -127,5 +127,15 @@ public class PrefixQueryBuilderTests extends AbstractQueryTestCase parseQuery(json)); assertEquals("[prefix] query doesn't support multiple fields, found [user1] and [user2]", e.getMessage()); + + String shortJson = + "{\n" + + " \"prefix\": {\n" + + " \"user1\": \"ki\",\n" + + " \"user2\": \"ki\"\n" + + " }\n" + + "}"; + e = expectThrows(ParsingException.class, () -> parseQuery(shortJson)); + assertEquals("[prefix] query doesn't support multiple fields, found [user1] and [user2]", e.getMessage()); } } From 796bc7416323e298956fbda4a99b48f46a1709d4 Mon Sep 17 00:00:00 2001 From: javanna Date: Mon, 8 Aug 2016 19:42:37 +0200 Subject: [PATCH 07/12] Throw parsing error if regexp query contains multiple fields in its short version --- .../elasticsearch/index/query/RegexpQueryBuilder.java | 4 ++++ .../index/query/RegexpQueryBuilderTests.java | 10 ++++++++++ 2 files changed, 14 insertions(+) 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 7be77795e82..c0d8be286b4 100644 --- a/core/src/main/java/org/elasticsearch/index/query/RegexpQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/RegexpQueryBuilder.java @@ -229,6 +229,10 @@ public class RegexpQueryBuilder extends AbstractQueryBuilder if (parseContext.getParseFieldMatcher().match(currentFieldName, NAME_FIELD)) { queryName = parser.text(); } else { + if (fieldName != null) { + throw new ParsingException(parser.getTokenLocation(), "[regexp] query doesn't support multiple fields, found [" + + fieldName + "] and [" + parser.currentName() + "]"); + } fieldName = currentFieldName; value = parser.textOrNull(); } diff --git a/core/src/test/java/org/elasticsearch/index/query/RegexpQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/RegexpQueryBuilderTests.java index 41cfc2c63ff..7daed416a05 100644 --- a/core/src/test/java/org/elasticsearch/index/query/RegexpQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/RegexpQueryBuilderTests.java @@ -135,5 +135,15 @@ public class RegexpQueryBuilderTests extends AbstractQueryTestCase parseQuery(json)); assertEquals("[regexp] query doesn't support multiple fields, found [user1] and [user2]", e.getMessage()); + + String shortJson = + "{\n" + + " \"regexp\": {\n" + + " \"user1\": \"k.*y\",\n" + + " \"user2\": \"k.*y\"\n" + + " }\n" + + "}"; + e = expectThrows(ParsingException.class, () -> parseQuery(shortJson)); + assertEquals("[regexp] query doesn't support multiple fields, found [user1] and [user2]", e.getMessage()); } } From 27a69836460735ba69ba2eb412520c350b71c78f Mon Sep 17 00:00:00 2001 From: javanna Date: Mon, 8 Aug 2016 19:42:48 +0200 Subject: [PATCH 08/12] Throw parsing error if wildcard query contains multiple fields in its short version --- .../index/query/WildcardQueryBuilder.java | 4 ++++ .../index/query/WildcardQueryBuilderTests.java | 10 ++++++++++ 2 files changed, 14 insertions(+) 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 105acf19a13..a7db8314514 100644 --- a/core/src/main/java/org/elasticsearch/index/query/WildcardQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/WildcardQueryBuilder.java @@ -174,6 +174,10 @@ public class WildcardQueryBuilder extends AbstractQueryBuilder parseQuery(json)); assertEquals("[wildcard] query doesn't support multiple fields, found [user1] and [user2]", e.getMessage()); + + String shortJson = + "{\n" + + " \"wildcard\": {\n" + + " \"user1\": \"ki*y\",\n" + + " \"user2\": \"ki*y\"\n" + + " }\n" + + "}"; + e = expectThrows(ParsingException.class, () -> parseQuery(shortJson)); + assertEquals("[wildcard] query doesn't support multiple fields, found [user1] and [user2]", e.getMessage()); } } From bbf40ca0cf56b8475d526dac44e729e9eaf38a38 Mon Sep 17 00:00:00 2001 From: javanna Date: Tue, 9 Aug 2016 09:50:12 +0200 Subject: [PATCH 09/12] [TEST] test that term query short syntax throws error when multiple fields are provided --- .../elasticsearch/index/query/TermQueryBuilderTests.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/core/src/test/java/org/elasticsearch/index/query/TermQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/TermQueryBuilderTests.java index 276768183b0..14e5054c5e1 100644 --- a/core/src/test/java/org/elasticsearch/index/query/TermQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/TermQueryBuilderTests.java @@ -155,5 +155,14 @@ public class TermQueryBuilderTests extends AbstractTermQueryTestCase parseQuery(json)); assertEquals("[term] query does not support different field names, use [bool] query instead", e.getMessage()); + + String shortJson = "{\n" + + " \"term\" : {\n" + + " \"message1\" : \"this\",\n" + + " \"message2\" : \"this\"\n" + + " }\n" + + "}"; + e = expectThrows(ParsingException.class, () -> parseQuery(shortJson)); + assertEquals("[term] query does not support different field names, use [bool] query instead", e.getMessage()); } } From d4db98782514ff0cefdf45e021b55ad9bea59d33 Mon Sep 17 00:00:00 2001 From: javanna Date: Tue, 9 Aug 2016 09:52:28 +0200 Subject: [PATCH 10/12] Add common method that throws exception whenever multiple fields are provided in a query that support one field only This makes sure that error messages are unified, and makes us save a few lines of code too. --- .../index/query/AbstractQueryBuilder.java | 10 ++++++++++ .../index/query/CommonTermsQueryBuilder.java | 10 ++-------- .../elasticsearch/index/query/FuzzyQueryBuilder.java | 10 ++-------- .../index/query/GeoDistanceQueryBuilder.java | 5 +---- .../index/query/MatchPhrasePrefixQueryBuilder.java | 10 ++-------- .../index/query/MatchPhraseQueryBuilder.java | 10 ++-------- .../elasticsearch/index/query/MatchQueryBuilder.java | 10 ++-------- .../elasticsearch/index/query/PrefixQueryBuilder.java | 10 ++-------- .../elasticsearch/index/query/RangeQueryBuilder.java | 5 +---- .../elasticsearch/index/query/RegexpQueryBuilder.java | 10 ++-------- .../index/query/WildcardQueryBuilder.java | 10 ++-------- 11 files changed, 28 insertions(+), 72 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 0dff9a98863..3ed3b80fb84 100644 --- a/core/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java @@ -26,10 +26,12 @@ import org.apache.lucene.search.spans.SpanQuery; import org.apache.lucene.util.BytesRef; import org.elasticsearch.action.support.ToXContentToBytes; import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.lucene.BytesRefs; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentLocation; import org.elasticsearch.common.xcontent.XContentType; import java.io.IOException; @@ -290,4 +292,12 @@ public abstract class AbstractQueryBuilder> } return value; } + + protected static void throwParsingExceptionOnMultipleFields(String queryName, XContentLocation contentLocation, + String processedFieldName, String currentFieldName) { + if (processedFieldName != null) { + throw new ParsingException(contentLocation, "[" + queryName + "] query doesn't support multiple fields, found [" + + processedFieldName + "] and [" + 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 6d4715e912c..21328ff8fc4 100644 --- a/core/src/main/java/org/elasticsearch/index/query/CommonTermsQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/CommonTermsQueryBuilder.java @@ -285,10 +285,7 @@ public class CommonTermsQueryBuilder extends AbstractQueryBuilder i } else if (parseContext.isDeprecatedSetting(currentFieldName)) { // skip } else if (token == XContentParser.Token.START_OBJECT) { - if (fieldName != null) { - throw new ParsingException(parser.getTokenLocation(), "[fuzzy] query doesn't support multiple fields, found [" - + fieldName + "] and [" + currentFieldName + "]"); - } + throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName); fieldName = currentFieldName; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { @@ -309,10 +306,7 @@ public class FuzzyQueryBuilder extends AbstractQueryBuilder i } } } else { - if (fieldName != null) { - throw new ParsingException(parser.getTokenLocation(), "[fuzzy] query doesn't support multiple fields, found [" - + fieldName + "] and [" + parser.currentName() + "]"); - } + throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, parser.currentName()); fieldName = parser.currentName(); value = parser.objectBytes(); } diff --git a/core/src/main/java/org/elasticsearch/index/query/GeoDistanceQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/GeoDistanceQueryBuilder.java index 1233df11714..fc92a490952 100644 --- a/core/src/main/java/org/elasticsearch/index/query/GeoDistanceQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/GeoDistanceQueryBuilder.java @@ -359,10 +359,7 @@ public class GeoDistanceQueryBuilder extends AbstractQueryBuilder field : { lat : 30, lon : 12 } String currentName = parser.currentName(); fieldName = currentFieldName; diff --git a/core/src/main/java/org/elasticsearch/index/query/MatchPhrasePrefixQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/MatchPhrasePrefixQueryBuilder.java index ef8f860a285..bff28d0f5be 100644 --- a/core/src/main/java/org/elasticsearch/index/query/MatchPhrasePrefixQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/MatchPhrasePrefixQueryBuilder.java @@ -207,10 +207,7 @@ public class MatchPhrasePrefixQueryBuilder extends AbstractQueryBuilder { } else if (parseContext.isDeprecatedSetting(currentFieldName)) { // skip } else if (token == XContentParser.Token.START_OBJECT) { - if (fieldName != null) { - throw new ParsingException(parser.getTokenLocation(), "[match] query doesn't support multiple fields, found [" - + fieldName + "] and [" + currentFieldName + "]"); - } + throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName); fieldName = currentFieldName; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { @@ -603,10 +600,7 @@ public class MatchQueryBuilder extends AbstractQueryBuilder { } } } else { - if (fieldName != null) { - throw new ParsingException(parser.getTokenLocation(), "[match] query doesn't support multiple fields, found [" - + fieldName + "] and [" + parser.currentName() + "]"); - } + throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, parser.currentName()); fieldName = parser.currentName(); value = parser.objectText(); } 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 ff3b0af9591..13cd9eca9d3 100644 --- a/core/src/main/java/org/elasticsearch/index/query/PrefixQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/PrefixQueryBuilder.java @@ -134,10 +134,7 @@ public class PrefixQueryBuilder extends AbstractQueryBuilder } else if (parseContext.isDeprecatedSetting(currentFieldName)) { // skip } else if (token == XContentParser.Token.START_OBJECT) { - if (fieldName != null) { - throw new ParsingException(parser.getTokenLocation(), "[prefix] query doesn't support multiple fields, found [" - + fieldName + "] and [" + currentFieldName + "]"); - } + throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName); fieldName = currentFieldName; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { @@ -158,10 +155,7 @@ public class PrefixQueryBuilder extends AbstractQueryBuilder } } } else { - if (fieldName != null) { - throw new ParsingException(parser.getTokenLocation(), "[prefix] query doesn't support multiple fields, found [" - + fieldName + "] and [" + parser.currentName() + "]"); - } + throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, parser.currentName()); fieldName = currentFieldName; value = parser.textOrNull(); } 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 8e1326b1988..8f52666fa8e 100644 --- a/core/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java @@ -318,10 +318,7 @@ public class RangeQueryBuilder extends AbstractQueryBuilder i } else if (parseContext.isDeprecatedSetting(currentFieldName)) { // skip } else if (token == XContentParser.Token.START_OBJECT) { - if (fieldName != null) { - throw new ParsingException(parser.getTokenLocation(), "[range] query doesn't support multiple fields, found [" - + fieldName + "] and [" + currentFieldName + "]"); - } + throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName); fieldName = currentFieldName; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { 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 c0d8be286b4..d8481d1529c 100644 --- a/core/src/main/java/org/elasticsearch/index/query/RegexpQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/RegexpQueryBuilder.java @@ -195,10 +195,7 @@ public class RegexpQueryBuilder extends AbstractQueryBuilder } else if (parseContext.isDeprecatedSetting(currentFieldName)) { // skip } else if (token == XContentParser.Token.START_OBJECT) { - if (fieldName != null) { - throw new ParsingException(parser.getTokenLocation(), "[regexp] query doesn't support multiple fields, found [" - + fieldName + "] and [" + currentFieldName + "]"); - } + throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName); fieldName = currentFieldName; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { @@ -229,10 +226,7 @@ public class RegexpQueryBuilder extends AbstractQueryBuilder if (parseContext.getParseFieldMatcher().match(currentFieldName, NAME_FIELD)) { queryName = parser.text(); } else { - if (fieldName != null) { - throw new ParsingException(parser.getTokenLocation(), "[regexp] query doesn't support multiple fields, found [" - + fieldName + "] and [" + parser.currentName() + "]"); - } + throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, parser.currentName()); fieldName = currentFieldName; value = parser.textOrNull(); } 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 a7db8314514..0cef251be07 100644 --- a/core/src/main/java/org/elasticsearch/index/query/WildcardQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/WildcardQueryBuilder.java @@ -148,10 +148,7 @@ public class WildcardQueryBuilder extends AbstractQueryBuilder Date: Tue, 9 Aug 2016 09:53:03 +0200 Subject: [PATCH 11/12] Throw parsing error if span term query contains multiple fields in its short version --- .../elasticsearch/index/query/SpanTermQueryBuilder.java | 6 ++---- .../index/query/SpanTermQueryBuilderTests.java | 9 +++++++++ 2 files changed, 11 insertions(+), 4 deletions(-) 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 1a77c73b0cc..b57206c33b5 100644 --- a/core/src/main/java/org/elasticsearch/index/query/SpanTermQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/SpanTermQueryBuilder.java @@ -104,10 +104,7 @@ public class SpanTermQueryBuilder extends BaseTermQueryBuilder parseQuery(json)); assertEquals("[span_term] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage()); + + String shortJson = "{\n" + + " \"span_term\" : {\n" + + " \"message1\" : \"this\",\n" + + " \"message2\" : \"this\"\n" + + " }\n" + + "}"; + e = expectThrows(ParsingException.class, () -> parseQuery(shortJson)); + assertEquals("[span_term] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage()); } } From f9a40344b271f2487a22668e4fd618ae4ad5f0fd Mon Sep 17 00:00:00 2001 From: javanna Date: Tue, 9 Aug 2016 10:01:56 +0200 Subject: [PATCH 12/12] Modify term query error when multiple fields are provided to comply with all other queries --- .../elasticsearch/index/query/TermQueryBuilder.java | 11 ++--------- .../index/query/TermQueryBuilderTests.java | 4 ++-- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/index/query/TermQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/TermQueryBuilder.java index b3ce8bab9bd..92ec60f402f 100644 --- a/core/src/main/java/org/elasticsearch/index/query/TermQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/TermQueryBuilder.java @@ -98,11 +98,7 @@ public class TermQueryBuilder extends BaseTermQueryBuilder { } else if (parseContext.isDeprecatedSetting(currentFieldName)) { // skip } else if (token == XContentParser.Token.START_OBJECT) { - // also support a format of "term" : {"field_name" : { ... }} - if (fieldName != null) { - throw new ParsingException(parser.getTokenLocation(), - "[term] query does not support different field names, use [bool] query instead"); - } + throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName); fieldName = currentFieldName; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { @@ -123,10 +119,7 @@ public class TermQueryBuilder extends BaseTermQueryBuilder { } } } else if (token.isValue()) { - if (fieldName != null) { - throw new ParsingException(parser.getTokenLocation(), - "[term] query does not support different field names, use [bool] query instead"); - } + throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, parser.currentName()); fieldName = currentFieldName; value = parser.objectBytes(); } else if (token == XContentParser.Token.START_ARRAY) { diff --git a/core/src/test/java/org/elasticsearch/index/query/TermQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/TermQueryBuilderTests.java index 14e5054c5e1..0e6542e4e3d 100644 --- a/core/src/test/java/org/elasticsearch/index/query/TermQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/TermQueryBuilderTests.java @@ -154,7 +154,7 @@ public class TermQueryBuilderTests extends AbstractTermQueryTestCase parseQuery(json)); - assertEquals("[term] query does not support different field names, use [bool] query instead", e.getMessage()); + assertEquals("[term] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage()); String shortJson = "{\n" + " \"term\" : {\n" + @@ -163,6 +163,6 @@ public class TermQueryBuilderTests extends AbstractTermQueryTestCase parseQuery(shortJson)); - assertEquals("[term] query does not support different field names, use [bool] query instead", e.getMessage()); + assertEquals("[term] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage()); } }