ElasticSearch 7.x创建索引,获取全部索引,添加数据,获取索引别名,索引下文档数量等

  ElasticSearch也用了一段时间,索引建立好后,基本就很少去用表达式操作它了,目前一直都是直接用Java代码去进行ES的增删改查,需要手动操作的时候居然忘记怎么写了特此记录一下。

1. 创建索引(使用的是Postman,若使用Kibana就不需要host那段)
PUT 方法
192.168.**.**:9200/test_index_001
{
    "settings": {
        "number_of_shards": 3,
        "number_of_replicas": 1
    },
    "mappings": {
        "properties": {
            "age": {
                "type": "integer"
            },
            "name": {
                "type": "keyword"
            }
        }
    }
}

在这里插入图片描述

2. 添加数据

  对于POST操作方式,索引不存在,则创建索引

POST方法
192.168.**.**:9200/test_index_001/_doc
{
           "age": "15",
           "name": "你大爷"
}

在这里插入图片描述

3. 获取名称为索引下面文档数量
GET
192.168.**.**:9200/_cat/indices/索引名称?v

在Kibana 上为:
GET /_cat/indices/索引名称?v

Java代码实现获取索引下面文档的个数

    /** 请求高级客户端 **/
    @Autowired
    private RestHighLevelClient client;

    /**
     * @param indexName
     *            索引名称
     * @return 数量
     * @throws IOException
     *             异常
     */
    public Long docCount(String indexName) throws IOException {

        // 构建请求:org.elasticsearch.client.core.CountRequest
        CountRequest request = new CountRequest(indexName);

        // 响应结果
        CountResponse response = client.count(request, RequestOptions.DEFAULT);

        // 返回文档个数,还可以返回其他一些如分片信息,副本信息等,
        return response.getCount();
    }
4. 获取索引的字段信息
Postman上:
POST
192.168.**.**:9200/索引名称/_mapping
在kibana上
GET 索引名称/_mapping

在这里插入图片描述

5. 获取全部索引/获取合乎某些条件的索引
//?v可以不要,只作为显示,标题栏(只演示Kibana,Postman上一次样加上源端口就行)
GET _cat/indices/*test*?v

其中 两个 * 号就像MySQL的like查询中的%一样。

health status index                 uuid                   pri rep docs.count docs.deleted store.size pri.store.size
健康    状态  索引名称                uuid           主分片数   副本数    文档数     删除的文档数  索引总大小  主分片索引大小

green  open   kafka_test            zMHxfuuXTuyceaV_8kllvA   1   1          9            0       44kb           22kb
green  open   mytest_user           L5ys8k8dQ-mr098f9Qv_hw   1   1          5            1     12.8kb          6.4kb
green  open   test_index_001        mBL-tiTgSp6kdTWemVALzQ   3   1          8            0     20.5kb         10.2kb
green  open   my_test_index_008     7DCl5SDISZiHP75rqa8HTw   1   1         51            0     86.8kb         43.4kb
green  open   mytest_user_01        ArguAEgjRmiq3ctgP-WkUw   1   1          9            1     17.8kb          8.9kb
green  open   customer-account_test yj9uC5cvRjeyg85p60ij9Q   5   1     213644        72657    164.6mb         82.5mb
green  open   test_index_008        yHkG4Mm1TFaltPMERcvDVQ   3   1          0            0      1.6kb           849b
green  open   test_index_007        7JHvLODYR9qdlIc1TT8qog   3   1          0            0      1.6kb           849b

在这里插入图片描述

名称解释
health健康状况,分为绿黄红。
status状态,打开还是关闭。
index索引名称。
uuiduuid,不解释。
pri主分片数。
rep副本数。
docs.count该索引中现有的文档数。
docs.deleted索引删除的文档数。
store.size索引占据的总大小。
pri.store.size主分片占据的总大小。

Java代码获取合乎要求的索引

	/** 请求高级客户端 **/
    @Autowired
    private RestHighLevelClient client;
     /**
     * @return 索引列表
     */
    public List<String> listIndexs() {

        try {
            // 构建请求,注意*号的写法
            GetIndexRequest getIndexRequest = new GetIndexRequest("*test*00*");

            // 构建获取所有索引的请求:org.elasticsearch.client.indices.GetIndexRequest
            GetIndexResponse getIndexResponse = client.indices().get(getIndexRequest, RequestOptions.DEFAULT);

            // 获取所有的索引
            String[] indices = getIndexResponse.getIndices();

            // 转化为list形式
            List<String> asList = Arrays.asList(indices);

            // 复制一下,不然不能追加
            return new ArrayList<>(asList);
        } catch (Exception e) {

            LOG.error("获取所有索引失败:{}", e);
            throw new Exception(e);
        }
    }
6. 获取索引的别名
GET 索引名称/_alias
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值