[https://www.elastic.co/guide/en/elasticsearch/reference/6.7/index-lifecycle-management.html](https://www.elastic.co/guide/en/elasticsearch/reference/6.7/index-lifecycle-management.html)
[https://www.cnblogs.com/vijayfly/p/6763127.html](https://www.cnblogs.com/vijayfly/p/6763127.html)
[https://www.orchome.com/477](https://www.orchome.com/477)
## 索引基本操作
Elasticsearch 与传统的 SQL数据库的一个明显的不同点是,Elasticsearch 是一个 非结构化 的数据库,或者说是一个 无模式 的数据库。Elasticsearch 中数据最重要的三要素当属:索引、类型、文档,其中索引这个概念非常重要,我们可以粗略地将其类比到传统SQL数据库中的数据表。
kibana提供的dev tools来操作索引,点击kibanna左侧侧边栏找到Dev Tools
## es常用操作
想要查询某集群下已存在的索引、文档数量,占用存储空间大小等信息,可使用如下命令:
```
GET _cat/indices?v
#或者
curl 10.75.5.38:32768/_cat/indices?v
curl 10.75.5.38:32768/_cat/indices?v > index.log
```
响应结果如图所示:
```
[root@k8sprodmaster01 ~]# curl 10.75.5.38:32768/_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open application-policygroup-2020.03.11 EshEe6b6RQ-gkBUqfgRbjw 5 1 66035129 0 85.2gb 42.6gb
green open wise2c_component-10.75.5.13:6444-2020.03.20 UfdPwk-YTM6WMma8daWFBQ 5 1 34530 0 53.2mb 26.6mb
```
返回的结果从左到右依次包括:pri(主分片数)、rep(副分片数)、docs.count(索引现有文档数)、docs.deleted(索引删除文档数)、store.size(索引总大小)、pri.store.size(索引主分片大小)。
也可以单独查询某一个索引下信息,如:想要查询索引test的相关信息:
```
GET _cat/indices/test?v
```
则此时响应结果只包含test的索引信息。
统计索引大小
```
curl 10.75.5.38:32768/_cat/indices?bytes=b | grep application* | awk '{print $9}' | paste -sd+ - | bc numfmt --to=iec-i
```
#### **查看所有的索引**
~~~swift
#查看所有索引
GET /_cat/indices?v
#查看某个索引
GET {index}/_stats
~~~
es返回内容,结果显示里面有2个索引分别是.kibana\_1,megacorp
~~~css
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open .kibana_1 B1GfSkleTF6cj5R4-c41rg 1 0 2 0 9.5kb 9.5kb
yellow open megacorp zdXsIWZtRMubzHXsiGq4Cw 5 1 5 0 28.2kb 28.2kb
~~~
#### **创建索引**
~~~undefined
PUT /{IndexName}?pretty
~~~
创建用户索引,在kibana的Console中输入命令
~~~undefined
PUT /user?pretty
~~~
ES返回
~~~json
#! Deprecation: the default number of shards will change from [5] to [1] in 7.0.0; if you wish to continue using the default of [5] shards, you must manage this on the create index request or with an index template
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "user"
}
~~~
> 翻译:弃用:默认的分片数将在7.0.0中从\[5\]更改为\[1\]; 如果您希望继续使用默认的\[5\]分片,则必须在创建索引请求或索引模板上进行管理。
我这里使用的是ES6.6已经是相对较新的版本了,这个问题我们暂时忽略,此时你可以使用`GET /_cat/indices?v`再次查看索引看看和之前有什么区别
#### 三:删除索引
~~~undefined
DELETE /{IndexName}?pretty
~~~
删除index
```
curl -XDELETE http://localhost:9200/test-2017-06
curl -DELETE http://es-host:9200/index-yyyy.mm*
```
#### 四:重索引和别名
**ES不能重命名索引名称,但是可以给索引取别名和重索引(reindex)**
**取别名语法**
~~~json
PUT /{OldIndexName}/_alias/{NewIndexName}
~~~
**测试**
1.往user索引插入文档
~~~json
PUT /user/introduce/1?pretty
{
"name" : "jack",
"age" : 20,
"gender" : "male"
}
~~~
2.取别名
~~~undefined
PUT /user/_alias/users
~~~
3.查看索引数据
~~~ruby
查users索引:GET /users/_search
查user索引:GET /user/_search
~~~
2次返回的结果都是一样的
~~~json
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "user",
"_type" : "introduce",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "jack",
"age" : 20,
"gender" : "male"
}
}
]
}
}
~~~