Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search - make term/prefix/wildcard/regex query parsing more lenient #63926

Merged
merged 2 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/reference/query-dsl/prefix-query.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ information, see the <<query-dsl-multi-term-rewrite, `rewrite` parameter>>.

`case_insensitive`::
(Optional, boolean) allows ASCII case insensitive matching of the
value with the indexed field values when set to true. Setting to false is disallowed.
value with the indexed field values when set to true. Default is false which means
the case sensitivity of matching depends on the underlying field's mapping.

[[prefix-query-notes]]
==== Notes
Expand Down
3 changes: 2 additions & 1 deletion docs/reference/query-dsl/regexp-query.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ expression syntax>>.

`case_insensitive`::
(Optional, boolean) allows case insensitive matching of the regular expression
value with the indexed field values when set to true. Setting to false is disallowed.
value with the indexed field values when set to true. Default is false which means
the case sensitivity of matching depends on the underlying field's mapping.

`max_determinized_states`::
+
Expand Down
3 changes: 2 additions & 1 deletion docs/reference/query-dsl/term-query.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ increases the relevance score.

`case_insensitive`::
(Optional, boolean) allows ASCII case insensitive matching of the
value with the indexed field values when set to true. Setting to false is disallowed.
value with the indexed field values when set to true. Default is false which means
the case sensitivity of matching depends on the underlying field's mapping

[[term-query-notes]]
==== Notes
Expand Down
3 changes: 2 additions & 1 deletion docs/reference/query-dsl/wildcard-query.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ increases the relevance score.

`case_insensitive`::
(Optional, boolean) allows case insensitive matching of the
pattern with the indexed field values when set to true. Setting to false is disallowed.
pattern with the indexed field values when set to true. Default is false which means
the case sensitivity of matching depends on the underlying field's mapping.

[[wildcard-query-notes]]
==== Notes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,6 @@ public String value() {
}

public PrefixQueryBuilder caseInsensitive(boolean caseInsensitive) {
if (caseInsensitive == false) {
throw new IllegalArgumentException("The case insensitive setting cannot be set to false.");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the plan here? To allow clients to set caseInsensitive to false?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. Makes some of the clients easier to write

this.caseInsensitive = caseInsensitive;
return this;
}
Expand Down Expand Up @@ -175,10 +172,6 @@ public static PrefixQueryBuilder fromXContent(XContentParser parser) throws IOEx
rewrite = parser.textOrNull();
} else if (CASE_INSENSITIVE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
caseInsensitive = parser.booleanValue();
if (caseInsensitive == false) {
throw new ParsingException(parser.getTokenLocation(),
"[prefix] query does not support [" + currentFieldName + "] = false");
}
} else {
throw new ParsingException(parser.getTokenLocation(),
"[prefix] query does not support [" + currentFieldName + "]");
Expand All @@ -196,9 +189,7 @@ public static PrefixQueryBuilder fromXContent(XContentParser parser) throws IOEx
.rewrite(rewrite)
.boost(boost)
.queryName(queryName);
if (caseInsensitive) {
result.caseInsensitive(caseInsensitive);
}
result.caseInsensitive(caseInsensitive);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,6 @@ public int flags() {
}

public RegexpQueryBuilder caseInsensitive(boolean caseInsensitive) {
if (caseInsensitive == false) {
throw new IllegalArgumentException("The case insensitive setting cannot be set to false.");
}
this.caseInsensitive = caseInsensitive;
return this;
}
Expand Down Expand Up @@ -240,10 +237,6 @@ public static RegexpQueryBuilder fromXContent(XContentParser parser) throws IOEx
flagsValue = parser.intValue();
} else if (CASE_INSENSITIVE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
caseInsensitive = parser.booleanValue();
if (caseInsensitive == false) {
throw new ParsingException(parser.getTokenLocation(),
"[regexp] query does not support [" + currentFieldName + "] = false");
}
} else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
queryName = parser.text();
} else {
Expand All @@ -265,9 +258,7 @@ public static RegexpQueryBuilder fromXContent(XContentParser parser) throws IOEx
.rewrite(rewrite)
.boost(boost)
.queryName(queryName);
if (caseInsensitive) {
result.caseInsensitive(caseInsensitive);
}
result.caseInsensitive(caseInsensitive);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,6 @@ public TermQueryBuilder(String fieldName, Object value) {
}

public TermQueryBuilder caseInsensitive(boolean caseInsensitive) {
if (caseInsensitive == false) {
throw new IllegalArgumentException("The case insensitive setting cannot be set to false.");
}
this.caseInsensitive = caseInsensitive;
return this;
}
Expand Down Expand Up @@ -143,10 +140,6 @@ public static TermQueryBuilder fromXContent(XContentParser parser) throws IOExce
boost = parser.floatValue();
} else if (CASE_INSENSITIVE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
caseInsensitive = parser.booleanValue();
if (caseInsensitive == false) {
throw new ParsingException(parser.getTokenLocation(),
"[term] query does not support [" + currentFieldName + "] = false");
}
} else {
throw new ParsingException(parser.getTokenLocation(),
"[term] query does not support [" + currentFieldName + "]");
Expand All @@ -167,9 +160,7 @@ public static TermQueryBuilder fromXContent(XContentParser parser) throws IOExce
if (queryName != null) {
termQuery.queryName(queryName);
}
if (caseInsensitive) {
termQuery.caseInsensitive(caseInsensitive);
}
termQuery.caseInsensitive(caseInsensitive);
return termQuery;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,6 @@ public String rewrite() {
}

public WildcardQueryBuilder caseInsensitive(boolean caseInsensitive) {
if (caseInsensitive == false) {
throw new IllegalArgumentException("The case insensitive setting cannot be set to false.");
}
this.caseInsensitive = caseInsensitive;
return this;
}
Expand Down Expand Up @@ -189,10 +186,6 @@ public static WildcardQueryBuilder fromXContent(XContentParser parser) throws IO
rewrite = parser.textOrNull();
} else if (CASE_INSENSITIVE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
caseInsensitive = parser.booleanValue();
if (caseInsensitive == false) {
throw new ParsingException(parser.getTokenLocation(),
"[prefix] query does not support [" + currentFieldName + "] = false");
}
} else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
queryName = parser.text();
} else {
Expand All @@ -212,9 +205,7 @@ public static WildcardQueryBuilder fromXContent(XContentParser parser) throws IO
.rewrite(rewrite)
.boost(boost)
.queryName(queryName);
if (caseInsensitive) {
result.caseInsensitive(caseInsensitive);
}
result.caseInsensitive(caseInsensitive);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,4 @@ public void testParseFailsWithMultipleFields() throws IOException {
e = expectThrows(ParsingException.class, () -> parseQuery(shortJson));
assertEquals("[regexp] query doesn't support multiple fields, found [user1] and [user2]", e.getMessage());
}

public void testParseFailsWithCaseSensitive() throws IOException {
String json =
"{\n" +
" \"regexp\": {\n" +
" \"user1\": {\n" +
" \"value\": \"k.*y\",\n" +
" \"case_insensitive\": false\n" +
" },\n" +
" }\n" +
"}";
ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(json));
assertEquals("[regexp] query does not support [case_insensitive] = false", e.getMessage());
}
}