Search in sources :

Example 1 with Stats

use of org.elasticsearch.index.search.stats.SearchStats.Stats in project elasticsearch by elastic.

the class SearchStatsIT method testOpenContexts.

public void testOpenContexts() {
    String index = "test1";
    createIndex(index);
    ensureGreen(index);
    // create shards * docs number of docs and attempt to distribute them equally
    // this distribution will not be perfect; each shard will have an integer multiple of docs (possibly zero)
    // we do this so we have a lot of pages to scroll through
    final int docs = scaledRandomIntBetween(20, 50);
    for (int s = 0; s < numAssignedShards(index); s++) {
        for (int i = 0; i < docs; i++) {
            client().prepareIndex(index, "type", Integer.toString(s * docs + i)).setSource("field", "value").setRouting(Integer.toString(s)).execute().actionGet();
        }
    }
    client().admin().indices().prepareRefresh(index).execute().actionGet();
    IndicesStatsResponse indicesStats = client().admin().indices().prepareStats(index).execute().actionGet();
    assertThat(indicesStats.getTotal().getSearch().getOpenContexts(), equalTo(0L));
    int size = scaledRandomIntBetween(1, docs);
    SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).setScroll(TimeValue.timeValueMinutes(2)).execute().actionGet();
    assertSearchResponse(searchResponse);
    // refresh the stats now that scroll contexts are opened
    indicesStats = client().admin().indices().prepareStats(index).execute().actionGet();
    assertThat(indicesStats.getTotal().getSearch().getOpenContexts(), equalTo((long) numAssignedShards(index)));
    assertThat(indicesStats.getTotal().getSearch().getTotal().getScrollCurrent(), equalTo((long) numAssignedShards(index)));
    int hits = 0;
    while (true) {
        if (searchResponse.getHits().getHits().length == 0) {
            break;
        }
        hits += searchResponse.getHits().getHits().length;
        searchResponse = client().prepareSearchScroll(searchResponse.getScrollId()).setScroll(TimeValue.timeValueMinutes(2)).execute().actionGet();
    }
    long expected = 0;
    // the number of queries executed is equal to at least the sum of number of pages in shard over all shards
    IndicesStatsResponse r = client().admin().indices().prepareStats(index).execute().actionGet();
    for (int s = 0; s < numAssignedShards(index); s++) {
        expected += (long) Math.ceil(r.getShards()[s].getStats().getDocs().getCount() / size);
    }
    indicesStats = client().admin().indices().prepareStats().execute().actionGet();
    Stats stats = indicesStats.getTotal().getSearch().getTotal();
    assertEquals(hits, docs * numAssignedShards(index));
    assertThat(stats.getQueryCount(), greaterThanOrEqualTo(expected));
    clearScroll(searchResponse.getScrollId());
    indicesStats = client().admin().indices().prepareStats().execute().actionGet();
    stats = indicesStats.getTotal().getSearch().getTotal();
    assertThat(indicesStats.getTotal().getSearch().getOpenContexts(), equalTo(0L));
    assertThat(stats.getScrollCount(), equalTo((long) numAssignedShards(index)));
    assertThat(stats.getScrollTimeInMillis(), greaterThan(0L));
}
Also used : IndicesStatsResponse(org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse) NodeStats(org.elasticsearch.action.admin.cluster.node.stats.NodeStats) Stats(org.elasticsearch.index.search.stats.SearchStats.Stats) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 2 with Stats

use of org.elasticsearch.index.search.stats.SearchStats.Stats in project elasticsearch by elastic.

the class SearchStatsUnitTests method testShardLevelSearchGroupStats.

// https://github.com/elastic/elasticsearch/issues/7644
public void testShardLevelSearchGroupStats() throws Exception {
    // let's create two dummy search stats with groups
    Map<String, Stats> groupStats1 = new HashMap<>();
    Map<String, Stats> groupStats2 = new HashMap<>();
    groupStats2.put("group1", new Stats(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1));
    SearchStats searchStats1 = new SearchStats(new Stats(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), 0, groupStats1);
    SearchStats searchStats2 = new SearchStats(new Stats(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), 0, groupStats2);
    // adding these two search stats and checking group stats are correct
    searchStats1.add(searchStats2);
    assertStats(groupStats1.get("group1"), 1);
    // another call, adding again ...
    searchStats1.add(searchStats2);
    assertStats(groupStats1.get("group1"), 2);
    // making sure stats2 was not affected (this would previously return 2!)
    assertStats(groupStats2.get("group1"), 1);
    // adding again would then return wrong search stats (would return 4! instead of 3)
    searchStats1.add(searchStats2);
    assertStats(groupStats1.get("group1"), 3);
}
Also used : HashMap(java.util.HashMap) Stats(org.elasticsearch.index.search.stats.SearchStats.Stats) SearchStats(org.elasticsearch.index.search.stats.SearchStats) SearchStats(org.elasticsearch.index.search.stats.SearchStats)

Example 3 with Stats

use of org.elasticsearch.index.search.stats.SearchStats.Stats in project elasticsearch by elastic.

the class SearchStatsIT method testSimpleStats.

public void testSimpleStats() throws Exception {
    // clear all stats first
    client().admin().indices().prepareStats().clear().execute().actionGet();
    final int numNodes = cluster().numDataNodes();
    assertThat(numNodes, greaterThanOrEqualTo(2));
    // we make sure each node gets at least a single shard...
    final int shardsIdx1 = randomIntBetween(1, 10);
    final int shardsIdx2 = Math.max(numNodes - shardsIdx1, randomIntBetween(1, 10));
    assertThat(numNodes, lessThanOrEqualTo(shardsIdx1 + shardsIdx2));
    assertAcked(prepareCreate("test1").setSettings(Settings.builder().put(SETTING_NUMBER_OF_SHARDS, shardsIdx1).put(SETTING_NUMBER_OF_REPLICAS, 0)));
    int docsTest1 = scaledRandomIntBetween(3 * shardsIdx1, 5 * shardsIdx1);
    for (int i = 0; i < docsTest1; i++) {
        client().prepareIndex("test1", "type", Integer.toString(i)).setSource("field", "value").execute().actionGet();
        if (rarely()) {
            refresh();
        }
    }
    assertAcked(prepareCreate("test2").setSettings(Settings.builder().put(SETTING_NUMBER_OF_SHARDS, shardsIdx2).put(SETTING_NUMBER_OF_REPLICAS, 0)));
    int docsTest2 = scaledRandomIntBetween(3 * shardsIdx2, 5 * shardsIdx2);
    for (int i = 0; i < docsTest2; i++) {
        client().prepareIndex("test2", "type", Integer.toString(i)).setSource("field", "value").execute().actionGet();
        if (rarely()) {
            refresh();
        }
    }
    assertThat(shardsIdx1 + shardsIdx2, equalTo(numAssignedShards("test1", "test2")));
    assertThat(numAssignedShards("test1", "test2"), greaterThanOrEqualTo(2));
    // THERE WILL BE AT LEAST 2 NODES HERE SO WE CAN WAIT FOR GREEN
    ensureGreen();
    refresh();
    int iters = scaledRandomIntBetween(100, 150);
    for (int i = 0; i < iters; i++) {
        SearchResponse searchResponse = internalCluster().coordOnlyNodeClient().prepareSearch().setQuery(QueryBuilders.termQuery("field", "value")).setStats("group1", "group2").highlighter(new HighlightBuilder().field("field")).addScriptField("script1", new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "_source.field", Collections.emptyMap())).setSize(100).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));
    assertThat(indicesStats.getTotal().getSearch().getTotal().getFetchTimeInMillis(), greaterThan(0L));
    assertThat(indicesStats.getTotal().getSearch().getGroupStats(), nullValue());
    indicesStats = client().admin().indices().prepareStats().setGroups("group1").execute().actionGet();
    assertThat(indicesStats.getTotal().getSearch().getGroupStats(), notNullValue());
    assertThat(indicesStats.getTotal().getSearch().getGroupStats().get("group1").getQueryCount(), greaterThan(0L));
    assertThat(indicesStats.getTotal().getSearch().getGroupStats().get("group1").getQueryTimeInMillis(), greaterThan(0L));
    assertThat(indicesStats.getTotal().getSearch().getGroupStats().get("group1").getFetchCount(), greaterThan(0L));
    assertThat(indicesStats.getTotal().getSearch().getGroupStats().get("group1").getFetchTimeInMillis(), greaterThan(0L));
    NodesStatsResponse nodeStats = client().admin().cluster().prepareNodesStats().execute().actionGet();
    Set<String> nodeIdsWithIndex = nodeIdsWithIndex("test1", "test2");
    int num = 0;
    for (NodeStats stat : nodeStats.getNodes()) {
        Stats total = stat.getIndices().getSearch().getTotal();
        if (nodeIdsWithIndex.contains(stat.getNode().getId())) {
            assertThat(total.getQueryCount(), greaterThan(0L));
            assertThat(total.getQueryTimeInMillis(), greaterThan(0L));
            num++;
        } else {
            assertThat(total.getQueryCount(), equalTo(0L));
            assertThat(total.getQueryTimeInMillis(), equalTo(0L));
        }
    }
    assertThat(num, greaterThan(0));
}
Also used : NodesStatsResponse(org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse) Script(org.elasticsearch.script.Script) IndicesStatsResponse(org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse) NodeStats(org.elasticsearch.action.admin.cluster.node.stats.NodeStats) NodeStats(org.elasticsearch.action.admin.cluster.node.stats.NodeStats) Stats(org.elasticsearch.index.search.stats.SearchStats.Stats) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

Stats (org.elasticsearch.index.search.stats.SearchStats.Stats)3 NodeStats (org.elasticsearch.action.admin.cluster.node.stats.NodeStats)2 IndicesStatsResponse (org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse)2 SearchResponse (org.elasticsearch.action.search.SearchResponse)2 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)2 HashMap (java.util.HashMap)1 NodesStatsResponse (org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse)1 SearchStats (org.elasticsearch.index.search.stats.SearchStats)1 Script (org.elasticsearch.script.Script)1 HighlightBuilder (org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)1