Added total, successful shard info to search related asserts in ElasticsearchAssertions
Rewrote SimpleSearchTests assertion to use that.
This commit is contained in:
parent
f8cf5ae7e9
commit
c70b8fbd7b
|
@ -48,56 +48,66 @@ import static org.junit.Assert.fail;
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class ElasticsearchAssertions {
|
public class ElasticsearchAssertions {
|
||||||
|
|
||||||
|
|
||||||
public static void assertAcked(DeleteIndexRequestBuilder builder) {
|
public static void assertAcked(DeleteIndexRequestBuilder builder) {
|
||||||
assertAcked(builder.get());
|
assertAcked(builder.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void assertAcked(CreateIndexRequestBuilder builder) {
|
public static void assertAcked(CreateIndexRequestBuilder builder) {
|
||||||
assertAcked(builder.get());
|
assertAcked(builder.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void assertAcked(DeleteIndexResponse response) {
|
public static void assertAcked(DeleteIndexResponse response) {
|
||||||
assertThat("Delete Index failed - not acked", response.isAcknowledged(), equalTo(true));
|
assertThat("Delete Index failed - not acked", response.isAcknowledged(), equalTo(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void assertAcked(CreateIndexResponse response) {
|
public static void assertAcked(CreateIndexResponse response) {
|
||||||
assertThat("Create Index failed - not acked", response.isAcknowledged(), equalTo(true));
|
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
|
* assertions
|
||||||
*/
|
*/
|
||||||
public static void assertHitCount(SearchResponse searchResponse, long expectedHitCount) {
|
public static void assertHitCount(SearchResponse searchResponse, long expectedHitCount) {
|
||||||
if (searchResponse.getHits().totalHits() != expectedHitCount) {
|
if (searchResponse.getHits().totalHits() != expectedHitCount) {
|
||||||
String msg = "Hit count is " + searchResponse.getHits().totalHits() + " but " + expectedHitCount + " was expected. " +
|
fail("Hit count is " + searchResponse.getHits().totalHits() + " but " + expectedHitCount + " was expected. " + formatShardStatus(searchResponse));
|
||||||
searchResponse.getFailedShards() + " shard failures:";
|
|
||||||
for (ShardSearchFailure failure : searchResponse.getShardFailures()) {
|
|
||||||
msg += "\n " + failure.toString();
|
|
||||||
}
|
|
||||||
fail(msg);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void assertSearchHits(SearchResponse searchResponse, String... ids) {
|
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));
|
Set<String> idsSet = new HashSet<String>(Arrays.asList(ids));
|
||||||
for (SearchHit hit : searchResponse.getHits()) {
|
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) {
|
public static void assertHitCount(CountResponse countResponse, long expectedHitCount) {
|
||||||
if (countResponse.getCount() != expectedHitCount) {
|
if (countResponse.getCount() != expectedHitCount) {
|
||||||
String msg = "Count is " + countResponse.getCount() + " but " + expectedHitCount + " was expected. " +
|
fail("Count is " + countResponse.getCount() + " but " + expectedHitCount + " was expected. " +
|
||||||
countResponse.getFailedShards() + " shard failures:";
|
formatShardStatus(countResponse));
|
||||||
for (ShardOperationFailedException failure : countResponse.getShardFailures()) {
|
|
||||||
msg += "\n " + failure.toString();
|
|
||||||
}
|
|
||||||
fail(msg);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -32,8 +32,8 @@ import java.util.concurrent.ExecutionException;
|
||||||
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
|
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.rangeQuery;
|
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.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
|
||||||
|
|
||||||
public class SimpleSearchTests extends AbstractSharedClusterTest {
|
public class SimpleSearchTests extends AbstractSharedClusterTest {
|
||||||
|
|
||||||
|
@ -53,23 +53,24 @@ public class SimpleSearchTests extends AbstractSharedClusterTest {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSearchRandomPreference() throws InterruptedException, ExecutionException {
|
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,
|
indexRandom("test", true,
|
||||||
client().prepareIndex("test", "type", "1").setSource("field", "value"),
|
client().prepareIndex("test", "type", "1").setSource("field", "value"),
|
||||||
client().prepareIndex("test", "type", "2").setSource("field", "value"),
|
client().prepareIndex("test", "type", "2").setSource("field", "value"),
|
||||||
client().prepareIndex("test", "type", "3").setSource("field", "value"),
|
client().prepareIndex("test", "type", "3").setSource("field", "value"),
|
||||||
client().prepareIndex("test", "type", "4").setSource("field", "value"),
|
client().prepareIndex("test", "type", "4").setSource("field", "value"),
|
||||||
client().prepareIndex("test", "type", "5").setSource("field", "value"),
|
client().prepareIndex("test", "type", "5").setSource("field", "value"),
|
||||||
client().prepareIndex("test", "type", "6").setSource("field", "value"));
|
client().prepareIndex("test", "type", "6").setSource("field", "value"));
|
||||||
|
|
||||||
int iters = atLeast(10);
|
int iters = atLeast(10);
|
||||||
for (int i = 0; i < iters; i++) {
|
for (int i = 0; i < iters; i++) {
|
||||||
// id is not indexed, but lets see that we automatically convert to
|
// id is not indexed, but lets see that we automatically convert to
|
||||||
SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchAllQuery()).setPreference(randomUnicodeOfLengthBetween(0, 4)).get();
|
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")))
|
.setQuery(boolQuery().must(rangeQuery("from").lt("192.168.0.7")).must(rangeQuery("to").gt("192.168.0.7")))
|
||||||
.execute().actionGet();
|
.execute().actionGet();
|
||||||
|
|
||||||
assertThat(search.getHits().totalHits(), equalTo(1l));
|
assertHitCount(search, 1l);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -100,17 +101,17 @@ public class SimpleSearchTests extends AbstractSharedClusterTest {
|
||||||
client().prepareIndex("test", "type", "XXX1").setSource("field", "value").setRefresh(true).execute().actionGet();
|
client().prepareIndex("test", "type", "XXX1").setSource("field", "value").setRefresh(true).execute().actionGet();
|
||||||
// id is not indexed, but lets see that we automatically convert to
|
// id is not indexed, but lets see that we automatically convert to
|
||||||
SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.termQuery("_id", "XXX1")).execute().actionGet();
|
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();
|
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
|
// id is not index, but we can automatically support prefix as well
|
||||||
searchResponse = client().prepareSearch().setQuery(QueryBuilders.prefixQuery("_id", "XXX")).execute().actionGet();
|
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();
|
searchResponse = client().prepareSearch().setQuery(QueryBuilders.queryString("_id:XXX*").lowercaseExpandedTerms(false)).execute().actionGet();
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1l));
|
assertHitCount(searchResponse, 1l);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -122,9 +123,9 @@ public class SimpleSearchTests extends AbstractSharedClusterTest {
|
||||||
|
|
||||||
// test include upper on ranges to include the full day on the upper bound
|
// 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();
|
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();
|
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
|
@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...)
|
// 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();
|
SearchResponse searchResponse = client().prepareSearch("test").setQuery(QueryBuilders.rangeQuery("field").gte("2010-01-05").lte("2010-01-06")).execute().actionGet();
|
||||||
assertNoFailures(searchResponse);
|
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();
|
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
|
@Test
|
||||||
|
@ -151,10 +152,10 @@ public class SimpleSearchTests extends AbstractSharedClusterTest {
|
||||||
refresh();
|
refresh();
|
||||||
SearchResponse searchResponse = client().prepareSearch("test").setQuery(QueryBuilders.rangeQuery("field").gte("2010-01-03||+2d").lte("2010-01-04||+2d")).execute().actionGet();
|
SearchResponse searchResponse = client().prepareSearch("test").setQuery(QueryBuilders.rangeQuery("field").gte("2010-01-03||+2d").lte("2010-01-04||+2d")).execute().actionGet();
|
||||||
assertNoFailures(searchResponse);
|
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();
|
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
|
@Test
|
||||||
|
@ -184,12 +185,14 @@ public class SimpleSearchTests extends AbstractSharedClusterTest {
|
||||||
SearchResponse searchResponse = client().prepareSearch("test")
|
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"))
|
.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();
|
.execute().actionGet();
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(10l));
|
assertHitCount(searchResponse, 10l);
|
||||||
|
|
||||||
|
|
||||||
searchResponse = client().prepareSearch("test")
|
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"))
|
.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();
|
.execute().actionGet();
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(20l));
|
assertHitCount(searchResponse, 20l);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue