@Test
  public void testAllFlags() throws Exception {
    // rely on 1 replica for this tests
    createIndex("test1");
    createIndex("test2");

    ClusterHealthResponse clusterHealthResponse =
        client()
            .admin()
            .cluster()
            .prepareHealth()
            .setWaitForEvents(Priority.LANGUID)
            .setWaitForGreenStatus()
            .execute()
            .actionGet();
    assertThat(clusterHealthResponse.isTimedOut(), equalTo(false));

    client()
        .prepareIndex("test1", "type1", Integer.toString(1))
        .setSource("field", "value")
        .execute()
        .actionGet();
    client()
        .prepareIndex("test1", "type2", Integer.toString(1))
        .setSource("field", "value")
        .execute()
        .actionGet();
    client()
        .prepareIndex("test2", "type", Integer.toString(1))
        .setSource("field", "value")
        .execute()
        .actionGet();

    client().admin().indices().prepareRefresh().execute().actionGet();
    IndicesStatsRequestBuilder builder = client().admin().indices().prepareStats();
    Flag[] values = CommonStatsFlags.Flag.values();
    for (Flag flag : values) {
      set(flag, builder, false);
    }

    IndicesStatsResponse stats = builder.execute().actionGet();
    for (Flag flag : values) {
      assertThat(isSet(flag, stats.getPrimaries()), equalTo(false));
      assertThat(isSet(flag, stats.getTotal()), equalTo(false));
    }

    for (Flag flag : values) {
      set(flag, builder, true);
    }
    stats = builder.execute().actionGet();
    for (Flag flag : values) {
      assertThat(isSet(flag, stats.getPrimaries()), equalTo(true));
      assertThat(isSet(flag, stats.getTotal()), equalTo(true));
    }
    Random random = getRandom();
    EnumSet<Flag> flags = EnumSet.noneOf(Flag.class);
    for (Flag flag : values) {
      if (random.nextBoolean()) {
        flags.add(flag);
      }
    }

    for (Flag flag : values) {
      set(flag, builder, false); // clear all
    }

    for (Flag flag : flags) { // set the flags
      set(flag, builder, true);
    }
    stats = builder.execute().actionGet();
    for (Flag flag : flags) { // check the flags
      assertThat(isSet(flag, stats.getPrimaries()), equalTo(true));
      assertThat(isSet(flag, stats.getTotal()), equalTo(true));
    }

    for (Flag flag : EnumSet.complementOf(flags)) { // check the complement
      assertThat(isSet(flag, stats.getPrimaries()), equalTo(false));
      assertThat(isSet(flag, stats.getTotal()), equalTo(false));
    }
  }
  @Test
  public void simpleStats() throws Exception {
    // rely on 1 replica for this tests
    createIndex("test1");
    createIndex("test2");

    ClusterHealthResponse clusterHealthResponse =
        client()
            .admin()
            .cluster()
            .prepareHealth()
            .setWaitForEvents(Priority.LANGUID)
            .setWaitForGreenStatus()
            .execute()
            .actionGet();
    assertThat(clusterHealthResponse.isTimedOut(), equalTo(false));

    client()
        .prepareIndex("test1", "type1", Integer.toString(1))
        .setSource("field", "value")
        .execute()
        .actionGet();
    client()
        .prepareIndex("test1", "type2", Integer.toString(1))
        .setSource("field", "value")
        .execute()
        .actionGet();
    client()
        .prepareIndex("test2", "type", Integer.toString(1))
        .setSource("field", "value")
        .execute()
        .actionGet();

    client().admin().indices().prepareRefresh().execute().actionGet();

    IndicesStatsResponse stats = client().admin().indices().prepareStats().execute().actionGet();
    assertThat(stats.getPrimaries().getDocs().getCount(), equalTo(3l));
    assertThat(stats.getTotal().getDocs().getCount(), equalTo(6l));
    assertThat(stats.getPrimaries().getIndexing().getTotal().getIndexCount(), equalTo(3l));
    assertThat(stats.getTotal().getIndexing().getTotal().getIndexCount(), equalTo(6l));
    assertThat(stats.getTotal().getStore(), notNullValue());
    // verify nulls
    assertThat(stats.getTotal().getMerge(), nullValue());
    assertThat(stats.getTotal().getFlush(), nullValue());
    assertThat(stats.getTotal().getRefresh(), nullValue());

    assertThat(stats.getIndex("test1").getPrimaries().getDocs().getCount(), equalTo(2l));
    assertThat(stats.getIndex("test1").getTotal().getDocs().getCount(), equalTo(4l));
    assertThat(stats.getIndex("test1").getPrimaries().getStore(), notNullValue());
    assertThat(stats.getIndex("test1").getPrimaries().getMerge(), nullValue());
    assertThat(stats.getIndex("test1").getPrimaries().getFlush(), nullValue());
    assertThat(stats.getIndex("test1").getPrimaries().getRefresh(), nullValue());

    assertThat(stats.getIndex("test2").getPrimaries().getDocs().getCount(), equalTo(1l));
    assertThat(stats.getIndex("test2").getTotal().getDocs().getCount(), equalTo(2l));

    // make sure that number of requests in progress is 0
    assertThat(
        stats.getIndex("test1").getTotal().getIndexing().getTotal().getIndexCurrent(), equalTo(0l));
    assertThat(
        stats.getIndex("test1").getTotal().getIndexing().getTotal().getDeleteCurrent(),
        equalTo(0l));
    assertThat(
        stats.getIndex("test1").getTotal().getSearch().getTotal().getFetchCurrent(), equalTo(0l));
    assertThat(
        stats.getIndex("test1").getTotal().getSearch().getTotal().getQueryCurrent(), equalTo(0l));

    // check flags
    stats =
        client()
            .admin()
            .indices()
            .prepareStats()
            .setDocs(false)
            .setStore(false)
            .setIndexing(false)
            .setFlush(true)
            .setRefresh(true)
            .setMerge(true)
            .execute()
            .actionGet();

    assertThat(stats.getTotal().getDocs(), nullValue());
    assertThat(stats.getTotal().getStore(), nullValue());
    assertThat(stats.getTotal().getIndexing(), nullValue());
    assertThat(stats.getTotal().getMerge(), notNullValue());
    assertThat(stats.getTotal().getFlush(), notNullValue());
    assertThat(stats.getTotal().getRefresh(), notNullValue());

    // check types
    stats =
        client().admin().indices().prepareStats().setTypes("type1", "type").execute().actionGet();
    assertThat(
        stats.getPrimaries().getIndexing().getTypeStats().get("type1").getIndexCount(),
        equalTo(1l));
    assertThat(
        stats.getPrimaries().getIndexing().getTypeStats().get("type").getIndexCount(), equalTo(1l));
    assertThat(stats.getPrimaries().getIndexing().getTypeStats().get("type2"), nullValue());
    assertThat(
        stats.getPrimaries().getIndexing().getTypeStats().get("type1").getIndexCurrent(),
        equalTo(0l));
    assertThat(
        stats.getPrimaries().getIndexing().getTypeStats().get("type1").getDeleteCurrent(),
        equalTo(0l));

    assertThat(stats.getTotal().getGet().getCount(), equalTo(0l));
    // check get
    GetResponse getResponse = client().prepareGet("test1", "type1", "1").execute().actionGet();
    assertThat(getResponse.isExists(), equalTo(true));

    stats = client().admin().indices().prepareStats().execute().actionGet();
    assertThat(stats.getTotal().getGet().getCount(), equalTo(1l));
    assertThat(stats.getTotal().getGet().getExistsCount(), equalTo(1l));
    assertThat(stats.getTotal().getGet().getMissingCount(), equalTo(0l));

    // missing get
    getResponse = client().prepareGet("test1", "type1", "2").execute().actionGet();
    assertThat(getResponse.isExists(), equalTo(false));

    stats = client().admin().indices().prepareStats().execute().actionGet();
    assertThat(stats.getTotal().getGet().getCount(), equalTo(2l));
    assertThat(stats.getTotal().getGet().getExistsCount(), equalTo(1l));
    assertThat(stats.getTotal().getGet().getMissingCount(), equalTo(1l));

    // clear all
    stats =
        client()
            .admin()
            .indices()
            .prepareStats()
            .setDocs(false)
            .setStore(false)
            .setIndexing(false)
            .setFlush(true)
            .setRefresh(true)
            .setMerge(true)
            .clear() // reset defaults
            .execute()
            .actionGet();

    assertThat(stats.getTotal().getDocs(), nullValue());
    assertThat(stats.getTotal().getStore(), nullValue());
    assertThat(stats.getTotal().getIndexing(), nullValue());
    assertThat(stats.getTotal().getGet(), nullValue());
    assertThat(stats.getTotal().getSearch(), nullValue());
  }
  @Test
  public void testMergeStats() {
    // rely on 1 replica for this tests
    createIndex("test1");

    ClusterHealthResponse clusterHealthResponse =
        client()
            .admin()
            .cluster()
            .prepareHealth()
            .setWaitForEvents(Priority.LANGUID)
            .setWaitForGreenStatus()
            .execute()
            .actionGet();
    assertThat(clusterHealthResponse.isTimedOut(), equalTo(false));

    // clear all
    IndicesStatsResponse stats =
        client()
            .admin()
            .indices()
            .prepareStats()
            .setDocs(false)
            .setStore(false)
            .setIndexing(false)
            .setFlush(true)
            .setRefresh(true)
            .setMerge(true)
            .clear() // reset defaults
            .execute()
            .actionGet();

    assertThat(stats.getTotal().getDocs(), nullValue());
    assertThat(stats.getTotal().getStore(), nullValue());
    assertThat(stats.getTotal().getIndexing(), nullValue());
    assertThat(stats.getTotal().getGet(), nullValue());
    assertThat(stats.getTotal().getSearch(), nullValue());

    for (int i = 0; i < 20; i++) {
      client()
          .prepareIndex("test1", "type1", Integer.toString(i))
          .setSource("field", "value")
          .execute()
          .actionGet();
      client()
          .prepareIndex("test1", "type2", Integer.toString(i))
          .setSource("field", "value")
          .execute()
          .actionGet();
      client().admin().indices().prepareFlush().execute().actionGet();
    }
    client()
        .admin()
        .indices()
        .prepareOptimize()
        .setWaitForMerge(true)
        .setMaxNumSegments(1)
        .execute()
        .actionGet();
    stats = client().admin().indices().prepareStats().setMerge(true).execute().actionGet();

    assertThat(stats.getTotal().getMerge(), notNullValue());
    assertThat(stats.getTotal().getMerge().getTotal(), greaterThan(0l));
  }