Elasticsearch-核心篇(3)-索引操作

1,638 阅读2分钟

一、索引新增

参考文档www.elastic.co/guide/en/el… 注意:索引库名称必须是小写,否则将会报错

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

操作说明

  • 对比关系型数据库,创建索引就等同于创建数据库
  • 注意:创建索引库的分片数默认 1 片,在 7.0.0 之前的 Elasticsearch 版本中,默认 5 片

命令示例

PUT es

返回示例

{
	"acknowledged"【响应结果】: true,
	"shards_acknowledged"【分片结果】: true,
	"index"【索引名称】: "es"
}

错误说明

  1. 重复创建将会报错
{
	"error": {
		"root_cause": [
			{
				"type": "resource_already_exists_exception",
				"reason": "index [es/5sI71aL8SzC1HWOyyt98vA] already exists",
				"index_uuid": "5sI71aL8SzC1HWOyyt98vA",
				"index": "es"
			}
		],
		"type": "resource_already_exists_exception",
		"reason": "index [es/5sI71aL8SzC1HWOyyt98vA] already exists",
		"index_uuid": "5sI71aL8SzC1HWOyyt98vA",
		"index": "es"
	},
	"status": 400
}

二、索引删除

2.1 单索引删除

操作说明

  • 删除指定索引
  • 删除具备幂等,多次删除都是返回true

命令示例

DELETE es

返回示例

  • acknowledged:响应结果
{
	"acknowledged": true
}

错误说明

  1. 删除不存在索引将会报错
{
	"error": {
		"root_cause": [
			{
				"type": "index_not_found_exception",
				"reason": "no such index [es2]",
				"resource.type": "index_or_alias",
				"resource.id": "es2",
				"index_uuid": "_na_",
				"index": "es2"
			}
		],
		"type": "index_not_found_exception",
		"reason": "no such index [es2]",
		"resource.type": "index_or_alias",
		"resource.id": "es2",
		"index_uuid": "_na_",
		"index": "es2"
	},
	"status": 404
}

2.2 多索引删除

  1. 在单个索引删除的基础上行使用逗号分隔多个需要删除的索引即可
DELETE product,springboot,customer
  1. 返回示例
{
	"acknowledged": true
}

三、索引查询

3.1 查看单个索引

操作说明

  • 查看单个索引信息

命令示例

GET es

返回示例

{
    "es"【索引名】: {        
        "aliases"【别名】: {},
        "mappings"【映射】: {},
        "settings"【设置】: {
            "index"【设置 - 索引】: {
                "creation_date"【设置 - 索引 - 创建时间】: "1622879605711",
                "number_of_shards"【设置 - 索引 - 主分片数量】: "1",
                "number_of_replicas"【设置 - 索引 - 副分片数量】: "1",
                "uuid"【设置 - 索引 - 唯一标识】: "iXRlOMczQWCDHqJLH0gOBA",
                "version"【设置 - 索引 - 版本】: {
                    "created": "7080099"
                },
                "provided_name"【设置 - 索引 - 名称】: "es"
            }
        }
    }
}

错误说明

  1. 索引不存在时将会抛出404
{
	"error": {
		"root_cause": [
			{
				"type": "index_not_found_exception",
				"reason": "no such index [es2]",
				"resource.type": "index_or_alias",
				"resource.id": "es2",
				"index_uuid": "_na_",
				"index": "es2"
			}
		],
		"type": "index_not_found_exception",
		"reason": "no such index [es2]",
		"resource.type": "index_or_alias",
		"resource.id": "es2",
		"index_uuid": "_na_",
		"index": "es2"
	},
	"status": 404
}

3.2 查看所有索引

操作说明

  • 可以通过内置_cat命令查看所有索引信息

Query参数

参数名参数类型是否必填参数描述
vString是否包含参数头说明

命令示例

GET _cat/indices

返回示例

health status index uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   es    iXRlOMczQWCDHqJLH0gOBA   1   1          0            0       208b           208b
  • 字段说明 | 表头 | 含义 | | --- | --- | | health | 当前服务器健康状态: green(集群完整) yellow(单点正常、集群不完整) red(单点不正常) | | status | 索引打开、关闭状态 | | index | 索引名 | | uuid | 索引统一编号 | | pri | 主分片数量 | | rep | 副本数量 | | docs.count | 可用文档数量 | | docs.deleted | 文档删除状态(逻辑删除) | | store.size | 主分片和副分片整体占空间大小 | | pri.store.size | 主分片占空间大小 |