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:
parent
5358cee29c
commit
e2eada2498
|
@ -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) {
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue