mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-17 10:25:15 +00:00
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:
parent
7cebc0fc93
commit
1847bbac4d
@ -80,13 +80,8 @@ public class MatchQueryBuilderTests extends AbstractQueryTestCase<MatchQueryBuil
|
|||||||
MatchQueryBuilder matchQuery = new MatchQueryBuilder(fieldName, value);
|
MatchQueryBuilder matchQuery = new MatchQueryBuilder(fieldName, value);
|
||||||
matchQuery.operator(randomFrom(Operator.values()));
|
matchQuery.operator(randomFrom(Operator.values()));
|
||||||
|
|
||||||
if (randomBoolean()) {
|
if (randomBoolean() && fieldName.equals(STRING_FIELD_NAME)) {
|
||||||
if (fieldName.equals(DATE_FIELD_NAME)) {
|
matchQuery.analyzer(randomFrom("simple", "keyword", "whitespace"));
|
||||||
// tokenized dates would trigger parse errors
|
|
||||||
matchQuery.analyzer(randomFrom("keyword", "whitespace"));
|
|
||||||
} else {
|
|
||||||
matchQuery.analyzer(randomFrom("simple", "keyword", "whitespace"));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fieldName.equals(STRING_FIELD_NAME) && randomBoolean()) {
|
if (fieldName.equals(STRING_FIELD_NAME) && randomBoolean()) {
|
||||||
@ -424,6 +419,15 @@ public class MatchQueryBuilderTests extends AbstractQueryTestCase<MatchQueryBuil
|
|||||||
expectThrows(IllegalStateException.class, () -> parseQuery(json2));
|
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
|
@Override
|
||||||
protected void initializeAdditionalMappings(MapperService mapperService) throws IOException {
|
protected void initializeAdditionalMappings(MapperService mapperService) throws IOException {
|
||||||
mapperService.merge("t_boost", new CompressedXContent(PutMappingRequest.buildFromSimplifiedDef("t_boost",
|
mapperService.merge("t_boost", new CompressedXContent(PutMappingRequest.buildFromSimplifiedDef("t_boost",
|
||||||
|
@ -85,13 +85,8 @@ public class MultiMatchQueryBuilderTests extends AbstractQueryTestCase<MultiMatc
|
|||||||
if (randomBoolean()) {
|
if (randomBoolean()) {
|
||||||
query.operator(randomFrom(Operator.values()));
|
query.operator(randomFrom(Operator.values()));
|
||||||
}
|
}
|
||||||
if (randomBoolean()) {
|
if (randomBoolean() && fieldName.equals(STRING_FIELD_NAME)) {
|
||||||
if (fieldName.equals(DATE_FIELD_NAME)) {
|
query.analyzer(randomAnalyzer());
|
||||||
// tokenized dates would trigger parse errors
|
|
||||||
query.analyzer("keyword");
|
|
||||||
} else {
|
|
||||||
query.analyzer(randomAnalyzer());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (randomBoolean()) {
|
if (randomBoolean()) {
|
||||||
query.slop(randomIntBetween(0, 5));
|
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 =
|
String json =
|
||||||
"{\n" +
|
"{\n" +
|
||||||
" \"multi_match\" : {\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());
|
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 {
|
public void testFuzzinessOnNonStringField() throws Exception {
|
||||||
assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
|
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);
|
MultiMatchQueryBuilder query = new MultiMatchQueryBuilder(42).field(INT_FIELD_NAME).field(BOOLEAN_FIELD_NAME);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user