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`::
(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

View File

@ -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`::
+

View File

@ -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

View File

@ -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

View File

@ -107,18 +107,15 @@ public class PrefixQueryBuilder extends AbstractQueryBuilder<PrefixQueryBuilder>
public String value() {
return this.value;
}
public PrefixQueryBuilder caseInsensitive(boolean caseInsensitive) {
if (caseInsensitive == false) {
throw new IllegalArgumentException("The case insensitive setting cannot be set to false.");
}
this.caseInsensitive = caseInsensitive;
return this;
}
}
public boolean caseInsensitive() {
return this.caseInsensitive;
}
}
public PrefixQueryBuilder rewrite(String rewrite) {
this.rewrite = rewrite;
@ -175,10 +172,6 @@ public class PrefixQueryBuilder extends AbstractQueryBuilder<PrefixQueryBuilder>
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 + "]");
@ -192,21 +185,18 @@ public class PrefixQueryBuilder extends AbstractQueryBuilder<PrefixQueryBuilder>
}
}
PrefixQueryBuilder result = new PrefixQueryBuilder(fieldName, value)
return new PrefixQueryBuilder(fieldName, value)
.rewrite(rewrite)
.boost(boost)
.queryName(queryName);
if (caseInsensitive) {
result.caseInsensitive(caseInsensitive);
}
return result;
.queryName(queryName)
.caseInsensitive(caseInsensitive);
}
@Override
public String getWriteableName() {
return NAME;
}
@Override
protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws IOException {
QueryShardContext context = queryRewriteContext.convertToShardContext();

View File

@ -154,9 +154,6 @@ public class RegexpQueryBuilder extends AbstractQueryBuilder<RegexpQueryBuilder>
}
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;
}
@ -240,10 +237,6 @@ public class RegexpQueryBuilder extends AbstractQueryBuilder<RegexpQueryBuilder>
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 {
@ -265,9 +258,7 @@ public class RegexpQueryBuilder extends AbstractQueryBuilder<RegexpQueryBuilder>
.rewrite(rewrite)
.boost(boost)
.queryName(queryName);
if (caseInsensitive) {
result.caseInsensitive(caseInsensitive);
}
result.caseInsensitive(caseInsensitive);
return result;
}

View File

@ -87,9 +87,6 @@ public class TermQueryBuilder extends BaseTermQueryBuilder<TermQueryBuilder> {
}
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;
}
@ -144,10 +141,6 @@ public class TermQueryBuilder extends BaseTermQueryBuilder<TermQueryBuilder> {
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 + "]");
@ -168,9 +161,7 @@ public class TermQueryBuilder extends BaseTermQueryBuilder<TermQueryBuilder> {
if (queryName != null) {
termQuery.queryName(queryName);
}
if (caseInsensitive) {
termQuery.caseInsensitive(caseInsensitive);
}
termQuery.caseInsensitive(caseInsensitive);
return termQuery;
}

View File

@ -126,18 +126,15 @@ public class WildcardQueryBuilder extends AbstractQueryBuilder<WildcardQueryBuil
public String rewrite() {
return this.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;
}
}
public boolean caseInsensitive() {
return this.caseInsensitive;
}
}
@Override
public String getWriteableName() {
@ -189,10 +186,6 @@ public class WildcardQueryBuilder extends AbstractQueryBuilder<WildcardQueryBuil
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 {
@ -208,16 +201,13 @@ public class WildcardQueryBuilder extends AbstractQueryBuilder<WildcardQueryBuil
}
}
WildcardQueryBuilder result = new WildcardQueryBuilder(fieldName, value)
return new WildcardQueryBuilder(fieldName, value)
.rewrite(rewrite)
.boost(boost)
.queryName(queryName);
if (caseInsensitive) {
result.caseInsensitive(caseInsensitive);
}
return result;
}
.queryName(queryName)
.caseInsensitive(caseInsensitive);
}
@Override
protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws IOException {
QueryShardContext context = queryRewriteContext.convertToShardContext();

View File

@ -151,18 +151,4 @@ public class RegexpQueryBuilderTests extends AbstractQueryTestCase<RegexpQueryBu
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());
}
}