[TEST] Ensure multi_match & match query equivalence in the single field case

This commit is contained in:
Simon Willnauer 2014-05-19 13:31:58 +02:00
parent 39018c5d0b
commit c593234b7c
1 changed files with 40 additions and 5 deletions

View File

@ -27,6 +27,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortBuilders;
@ -211,12 +212,42 @@ public class MultiMatchQueryTests extends ElasticsearchIntegrationTest {
}
@Test
public void testSingleField() {
public void testSingleField() throws NoSuchFieldException, IllegalAccessException {
SearchResponse searchResponse = client().prepareSearch("test")
.setQuery(randomizeType(multiMatchQuery("15", "skill"))).get();
assertNoFailures(searchResponse);
assertFirstHit(searchResponse, hasId("theone"));
// TODO we need equivalence tests with match query here
String[] fields = {"full_name", "first_name", "last_name", "last_name_phrase", "first_name_phrase", "category_phrase", "category"};
String[] query = {"marvel","hero", "captain", "america", "15", "17", "1", "5", "ultimate", "Man",
"marvel", "wolferine", "ninja"};
// check if it's equivalent to a match query.
int numIters = scaledRandomIntBetween(10, 100);
for (int i = 0; i < numIters; i++) {
String field = RandomPicks.randomFrom(getRandom(), fields);
int numTerms = randomIntBetween(1, query.length);
StringBuilder builder = new StringBuilder();
for (int j = 0; j < numTerms; j++) {
builder.append(RandomPicks.randomFrom(getRandom(), query)).append(" ");
}
MultiMatchQueryBuilder multiMatchQueryBuilder = randomizeType(multiMatchQuery(builder.toString(), field));
SearchResponse multiMatchResp = client().prepareSearch("test")
.setQuery(multiMatchQueryBuilder).get();
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery(field, builder.toString());
if (getType(multiMatchQueryBuilder) != null) {
matchQueryBuilder.type(MatchQueryBuilder.Type.valueOf(getType(multiMatchQueryBuilder).matchQueryType().toString()));
}
SearchResponse matchResp = client().prepareSearch("test")
.setQuery(matchQueryBuilder).get();
assertThat("field: " + field + " query: " + builder.toString(), multiMatchResp.getHits().getTotalHits(), equalTo(matchResp.getHits().getTotalHits()));
SearchHits hits = multiMatchResp.getHits();
for (int j = 0; j < hits.hits().length; j++) {
assertThat(hits.getHits()[j].score(), equalTo(matchResp.getHits().getHits()[j].score()));
assertThat(hits.getHits()[j].getId(), equalTo(matchResp.getHits().getHits()[j].getId()));
}
}
}
@Test
@ -531,9 +562,7 @@ public class MultiMatchQueryTests extends ElasticsearchIntegrationTest {
public MultiMatchQueryBuilder randomizeType(MultiMatchQueryBuilder builder) {
try {
Field field = MultiMatchQueryBuilder.class.getDeclaredField("type");
field.setAccessible(true);
MultiMatchQueryBuilder.Type type = (MultiMatchQueryBuilder.Type) field.get(builder);
MultiMatchQueryBuilder.Type type = getType(builder);
if (type == null && randomBoolean()) {
return builder;
}
@ -575,4 +604,10 @@ public class MultiMatchQueryTests extends ElasticsearchIntegrationTest {
throw new RuntimeException(ex);
}
}
private MultiMatchQueryBuilder.Type getType(MultiMatchQueryBuilder builder) throws NoSuchFieldException, IllegalAccessException {
Field field = MultiMatchQueryBuilder.class.getDeclaredField("type");
field.setAccessible(true);
return (MultiMatchQueryBuilder.Type) field.get(builder);
}
}