[TEST] fix MultiMatchQueryIT random docs generation so that they don't interfere in score tests

When generating random bogus documents, it could happen that they contain both the terms "the" and "ultimate", which would match the query "the ultimate" better than all the other non bogus documents, which would cause testCrossFieldMode to fail. "the" is a term that's relatively likely to be randomly generated given its length; we can simply increase the minimum length of randomly generated terms to 5, so that there are no collisions, as "the" cannot be generated anymore (nor can "ultimate" as the lenght doesn't go up to 8).

Also made some assertions more accurate to check how many hits match a query rather than checking only that the first or second hits are there.

Closes #18873
This commit is contained in:
javanna 2016-09-13 18:25:33 +02:00 committed by Luca Cavanna
parent afbd7cbeb8
commit e0074ee9d4
1 changed files with 17 additions and 13 deletions

View File

@ -260,8 +260,8 @@ public class MultiMatchQueryIT extends ESIntegTestCase {
.addSort("_uid", SortOrder.ASC)
.setQuery(multiMatchQueryBuilder).get();
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery(field, builder.toString());
if (getType(multiMatchQueryBuilder) != null) {
matchQueryBuilder.type(MatchQuery.Type.valueOf(getType(multiMatchQueryBuilder).matchQueryType().toString()));
if (multiMatchQueryBuilder.getType() != null) {
matchQueryBuilder.type(MatchQuery.Type.valueOf(multiMatchQueryBuilder.getType().matchQueryType().toString()));
}
SearchResponse matchResp = client().prepareSearch("test")
// _uid tie sort
@ -569,9 +569,10 @@ public class MultiMatchQueryIT extends ESIntegTestCase {
// test if boosts work
searchResponse = client().prepareSearch("test")
.setQuery(randomizeType(multiMatchQuery("the ultimate", "full_name", "first_name", "last_name", "category").field("last_name", 10)
.setQuery(randomizeType(multiMatchQuery("the ultimate", "full_name", "first_name", "category").field("last_name", 10)
.type(MultiMatchQueryBuilder.Type.CROSS_FIELDS)
.operator(Operator.AND))).get();
assertHitCount(searchResponse, 2L);
assertFirstHit(searchResponse, hasId("ultimate1")); // has ultimate in the last_name and that is boosted
assertSecondHit(searchResponse, hasId("ultimate2"));
assertThat(searchResponse.getHits().hits()[0].getScore(), greaterThan(searchResponse.getHits().hits()[1].getScore()));
@ -582,6 +583,7 @@ public class MultiMatchQueryIT extends ESIntegTestCase {
.setQuery(randomizeType(multiMatchQuery("the ultimate", "full_name", "first_name", "last_name", "category")
.type(MultiMatchQueryBuilder.Type.CROSS_FIELDS)
.operator(Operator.AND))).get();
assertHitCount(searchResponse, 2L);
assertFirstHit(searchResponse, hasId("ultimate2"));
assertSecondHit(searchResponse, hasId("ultimate1"));
assertThat(searchResponse.getHits().hits()[0].getScore(), greaterThan(searchResponse.getHits().hits()[1].getScore()));
@ -590,28 +592,33 @@ public class MultiMatchQueryIT extends ESIntegTestCase {
searchResponse = client().prepareSearch("test")
.setQuery(randomizeType(multiMatchQuery("15", "skill")
.type(MultiMatchQueryBuilder.Type.CROSS_FIELDS))).get();
assertHitCount(searchResponse, 1L);
assertFirstHit(searchResponse, hasId("theone"));
searchResponse = client().prepareSearch("test")
.setQuery(randomizeType(multiMatchQuery("15", "skill", "first_name")
.type(MultiMatchQueryBuilder.Type.CROSS_FIELDS))).get();
assertHitCount(searchResponse, 1L);
assertFirstHit(searchResponse, hasId("theone"));
// Two numeric fields together caused trouble at one point!
searchResponse = client().prepareSearch("test")
.setQuery(randomizeType(multiMatchQuery("15", "int-field", "skill")
.type(MultiMatchQueryBuilder.Type.CROSS_FIELDS))).get();
assertHitCount(searchResponse, 1L);
assertFirstHit(searchResponse, hasId("theone"));
searchResponse = client().prepareSearch("test")
.setQuery(randomizeType(multiMatchQuery("15", "int-field", "first_name", "skill")
.type(MultiMatchQueryBuilder.Type.CROSS_FIELDS))).get();
assertHitCount(searchResponse, 1L);
assertFirstHit(searchResponse, hasId("theone"));
searchResponse = client().prepareSearch("test")
.setQuery(randomizeType(multiMatchQuery("alpha 15", "first_name", "skill")
.type(MultiMatchQueryBuilder.Type.CROSS_FIELDS)
.lenient(true))).get();
assertHitCount(searchResponse, 1L);
assertFirstHit(searchResponse, hasId("ultimate1"));
/*
* Doesn't find theone because "alpha 15" isn't a number and we don't
@ -624,6 +631,7 @@ public class MultiMatchQueryIT extends ESIntegTestCase {
.setQuery(randomizeType(multiMatchQuery("alpha 15", "int-field", "first_name", "skill")
.type(MultiMatchQueryBuilder.Type.CROSS_FIELDS)
.lenient(true))).get();
assertHitCount(searchResponse, 1L);
assertFirstHit(searchResponse, hasId("ultimate1"));
}
@ -647,21 +655,21 @@ public class MultiMatchQueryIT extends ESIntegTestCase {
}
}
public static List<String> fill(List<String> list, String value, int times) {
private static List<String> fill(List<String> list, String value, int times) {
for (int i = 0; i < times; i++) {
list.add(value);
}
return list;
}
public List<String> fillRandom(List<String> list, int times) {
private static List<String> fillRandom(List<String> list, int times) {
for (int i = 0; i < times; i++) {
list.add(randomAsciiOfLengthBetween(1, 5));
list.add(randomAsciiOfLength(5));
}
return list;
}
public <T> T randomPickExcept(List<T> fromList, T butNot) {
private static <T> T randomPickExcept(List<T> fromList, T butNot) {
while (true) {
T t = RandomPicks.randomFrom(random(), fromList);
if (t.equals(butNot)) {
@ -671,9 +679,9 @@ public class MultiMatchQueryIT extends ESIntegTestCase {
}
}
public MultiMatchQueryBuilder randomizeType(MultiMatchQueryBuilder builder) {
private static MultiMatchQueryBuilder randomizeType(MultiMatchQueryBuilder builder) {
try {
MultiMatchQueryBuilder.Type type = getType(builder);
MultiMatchQueryBuilder.Type type = builder.getType();
if (type == null && randomBoolean()) {
return builder;
}
@ -715,8 +723,4 @@ public class MultiMatchQueryIT extends ESIntegTestCase {
throw new RuntimeException(ex);
}
}
private MultiMatchQueryBuilder.Type getType(MultiMatchQueryBuilder builder) throws NoSuchFieldException, IllegalAccessException {
return builder.getType();
}
}