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

Remove errors when case_insensitive flag set to false

Closes #63893
This commit is contained in:
markharwood 2020-10-21 13:33:19 +01:00 committed by GitHub
parent ddd897f747
commit b933bd9f45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 25 additions and 73 deletions

View File

@ -43,7 +43,8 @@ information, see the <<query-dsl-multi-term-rewrite, `rewrite` parameter>>.
`case_insensitive`:: `case_insensitive`::
(Optional, boolean) allows ASCII case insensitive matching of the (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]] [[prefix-query-notes]]
==== Notes ==== Notes

View File

@ -70,7 +70,8 @@ expression syntax>>.
`case_insensitive`:: `case_insensitive`::
(Optional, boolean) allows case insensitive matching of the regular expression (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`:: `max_determinized_states`::
+ +

View File

@ -64,7 +64,8 @@ increases the relevance score.
`case_insensitive`:: `case_insensitive`::
(Optional, boolean) allows ASCII case insensitive matching of the (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]] [[term-query-notes]]
==== Notes ==== Notes

View File

@ -71,7 +71,8 @@ increases the relevance score.
`case_insensitive`:: `case_insensitive`::
(Optional, boolean) allows case insensitive matching of the (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]] [[wildcard-query-notes]]
==== Notes ==== Notes

View File

@ -109,9 +109,6 @@ public class PrefixQueryBuilder extends AbstractQueryBuilder<PrefixQueryBuilder>
} }
public PrefixQueryBuilder caseInsensitive(boolean caseInsensitive) { public PrefixQueryBuilder caseInsensitive(boolean caseInsensitive) {
if (caseInsensitive == false) {
throw new IllegalArgumentException("The case insensitive setting cannot be set to false.");
}
this.caseInsensitive = caseInsensitive; this.caseInsensitive = caseInsensitive;
return this; return this;
} }
@ -175,10 +172,6 @@ public class PrefixQueryBuilder extends AbstractQueryBuilder<PrefixQueryBuilder>
rewrite = parser.textOrNull(); rewrite = parser.textOrNull();
} else if (CASE_INSENSITIVE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { } else if (CASE_INSENSITIVE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
caseInsensitive = parser.booleanValue(); caseInsensitive = parser.booleanValue();
if (caseInsensitive == false) {
throw new ParsingException(parser.getTokenLocation(),
"[prefix] query does not support [" + currentFieldName + "] = false");
}
} else { } else {
throw new ParsingException(parser.getTokenLocation(), throw new ParsingException(parser.getTokenLocation(),
"[prefix] query does not support [" + currentFieldName + "]"); "[prefix] query does not support [" + currentFieldName + "]");
@ -192,14 +185,11 @@ public class PrefixQueryBuilder extends AbstractQueryBuilder<PrefixQueryBuilder>
} }
} }
PrefixQueryBuilder result = new PrefixQueryBuilder(fieldName, value) return new PrefixQueryBuilder(fieldName, value)
.rewrite(rewrite) .rewrite(rewrite)
.boost(boost) .boost(boost)
.queryName(queryName); .queryName(queryName)
if (caseInsensitive) { .caseInsensitive(caseInsensitive);
result.caseInsensitive(caseInsensitive);
}
return result;
} }
@Override @Override

View File

@ -154,9 +154,6 @@ public class RegexpQueryBuilder extends AbstractQueryBuilder<RegexpQueryBuilder>
} }
public RegexpQueryBuilder caseInsensitive(boolean caseInsensitive) { public RegexpQueryBuilder caseInsensitive(boolean caseInsensitive) {
if (caseInsensitive == false) {
throw new IllegalArgumentException("The case insensitive setting cannot be set to false.");
}
this.caseInsensitive = caseInsensitive; this.caseInsensitive = caseInsensitive;
return this; return this;
} }
@ -240,10 +237,6 @@ public class RegexpQueryBuilder extends AbstractQueryBuilder<RegexpQueryBuilder>
flagsValue = parser.intValue(); flagsValue = parser.intValue();
} else if (CASE_INSENSITIVE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { } else if (CASE_INSENSITIVE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
caseInsensitive = parser.booleanValue(); 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())) { } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
queryName = parser.text(); queryName = parser.text();
} else { } else {
@ -265,9 +258,7 @@ public class RegexpQueryBuilder extends AbstractQueryBuilder<RegexpQueryBuilder>
.rewrite(rewrite) .rewrite(rewrite)
.boost(boost) .boost(boost)
.queryName(queryName); .queryName(queryName);
if (caseInsensitive) {
result.caseInsensitive(caseInsensitive); result.caseInsensitive(caseInsensitive);
}
return result; return result;
} }

View File

@ -87,9 +87,6 @@ public class TermQueryBuilder extends BaseTermQueryBuilder<TermQueryBuilder> {
} }
public TermQueryBuilder caseInsensitive(boolean caseInsensitive) { public TermQueryBuilder caseInsensitive(boolean caseInsensitive) {
if (caseInsensitive == false) {
throw new IllegalArgumentException("The case insensitive setting cannot be set to false.");
}
this.caseInsensitive = caseInsensitive; this.caseInsensitive = caseInsensitive;
return this; return this;
} }
@ -144,10 +141,6 @@ public class TermQueryBuilder extends BaseTermQueryBuilder<TermQueryBuilder> {
boost = parser.floatValue(); boost = parser.floatValue();
} else if (CASE_INSENSITIVE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { } else if (CASE_INSENSITIVE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
caseInsensitive = parser.booleanValue(); caseInsensitive = parser.booleanValue();
if (caseInsensitive == false) {
throw new ParsingException(parser.getTokenLocation(),
"[term] query does not support [" + currentFieldName + "] = false");
}
} else { } else {
throw new ParsingException(parser.getTokenLocation(), throw new ParsingException(parser.getTokenLocation(),
"[term] query does not support [" + currentFieldName + "]"); "[term] query does not support [" + currentFieldName + "]");
@ -168,9 +161,7 @@ public class TermQueryBuilder extends BaseTermQueryBuilder<TermQueryBuilder> {
if (queryName != null) { if (queryName != null) {
termQuery.queryName(queryName); termQuery.queryName(queryName);
} }
if (caseInsensitive) {
termQuery.caseInsensitive(caseInsensitive); termQuery.caseInsensitive(caseInsensitive);
}
return termQuery; return termQuery;
} }

View File

@ -128,9 +128,6 @@ public class WildcardQueryBuilder extends AbstractQueryBuilder<WildcardQueryBuil
} }
public WildcardQueryBuilder caseInsensitive(boolean caseInsensitive) { public WildcardQueryBuilder caseInsensitive(boolean caseInsensitive) {
if (caseInsensitive == false) {
throw new IllegalArgumentException("The case insensitive setting cannot be set to false.");
}
this.caseInsensitive = caseInsensitive; this.caseInsensitive = caseInsensitive;
return this; return this;
} }
@ -189,10 +186,6 @@ public class WildcardQueryBuilder extends AbstractQueryBuilder<WildcardQueryBuil
rewrite = parser.textOrNull(); rewrite = parser.textOrNull();
} else if (CASE_INSENSITIVE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { } else if (CASE_INSENSITIVE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
caseInsensitive = parser.booleanValue(); 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())) { } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
queryName = parser.text(); queryName = parser.text();
} else { } else {
@ -208,14 +201,11 @@ public class WildcardQueryBuilder extends AbstractQueryBuilder<WildcardQueryBuil
} }
} }
WildcardQueryBuilder result = new WildcardQueryBuilder(fieldName, value) return new WildcardQueryBuilder(fieldName, value)
.rewrite(rewrite) .rewrite(rewrite)
.boost(boost) .boost(boost)
.queryName(queryName); .queryName(queryName)
if (caseInsensitive) { .caseInsensitive(caseInsensitive);
result.caseInsensitive(caseInsensitive);
}
return result;
} }
@Override @Override

View File

@ -151,18 +151,4 @@ public class RegexpQueryBuilderTests extends AbstractQueryTestCase<RegexpQueryBu
e = expectThrows(ParsingException.class, () -> parseQuery(shortJson)); e = expectThrows(ParsingException.class, () -> parseQuery(shortJson));
assertEquals("[regexp] query doesn't support multiple fields, found [user1] and [user2]", e.getMessage()); 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());
}
} }