ElasticSearch获取Index的document数量

ElasticSearch可以通过/_cat/indices来查看Index的状态

GET _cat/indices/your_index_name*?v

health status index                            uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   your-index-name-20210630_041604 tncQI-oBQxu7Y6OgdoNe2Q   1   2       7896            0     15.2mb          5.2mb
green  open   your-index-name-20210630_072026 JcNRNxnHQt2VS6Ld2PIMBQ   1   2     650061            0      1.2gb          437mb
green  open   your-index-name-20210518_102722 4toZnuRmT1akGfwT7Rd-Cg   1   2     651661            0      1.3gb        446.5mb
green  open   your-index-name-20210630_075626 DhPg8W8yQjK_OwT-HQ1OEA   1   2      58255            0    128.7mb         42.9mb

在Response中有docs.count等信息,但此处的docs.count不是document的数量,而是Lucene的文档数。当docs中存在nest的类型时,docs.count != Index的doc数。

通过_count API来查看Index的document数量:

GET your-index-name-20210630_075626/_count

{
    "count": 6000,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    }
}

可以看出Index的document是6000,但 docs.count是58255。原因是Index中存在nested类型

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Elasticsearch 的 Java 客户端中,有一个名为 `org.elasticsearch.client.document.DocumentClient` 的类,用于执行与文档相关的操作。这个类提供了一系列方法,可以用来索引、获取、更新和删除文档。下面是一个简单的示例: ```java import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentType; public class DocumentExample { private RestHighLevelClient client; public DocumentExample(RestHighLevelClient client) { this.client = client; } public void indexDocument(String index, String id, String json) throws IOException { IndexRequest request = new IndexRequest(index) .id(id) .source(json, XContentType.JSON); IndexResponse response = client.index(request, RequestOptions.DEFAULT); System.out.println(response); } public void getDocument(String index, String id) throws IOException { GetRequest request = new GetRequest(index, id); GetResponse response = client.get(request, RequestOptions.DEFAULT); System.out.println(response); } public void updateDocument(String index, String id, String json) throws IOException { UpdateRequest request = new UpdateRequest(index, id) .doc(json, XContentType.JSON); UpdateResponse response = client.update(request, RequestOptions.DEFAULT); System.out.println(response); } public void deleteDocument(String index, String id) throws IOException { DeleteRequest request = new DeleteRequest(index, id); DeleteResponse response = client.delete(request, RequestOptions.DEFAULT); System.out.println(response); } } ``` 以上示例展示了如何使用 `DocumentClient` 类来执行索引、获取、更新和删除文档的操作。你可以根据自己的需求调用这些方法,并根据需要处理相应的响应。请注意,需要先创建一个 `RestHighLevelClient` 实例,并将其传递给 `DocumentExample` 的构造函数中,以便与 Elasticsearch 集群进行交互。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值