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"
            }
        }
    }
}

java怎么获取es全部索引名称 elasticsearch获取所有索引名称_java怎么获取es全部索引名称

2. 添加数据

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

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

java怎么获取es全部索引名称 elasticsearch获取所有索引名称_java怎么获取es全部索引名称_02

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

java怎么获取es全部索引名称 elasticsearch获取所有索引名称_nosql_03

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

java怎么获取es全部索引名称 elasticsearch获取所有索引名称_json_04

名称

解释

health

健康状况,分为绿黄红。

status

状态,打开还是关闭。

index

索引名称。

uuid

uuid,不解释。

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