Skip to content

索引基本操作

创建

PUT /<INDEX_NAME>
PUT {{host}}/product1 HTTP/1.1
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-encoding: gzip
content-length: 75

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

ES 中的索引有一个健康状态 health,有三个状态:

  • green: 健康
  • yellow: 索引可用,但存在风险
  • red: 索引不可用

Warning

ES 默认在创建索引时为索引创建一个备份索引和一个 Primary 索引,可以通过创建时配置

1
2
3
4
5
6
7
8
PUT /<INDEX_NAME>

{
    "settings": {
        "number_of_shards": 1,      // 指定主分片的数量
        "number_of_replicas": 0     // 指定副本分片的数量
    }
}
1
2
3
4
5
6
7
8
9
PUT {{host}}/product2 HTTP/1.1
content-type: application/json

{
    "settings": {
        "number_of_shards": 1,
        "number_of_replicas": 0
    }
}
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-encoding: gzip
content-length: 75

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

查看

查看所有索引

GET /_cat/indices

Example

GET {{host}}/_cat/indices HTTP/1.1
1
2
3
4
5
6
7
HTTP/1.1 200 OK
content-type: text/plain; charset=UTF-8
content-encoding: gzip
content-length: 123

green  open product2 eJAfIwVySY2FXrlCpi2GUg 1 0 0 0 230b 230b
yellow open product1 0bYGE7asQgaxgVqpIDMiCQ 1 1 0 0 283b 283b

查看的时候带上标题

GET /_cat/indices?v

Example

GET {{host}}/_cat/indices?v HTTP/1.1
1
2
3
4
5
6
7
8
HTTP/1.1 200 OK
content-type: text/plain; charset=UTF-8
content-encoding: gzip
content-length: 193

health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   product2 eJAfIwVySY2FXrlCpi2GUg   1   0          0            0       283b           283b
yellow open   product1 0bYGE7asQgaxgVqpIDMiCQ   1   1          0            0       283b           283b

查看指定索引

查看全部信息:

GET /<INDEX_NAME>

Example

GET {{host}}/product2 HTTP/1.1
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-encoding: gzip
content-length: 225

{
    "product2": {
        "aliases": {},
        "mappings": {
            "properties": {
                "description": {
                    "type": "text"
                },
                "title": {
                    "type": "text"
                }
            }
        },
        "settings": {
            "index": {
                "creation_date": "1657988444815",
                "number_of_shards": "1",
                "number_of_replicas": "0",
                "uuid": "XtoUs9lqQ8eHX8uYaGOonw",
                "version": {
                    "created": "7010199"
                },
                "provided_name": "product2"
            }
        }
    }
}

查看索引的设置:

GET /<INDEX_NAME>/_settings

Example

GET {{host}}/product2/_settings HTTP/1.1
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-encoding: gzip
content-length: 180

{
    "product2": {
        "settings": {
            "index": {
                "creation_date": "1657988444815",
                "number_of_shards": "1",
                "number_of_replicas": "0",
                "uuid": "XtoUs9lqQ8eHX8uYaGOonw",
                "version": {
                    "created": "7010199"
                },
                "provided_name": "product2"
            }
        }
    }
}

查看mappings信息:

GET /<INDEX_NAME>/_mappings

Example

GET {{host}}/product2/_mappings HTTP/1.1
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-encoding: gzip
content-length: 97

{
    "product2": {
        "mappings": {
            "properties": {
                "description": {
                    "type": "text"
                },
                "title": {
                    "type": "text"
                }
            }
        }
    }
}

删除

DELETE /<INDEX_NAME>

Example

DELETE {{host}}/product2 HTTP/1.1
1
2
3
4
5
6
7
8
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-encoding: gzip
content-length: 47

{
    "acknowledged": true
}

更新

ES 中索引只有创建、查看、删除,没有更新操作