Added total, successful shard info to search related asserts in ElasticsearchAssertions

Rewrote SimpleSearchTests assertion to use that.
This commit is contained in:
Boaz Leskes 2013-08-30 14:02:13 +02:00
parent f8cf5ae7e9
commit c70b8fbd7b
2 changed files with 58 additions and 45 deletions

View File

@ -66,38 +66,48 @@ public class ElasticsearchAssertions {
assertThat("Create Index failed - not acked", response.isAcknowledged(), equalTo(true));
}
public static String formatShardStatus(BroadcastOperationResponse response) {
String msg = " Total shards: " + response.getTotalShards() + " Successful shards: " + response.getSuccessfulShards() +
" & " + response.getFailedShards() + " shard failures:";
for (ShardOperationFailedException failure : response.getShardFailures()) {
msg += "\n " + failure.toString();
}
return msg;
}
public static String formatShardStatus(SearchResponse response) {
String msg = " Total shards: " + response.getTotalShards() + " Successful shards: " + response.getSuccessfulShards() +
" & " + response.getFailedShards() + " shard failures:";
for (ShardSearchFailure failure : response.getShardFailures()) {
msg += "\n " + failure.toString();
}
return msg;
}
/*
* assertions
*/
public static void assertHitCount(SearchResponse searchResponse, long expectedHitCount) {
if (searchResponse.getHits().totalHits() != expectedHitCount) {
String msg = "Hit count is " + searchResponse.getHits().totalHits() + " but " + expectedHitCount + " was expected. " +
searchResponse.getFailedShards() + " shard failures:";
for (ShardSearchFailure failure : searchResponse.getShardFailures()) {
msg += "\n " + failure.toString();
}
fail(msg);
fail("Hit count is " + searchResponse.getHits().totalHits() + " but " + expectedHitCount + " was expected. " + formatShardStatus(searchResponse));
}
}
public static void assertSearchHits(SearchResponse searchResponse, String... ids) {
assertThat("Expected different hit count", searchResponse.getHits().hits().length, equalTo(ids.length));
String shardStatus = formatShardStatus(searchResponse);
assertThat("Expected different hit count. " + shardStatus, 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 id: " + hit.getId() + " in the result but wasn't." + shardStatus, 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));
assertThat("Expected ids: " + Arrays.toString(idsSet.toArray(new String[0])) + " in the result - result size differs." + shardStatus, idsSet.size(), equalTo(0));
}
public static void assertHitCount(CountResponse countResponse, long expectedHitCount) {
if (countResponse.getCount() != expectedHitCount) {
String msg = "Count is " + countResponse.getCount() + " but " + expectedHitCount + " was expected. " +
countResponse.getFailedShards() + " shard failures:";
for (ShardOperationFailedException failure : countResponse.getShardFailures()) {
msg += "\n " + failure.toString();
}
fail(msg);
fail("Count is " + countResponse.getCount() + " but " + expectedHitCount + " was expected. " +
formatShardStatus(countResponse));
}
}

View File

@ -32,8 +32,8 @@ import java.util.concurrent.ExecutionException;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.rangeQuery;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
import static org.hamcrest.Matchers.equalTo;
public class SimpleSearchTests extends AbstractSharedClusterTest {
@ -56,20 +56,21 @@ public class SimpleSearchTests extends AbstractSharedClusterTest {
@Test
public void testSearchRandomPreference() throws InterruptedException, ExecutionException {
client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", between(1,3))).get();
client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", between(1, 3))).get();
indexRandom("test", true,
client().prepareIndex("test", "type", "1").setSource("field", "value"),
client().prepareIndex("test", "type", "2").setSource("field", "value"),
client().prepareIndex("test", "type", "3").setSource("field", "value"),
client().prepareIndex("test", "type", "4").setSource("field", "value"),
client().prepareIndex("test", "type", "5").setSource("field", "value"),
client().prepareIndex("test", "type", "6").setSource("field", "value"));
client().prepareIndex("test", "type", "1").setSource("field", "value"),
client().prepareIndex("test", "type", "2").setSource("field", "value"),
client().prepareIndex("test", "type", "3").setSource("field", "value"),
client().prepareIndex("test", "type", "4").setSource("field", "value"),
client().prepareIndex("test", "type", "5").setSource("field", "value"),
client().prepareIndex("test", "type", "6").setSource("field", "value"));
int iters = atLeast(10);
for (int i = 0; i < iters; i++) {
// id is not indexed, but lets see that we automatically convert to
SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchAllQuery()).setPreference(randomUnicodeOfLengthBetween(0, 4)).get();
assertThat(searchResponse.getHits().totalHits(), equalTo(6l));
assertHitCount(searchResponse, 6l);
}
}
@ -90,7 +91,7 @@ public class SimpleSearchTests extends AbstractSharedClusterTest {
.setQuery(boolQuery().must(rangeQuery("from").lt("192.168.0.7")).must(rangeQuery("to").gt("192.168.0.7")))
.execute().actionGet();
assertThat(search.getHits().totalHits(), equalTo(1l));
assertHitCount(search, 1l);
}
@Test
@ -100,17 +101,17 @@ public class SimpleSearchTests extends AbstractSharedClusterTest {
client().prepareIndex("test", "type", "XXX1").setSource("field", "value").setRefresh(true).execute().actionGet();
// id is not indexed, but lets see that we automatically convert to
SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.termQuery("_id", "XXX1")).execute().actionGet();
assertThat(searchResponse.getHits().totalHits(), equalTo(1l));
assertHitCount(searchResponse, 1l);
searchResponse = client().prepareSearch().setQuery(QueryBuilders.queryString("_id:XXX1")).execute().actionGet();
assertThat(searchResponse.getHits().totalHits(), equalTo(1l));
assertHitCount(searchResponse, 1l);
// id is not index, but we can automatically support prefix as well
searchResponse = client().prepareSearch().setQuery(QueryBuilders.prefixQuery("_id", "XXX")).execute().actionGet();
assertThat(searchResponse.getHits().totalHits(), equalTo(1l));
assertHitCount(searchResponse, 1l);
searchResponse = client().prepareSearch().setQuery(QueryBuilders.queryString("_id:XXX*").lowercaseExpandedTerms(false)).execute().actionGet();
assertThat(searchResponse.getHits().totalHits(), equalTo(1l));
assertHitCount(searchResponse, 1l);
}
@Test
@ -122,9 +123,9 @@ public class SimpleSearchTests extends AbstractSharedClusterTest {
// test include upper on ranges to include the full day on the upper bound
SearchResponse searchResponse = client().prepareSearch("test").setQuery(QueryBuilders.rangeQuery("field").gte("2010-01-05").lte("2010-01-06")).execute().actionGet();
assertThat(searchResponse.getHits().totalHits(), equalTo(2l));
assertHitCount(searchResponse, 2l);
searchResponse = client().prepareSearch("test").setQuery(QueryBuilders.rangeQuery("field").gte("2010-01-05").lt("2010-01-06")).execute().actionGet();
assertThat(searchResponse.getHits().totalHits(), equalTo(1l));
assertHitCount(searchResponse, 1l);
}
@Test
@ -137,9 +138,9 @@ public class SimpleSearchTests extends AbstractSharedClusterTest {
// test include upper on ranges to include the full day on the upper bound (disabled here though...)
SearchResponse searchResponse = client().prepareSearch("test").setQuery(QueryBuilders.rangeQuery("field").gte("2010-01-05").lte("2010-01-06")).execute().actionGet();
assertNoFailures(searchResponse);
assertThat(searchResponse.getHits().totalHits(), equalTo(1l));
assertHitCount(searchResponse, 1l);
searchResponse = client().prepareSearch("test").setQuery(QueryBuilders.rangeQuery("field").gte("2010-01-05").lt("2010-01-06")).execute().actionGet();
assertThat(searchResponse.getHits().totalHits(), equalTo(1l));
assertHitCount(searchResponse, 1l);
}
@Test
@ -151,10 +152,10 @@ public class SimpleSearchTests extends AbstractSharedClusterTest {
refresh();
SearchResponse searchResponse = client().prepareSearch("test").setQuery(QueryBuilders.rangeQuery("field").gte("2010-01-03||+2d").lte("2010-01-04||+2d")).execute().actionGet();
assertNoFailures(searchResponse);
assertThat(searchResponse.getHits().totalHits(), equalTo(2l));
assertHitCount(searchResponse, 2l);
searchResponse = client().prepareSearch("test").setQuery(QueryBuilders.queryString("field:[2010-01-03||+2d TO 2010-01-04||+2d]")).execute().actionGet();
assertThat(searchResponse.getHits().totalHits(), equalTo(2l));
assertHitCount(searchResponse, 2l);
}
@Test
@ -184,12 +185,14 @@ public class SimpleSearchTests extends AbstractSharedClusterTest {
SearchResponse searchResponse = client().prepareSearch("test")
.setQuery(QueryBuilders.rangeQuery("date_field").gte("Di, 05 Dez 2000 02:55:00 -0800").lte("Do, 07 Dez 2000 00:00:00 -0800"))
.execute().actionGet();
assertThat(searchResponse.getHits().totalHits(), equalTo(10l));
assertHitCount(searchResponse, 10l);
searchResponse = client().prepareSearch("test")
.setQuery(QueryBuilders.rangeQuery("date_field").gte("Di, 05 Dez 2000 02:55:00 -0800").lte("Fr, 08 Dez 2000 00:00:00 -0800"))
.execute().actionGet();
assertThat(searchResponse.getHits().totalHits(), equalTo(20l));
assertHitCount(searchResponse, 20l);
}
}
}