Skip to content

Commit

Permalink
Tests: Use random analyzer only on string fields in Match/MultiMatchB…
Browse files Browse the repository at this point in the history
…uilderTests

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.
  • Loading branch information
cbuescher committed Apr 12, 2017
1 parent 1f9ad16 commit 54a7249
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,8 @@ protected MatchQueryBuilder doCreateTestQueryBuilder() {
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()) {
Expand Down Expand Up @@ -448,6 +443,15 @@ public void testParseFailsWithTermsArray() throws Exception {
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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,8 @@ protected MultiMatchQueryBuilder doCreateTestQueryBuilder() {
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));
Expand Down Expand Up @@ -276,7 +271,7 @@ public void testFuzzinessNotAllowedTypes() throws IOException {
}
}

public void testQueryParameterArrayException() throws IOException {
public void testQueryParameterArrayException() {
String json =
"{\n" +
" \"multi_match\" : {\n" +
Expand All @@ -289,6 +284,16 @@ public void testQueryParameterArrayException() throws IOException {
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);
Expand Down

0 comments on commit 54a7249

Please sign in to comment.