Skip to content

Commit

Permalink
Fix test that assumed a certain order of doc IDs with equal score
Browse files Browse the repository at this point in the history
If the score is equal in Lucene the order of the result depends on the
actual global doc ID such that due to background merges or concurrency
these test can return different result set orders.
  • Loading branch information
s1monw committed Jul 25, 2013
1 parent 50a835d commit 6101cbf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
Expand Up @@ -31,6 +31,8 @@
import org.hamcrest.Matcher;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
Expand All @@ -46,6 +48,16 @@ public class ElasticsearchAssertions {
public static void assertHitCount(SearchResponse searchResponse, long expectedHitCount) {
assertThat(searchResponse.getHits().totalHits(), is(expectedHitCount));
}

public static void assertSearchHits(SearchResponse searchResponse, String... ids) {

This comment has been minimized.

Copy link
@spinscale

spinscale Jul 25, 2013

Contributor

Can we make the naming more exact? The name does not imply that the order can be arbitrary.

assertThat("Expected different hit count", searchResponse.getHits().hits().length, equalTo(ids.length));

Set<String> idsSet = new HashSet<String>(Arrays.asList(ids));
for (SearchHit hit : searchResponse.getHits()) {
assertThat("Expected id: " + hit.getId() + " in the result but wasn't", idsSet.remove(hit.getId()), equalTo(true));
}
assertThat("Expected ids: " + Arrays.toString(idsSet.toArray(new String[0])) + " in the result - result size differs", idsSet.size(), equalTo(0));
}

public static void assertHitCount(CountResponse countResponse, long expectedHitCount) {
assertThat(countResponse.getCount(), is(expectedHitCount));
Expand Down
Expand Up @@ -39,13 +39,9 @@
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.FilterBuilders.*;
import static org.elasticsearch.index.query.QueryBuilders.*;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
*
Expand Down Expand Up @@ -545,8 +541,8 @@ public void testMultiMatchQuery() throws Exception {
.execute().actionGet();

assertThat(searchResponse.getHits().totalHits(), equalTo(2l));
assertThat("1", equalTo(searchResponse.getHits().getAt(0).id()));
assertThat("2", equalTo(searchResponse.getHits().getAt(1).id()));
// this uses dismax so scores are equal and the order can be arbitrary
assertSearchHits(searchResponse, "1", "2");

builder.useDisMax(false);
searchResponse = client().prepareSearch()
Expand Down

0 comments on commit 6101cbf

Please sign in to comment.