Fix disabling `allow_leading_wildcard` (#62300) (#62318)

Disabling the `query_string` queries `allow_leading_wildcard` parameter didn't
work after a change probably introduced in #60959 because the various field types
`wildcardQuery` don't check the leading characters like
QueryParserBase#getWildcardQuery does. This PR adds the missing check also
before calling the field types wildcard generating method.

Closes #62267
This commit is contained in:
Christoph Büscher 2020-09-14 17:13:17 +02:00 committed by GitHub
parent 5358cee29c
commit e2eada2498
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View File

@ -681,7 +681,9 @@ public class QueryStringQueryParser extends XQueryParser {
setAnalyzer(forceAnalyzer);
return super.getWildcardQuery(currentFieldType.name(), termStr);
}
if (getAllowLeadingWildcard() == false && (termStr.startsWith("*") || termStr.startsWith("?"))) {
throw new ParseException("'*' or '?' not allowed as first character in WildcardQuery");
}
return currentFieldType.wildcardQuery(termStr, getMultiTermRewriteMethod(), context);
} catch (RuntimeException e) {
if (lenient) {

View File

@ -529,6 +529,20 @@ public class QueryStringQueryBuilderTests extends AbstractQueryTestCase<QueryStr
equalTo(new Term(KEYWORD_FIELD_NAME, "test")));
}
/**
* Test that dissalowing leading wildcards causes exception
*/
public void testAllowLeadingWildcard() throws Exception {
Query query = queryStringQuery("*test").field("mapped_string").allowLeadingWildcard(true).toQuery(createShardContext());
assertThat(query, instanceOf(WildcardQuery.class));
QueryShardException ex = expectThrows(
QueryShardException.class,
() -> queryStringQuery("*test").field("mapped_string").allowLeadingWildcard(false).toQuery(createShardContext())
);
assertEquals("Failed to parse query [*test]", ex.getMessage());
assertEquals("Cannot parse '*test': '*' or '?' not allowed as first character in WildcardQuery", ex.getCause().getMessage());
}
public void testToQueryDisMaxQuery() throws Exception {
Query query = queryStringQuery("test").field(TEXT_FIELD_NAME, 2.2f)
.field(KEYWORD_FIELD_NAME)