diff --git a/src/main/java/org/elasticsearch/index/search/stats/SearchStats.java b/src/main/java/org/elasticsearch/index/search/stats/SearchStats.java index 7e2fda307f6..06136ee68fb 100644 --- a/src/main/java/org/elasticsearch/index/search/stats/SearchStats.java +++ b/src/main/java/org/elasticsearch/index/search/stats/SearchStats.java @@ -27,6 +27,7 @@ import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilderString; +import org.elasticsearch.common.xcontent.XContentFactory; import java.io.IOException; import java.util.HashMap; @@ -264,4 +265,17 @@ public class SearchStats implements Streamable, ToXContent { } } } + + @Override + public String toString() { + try { + XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint(); + builder.startObject(); + toXContent(builder, EMPTY_PARAMS); + builder.endObject(); + return builder.string(); + } catch (IOException e) { + return "{ \"error\" : \"" + e.getMessage() + "\"}"; + } + } } diff --git a/src/test/java/org/elasticsearch/search/stats/SearchStatsTests.java b/src/test/java/org/elasticsearch/search/stats/SearchStatsTests.java index 6f4235ad38a..75e228cd7fc 100644 --- a/src/test/java/org/elasticsearch/search/stats/SearchStatsTests.java +++ b/src/test/java/org/elasticsearch/search/stats/SearchStatsTests.java @@ -91,12 +91,13 @@ public class SearchStatsTests extends ElasticsearchIntegrationTest { refresh(); int iters = scaledRandomIntBetween(20, 50); for (int i = 0; i < iters; i++) { - SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.termQuery("field", "value")).setStats("group1", "group2").execute().actionGet(); + SearchResponse searchResponse = cluster().clientNodeClient().prepareSearch().setQuery(QueryBuilders.termQuery("field", "value")).setStats("group1", "group2").execute().actionGet(); assertHitCount(searchResponse, docsTest1 + docsTest2); assertAllSuccessful(searchResponse); } IndicesStatsResponse indicesStats = client().admin().indices().prepareStats().execute().actionGet(); + logger.debug("###### indices search stats: " + indicesStats.getTotal().getSearch()); assertThat(indicesStats.getTotal().getSearch().getTotal().getQueryCount(), greaterThan(0l)); assertThat(indicesStats.getTotal().getSearch().getTotal().getQueryTimeInMillis(), greaterThan(0l)); assertThat(indicesStats.getTotal().getSearch().getTotal().getFetchCount(), greaterThan(0l));