From 54a7249895a93e7166334f4be5948dd0efdfce74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Wed, 12 Apr 2017 11:07:40 +0200 Subject: [PATCH] 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. --- .../index/query/MatchQueryBuilderTests.java | 18 +++++++++------- .../query/MultiMatchQueryBuilderTests.java | 21 ++++++++++++------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/core/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java index 781cb673039d1..668f2fdd221a1 100644 --- a/core/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java @@ -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()) { @@ -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", diff --git a/core/src/test/java/org/elasticsearch/index/query/MultiMatchQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/MultiMatchQueryBuilderTests.java index 3373ded3fdc8a..a2cd819fb17e1 100644 --- a/core/src/test/java/org/elasticsearch/index/query/MultiMatchQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/MultiMatchQueryBuilderTests.java @@ -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)); @@ -276,7 +271,7 @@ public void testFuzzinessNotAllowedTypes() throws IOException { } } - public void testQueryParameterArrayException() throws IOException { + public void testQueryParameterArrayException() { String json = "{\n" + " \"multi_match\" : {\n" + @@ -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);