Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
可以通过Java API获得Elasticsearch索引的大小吗?_Java_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch - Fatal编程技术网 elasticsearch,Java,elasticsearch" /> elasticsearch,Java,elasticsearch" />

可以通过Java API获得Elasticsearch索引的大小吗?

可以通过Java API获得Elasticsearch索引的大小吗?,java,elasticsearch,Java,elasticsearch,我一直试图通过JavaAPI获得Elasticsearch索引的大小,但我没有找到正确的调用来实现这一点。我发现了一些类似的建议(),但它是从2012年开始的,似乎不再相关 我已经能够通过以下方式获得指示状态响应: IndicesStatsResponse response = client.admin().indices() .prepareStats(makeIndexName(tenant.getId())) .clear() .setSto

我一直试图通过JavaAPI获得Elasticsearch索引的大小,但我没有找到正确的调用来实现这一点。我发现了一些类似的建议(),但它是从2012年开始的,似乎不再相关

我已经能够通过以下方式获得
指示状态响应

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

但是从这一点上我找不到我需要的信息。这可能吗?

可以使用访问索引统计信息

  • 使用cURL:
    cURL-XGET localhost:9200/index\u name/\u stats?pretty=true
    存储
    下有
    大小(以字节为单位)

  • 使用
    JavaAPI
    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
    

使用
RestHighLevelClient
,使用es
7.1.0
进行测试:

RestHighLevelClient client = ...;

var resp = client.getLowLevelClient().performRequest(new Request("GET", "INDEX_NAME/_stats"));
var body =  new ObjectMapper().readTree(resp.getEntity().getContent());
var size = body.get("indices").get("INDEX_NAME").get("primaries").get("store").get("size_in_bytes");

我使用
Jackson
解析json。

非常感谢。这正是我要找的。indexName是包含更多信息的indexPlease编辑的名称。不鼓励只编写代码和“试试这个”答案,因为它们不包含可搜索的内容,也不解释为什么有人应该“试试这个”。我们努力成为知识的源泉,这是完美的。一旦我有了resp.getEntity().getContent(),我就有了我需要的所有信息,谢谢
RestHighLevelClient client = ...;

var resp = client.getLowLevelClient().performRequest(new Request("GET", "INDEX_NAME/_stats"));
var body =  new ObjectMapper().readTree(resp.getEntity().getContent());
var size = body.get("indices").get("INDEX_NAME").get("primaries").get("store").get("size_in_bytes");