我一直在尝试通过 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
在store
下你有size_in_bytes
使用
Java API
:IndexStatsResponse
是一个响应,如果您想阅读它,必须将其转换为 JSON。您可以使用gson
来解析te 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 - 可以通过 Java API 获取 Elasticsearch 索引的大小吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32083230/