+-
可以通过Java API获得Elasticsearch索引的大小吗?
我一直在尝试通过 Java API来获取我的Elasticsearch索引的大小,而我却找不到正确的调用来完成此任务.我发现了一些类似的建议( https://groups.google.com/forum/#!topic/elasticsearch/jNCjCqAS1us),但这是从2012年开始的,似乎不再有意义.

我已经可以通过以下方式获得IndicesStatsResponse:

IndicesStatsResponse response = client.admin().indices()
        .prepareStats(makeIndexName(tenant.getId()))
        .clear()
        .setStore(true)
        .execute()
        .actionGet();

但是从这一点来看,我找不到所需的信息.这可能吗?

最佳答案
可以使用 Stats API访问索引统计信息.

>使用cURL:curl -XGET localhost:9200 / index_name / _stats?pretty = true,在存储区下您具有size_in_bytes
>使用Java API:IndexStatsResponse是一个响应,如果要读取它,则必须将其转换为JSON.您可以使用gson解析Json.

    IndicesStatsResponse indicesStatsResponse = StartTCPService.getClient()
            .admin()
            .indices()
            .prepareStats(index_name)
            .all()
            .execute().actionGet();

    XContentBuilder builder = XContentFactory.jsonBuilder();
    builder.startObject();
    indicesStatsResponse.toXContent(builder, ToXContent.EMPTY_PARAMS);
    builder.endObject();
    String jsonResponse = builder.prettyPrint().string();

    JsonParser jsonParser = new JsonParser(); // from import com.google.gson.JsonParser;
    Long sizeOfIndex = jsonParser.parse(jsonResponse)
            .getAsJsonObject().get("_all")
            .getAsJsonObject().get("primaries")
            .getAsJsonObject().get("store")
            .getAsJsonObject().get("size_in_bytes").getAsLong();

    System.out.println(sizeOfIndex); // this is in Bytes
点击查看更多相关文章

转载注明原文:可以通过Java API获得Elasticsearch索引的大小吗? - 乐贴网