zl程序教程

您现在的位置是:首页 >  工具

当前栏目

【ES三周年】- Elasticsearch索引的创建、查询和删除

ESelasticsearch索引 查询 删除 创建 三周年
2023-06-13 09:17:10 时间

启动Elasticsearch

进入bin目录,双击elasticsearch.bat运行。

在这里插入图片描述
在这里插入图片描述

Elasticsearch启动后会暴露两个端口:

  • 9300 端口为 Elasticsearch 集群间组件的通信端口
  • 9200 端口为浏览器访问的 http协议 RESTful 端口。

索引操作

Postman

Postman是一个接口测试工具。

在做接口测试的时候,Postman相当于一个客户端,它可以模拟用户发起的各类HTTP请求,将请求数据发送至服务端,获取对应的响应结果, 从而验证响应中的结果数据是否和预期值相匹配;并确保开发人员能够及时处理接口中的bug,进而保证产品上线之后的稳定性和安全性。

它主要是用来模拟各种HTTP请求的(如:get/post/delete/put..等等),Postman与浏览器的区别在于有的浏览器不能输出Json格式,而Postman更直观接口返回的结果。

创建索引

在Elasticsearch中创建索引就相当于在关系型数据库中创建数据库。

进入postman中,向Elasticsearch服务器发送PUT请求,即创建一个索引。例如:

向ES服务器发送 PUT请求:localhost:9200/new_index

在这里插入图片描述

得到响应结果:

{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "new_index"
}

PUT请求:localhost:9200/new_index,表示在Elasticsearch中创建一个名为“new_index”。

==注意1:Elasticsearch中的索引不能使用大写字母==

如果使用了大写字母就会得到如下的错误响应信息:

{
    "error": {
        "root_cause": [
            {
                "type": "invalid_index_name_exception",
                "reason": "Invalid index name [new_indeX], must be lowercase",
                "index_uuid": "_na_",
                "index": "new_indeX"
            }
        ],
        "type": "invalid_index_name_exception",
        "reason": "Invalid index name [new_indeX], must be lowercase",
        "index_uuid": "_na_",
        "index": "new_indeX"
    },
    "status": 400
}

==注意2:Elasticsearch中不能重复发送相同的PUT请求,即不能重复创建相同索引。==

如果重复创建相同索引就会得到如下的错误响应信息:

{
    "error": {
        "root_cause": [
            {
                "type": "resource_already_exists_exception",
                "reason": "index [new_index/U_T66D1VSg-o0wrWxVgKSw] already exists",
                "index_uuid": "U_T66D1VSg-o0wrWxVgKSw",
                "index": "new_index"
            }
        ],
        "type": "resource_already_exists_exception",
        "reason": "index [new_index/U_T66D1VSg-o0wrWxVgKSw] already exists",
        "index_uuid": "U_T66D1VSg-o0wrWxVgKSw",
        "index": "new_index"
    },
    "status": 400
}

查询索引信息

1. 查询指定索引信息

在这里插入图片描述

向ES服务器发送 GET请求:localhost:9200/new_index。表示查询名为“new_index”的索引信息,将返回响应结果:

{
    "new_index": {
        "aliases": {},
        "mappings": {},
        "settings": {
            "index": {
                "creation_date": "1653894682108",
                "number_of_shards": "1",
                "number_of_replicas": "1",
                "uuid": "U_T66D1VSg-o0wrWxVgKSw",
                "version": {
                    "created": "7080099"
                },
                "provided_name": "new_index"
            }
        }
    }
}

2. 查询所有索引

向ES服务器发送 GET请求:localhost:9200/_cat/indices?v即可查询ES服务中的所有索引。

在这里插入图片描述

其中_cat 表示查看, indices 表示索引,这就相当于MySQL中的show tables。

响应结果中表头的含义如下:

表头

含义

health

健康状态: green(集群完整)、 yellow(单点正常、集群不完整)、 red(单点不正常)

status

索引状态

index

索引名

uuid

索引编号

pri

主分片数量

rep

副本数量

docs.count

可用文档数量

docs.deleted

文档删除状态

store.size

整体所占空间大小

pri.store.size

主分片所占空间大小

删除索引

向ES服务器发送 DELETE请求:localhost:9200/new_index,表示删除名为“new_index”的索引。

在这里插入图片描述

再次发送GET请求:localhost:9200/_cat/indices?v,“new_index”索引已不存在,删除成功。

在这里插入图片描述