Tests: Use random analyzer only on string fields in Match/MultiMatchBuilderTests

Currently we can run into test errors by accidently using e.g. a "simple"
analyzer on a numeric field which might lead to number parsing errors. While
these errors are correct, we should avoid these combinations in our regular
tests.
This commit is contained in:
Christoph Büscher 2017-04-12 11:07:40 +02:00
parent 7cebc0fc93
commit 1847bbac4d
2 changed files with 24 additions and 15 deletions

View File

@ -80,13 +80,8 @@ public class MatchQueryBuilderTests extends AbstractQueryTestCase<MatchQueryBuil
MatchQueryBuilder matchQuery = new MatchQueryBuilder(fieldName, value);
matchQuery.operator(randomFrom(Operator.values()));
if (randomBoolean()) {
if (fieldName.equals(DATE_FIELD_NAME)) {
// tokenized dates would trigger parse errors
matchQuery.analyzer(randomFrom("keyword", "whitespace"));
} else {
matchQuery.analyzer(randomFrom("simple", "keyword", "whitespace"));
}
if (randomBoolean() && fieldName.equals(STRING_FIELD_NAME)) {
matchQuery.analyzer(randomFrom("simple", "keyword", "whitespace"));
}
if (fieldName.equals(STRING_FIELD_NAME) && randomBoolean()) {
@ -424,6 +419,15 @@ public class MatchQueryBuilderTests extends AbstractQueryTestCase<MatchQueryBuil
expectThrows(IllegalStateException.class, () -> parseQuery(json2));
}
public void testExceptionUsingAnalyzerOnNumericField() {
assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
QueryShardContext shardContext = createShardContext();
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder(DOUBLE_FIELD_NAME, 6.075210893508043E-4);
matchQueryBuilder.analyzer("simple");
NumberFormatException e = expectThrows(NumberFormatException.class, () -> matchQueryBuilder.toQuery(shardContext));
assertEquals("For input string: \"e\"", e.getMessage());
}
@Override
protected void initializeAdditionalMappings(MapperService mapperService) throws IOException {
mapperService.merge("t_boost", new CompressedXContent(PutMappingRequest.buildFromSimplifiedDef("t_boost",

View File

@ -85,13 +85,8 @@ public class MultiMatchQueryBuilderTests extends AbstractQueryTestCase<MultiMatc
if (randomBoolean()) {
query.operator(randomFrom(Operator.values()));
}
if (randomBoolean()) {
if (fieldName.equals(DATE_FIELD_NAME)) {
// tokenized dates would trigger parse errors
query.analyzer("keyword");
} else {
query.analyzer(randomAnalyzer());
}
if (randomBoolean() && fieldName.equals(STRING_FIELD_NAME)) {
query.analyzer(randomAnalyzer());
}
if (randomBoolean()) {
query.slop(randomIntBetween(0, 5));
@ -276,7 +271,7 @@ public class MultiMatchQueryBuilderTests extends AbstractQueryTestCase<MultiMatc
}
}
public void testQueryParameterArrayException() throws IOException {
public void testQueryParameterArrayException() {
String json =
"{\n" +
" \"multi_match\" : {\n" +
@ -289,6 +284,16 @@ public class MultiMatchQueryBuilderTests extends AbstractQueryTestCase<MultiMatc
assertEquals("[multi_match] unknown token [START_ARRAY] after [query]", e.getMessage());
}
public void testExceptionUsingAnalyzerOnNumericField() {
assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
QueryShardContext shardContext = createShardContext();
MultiMatchQueryBuilder multiMatchQueryBuilder = new MultiMatchQueryBuilder(6.075210893508043E-4);
multiMatchQueryBuilder.field(DOUBLE_FIELD_NAME);
multiMatchQueryBuilder.analyzer("simple");
NumberFormatException e = expectThrows(NumberFormatException.class, () -> multiMatchQueryBuilder.toQuery(shardContext));
assertEquals("For input string: \"e\"", e.getMessage());
}
public void testFuzzinessOnNonStringField() throws Exception {
assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
MultiMatchQueryBuilder query = new MultiMatchQueryBuilder(42).field(INT_FIELD_NAME).field(BOOLEAN_FIELD_NAME);