https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html
2.1 索引名规范
索引命名有如下限制:
- 仅限小写字母
- 不能包含
\
、/
、*
、?
、"
、<
、>
、|
、#以及空格符等特殊符号 - 从7.0版本开始不再包含冒号
- 不能以
-
、_
或+
开头 - 不能超过255个字节(注意它是字节,因此多字节字符将计入255个限制)
2.2 新建索引
(1)索引名小写
PUT test
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "test"
}
(2)索引名不能包含大些字母
PUT Blog
相应结果:
{
"error": {
"root_cause": [
{
"type": "invalid_index_name_exception",
"reason": "Invalid index name [Blog], must be lowercase",
"index_uuid": "_na_",
"index": "Blog"
}
],
"type": "invalid_index_name_exception",
"reason": "Invalid index name [Blog], must be lowercase",
"index_uuid": "_na_",
"index": "Blog"
},
"status": 400
}
将索引名修改为小写,即可完成创建
put blog
(3)重复创建索引
PUT test
相应结果:
{
"error": {
"root_cause": [
{
"type": "resource_already_exists_exception",
"reason": "index [test/XBKm9hVxSQGP6KTncA10WA] already exists",
"index_uuid": "XBKm9hVxSQGP6KTncA10WA",
"index": "test"
}
],
"type": "resource_already_exists_exception",
"reason": "index [test/XBKm9hVxSQGP6KTncA10WA] already exists",
"index_uuid": "XBKm9hVxSQGP6KTncA10WA",
"index": "test"
},
"status": 400
}
2.3 索引配置
创建索引时,可以制定相关设置,比如设置索引的分片数number_of_shards和副本数number_of_replicas
PUT twitter
{
"settings" : {
"index" : {
"number_of_shards" : 2,
"number_of_replicas" : 2
}
}
}
也可以简化为
PUT twitter
{
"settings" : {
"number_of_shards" : 2,
"number_of_replicas" : 2
}
}
也就是说,不必在settings部分中明确指定索引部分。
相应结果:
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "twitter"
}
2.4 查看索引
(1)查看制定索引
GET twitter
相应结果:
{
"twitter" : {
"aliases" : { },
"mappings" : { },
"settings" : {
"index" : {
"creation_date" : "1546851277179",
"number_of_shards" : "2",
"number_of_replicas" : "2",
"uuid" : "cibrmh7gQiuqIzpRmBvw0A",
"version" : {
"created" : "7000099"
},
"provided_name" : "twitter"
}
}
}
}
(2)查看索引列表
GET /_cat/indices?v
相应结果如下,可以看到两个刚刚创建的索引,.kibana_1
是Kibana自带样例索引。
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open twitter ulZQ6W22Tyi-MEWXC2EJbw 2 2 0 0 460b 460b
yellow open blog afqWzikqQiSqTEFm-RxcAg 1 1 0 0 283b 283b
green open .kibana_1 spncL8ZaRPmrfKKMOcz12Q 1 0 3 0 12kb 12kb
yellow open test XBKm9hVxSQGP6KTncA10WA 1 1 0 0 283b 283b
需要注意:我们新建的索引,默认分片和副本都是1。
(3)判定索引是否存在
HEAD twitter
相应结果:
200 - OK
2.5 更新副本数
PUT twitter/_settings
{
"number_of_replicas": 3
}
相应结果:
{
"acknowledged" : true
}
2.6 查看索引配置信息
GET twitter/_settings
相应结果:
{
"twitter" : {
"settings" : {
"index" : {
"creation_date" : "1546849909717",
"number_of_shards" : "2",
"number_of_replicas" : "3",
"uuid" : "ulZQ6W22Tyi-MEWXC2EJbw",
"version" : {
"created" : "7000099"
},
"provided_name" : "twitter"
}
}
}
}
2.7 删除索引
可以直接使用DELETE命令删除索引
DELETE blog
相应结果:
{
"acknowledged" : true
}
2.8 索引的关闭与打开
一个关闭的索引几乎不占用系统资源。我们可以临时关闭某个索引,在需要时再重新打开该索引。
(1)关闭twitter
POST twitter/_close
相应结果:
{
"acknowledged" : true
}
(2)查看索引
GET /_cat/indices?v
相应结果:
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open .kibana_1 spncL8ZaRPmrfKKMOcz12Q 1 0 3 0 12kb 12kb
close twitter 3vN_0m2fRAC2-aSzHPhitA
yellow open test XBKm9hVxSQGP6KTncA10WA 1 1 0 0 283b 283b
(3)重新打开twitter
POST twitter/_open
相应结果:
{
"acknowledged" : true,
"shards_acknowledged" : true
}
2.9 指定type的mapping
创新索引时,允许提供一个type的映射
(1)先删除test索引
DELETE test
(2)创新创建索引test,并制定默认_doc类型的mappings
PUT test
{
"settings" : {
"number_of_shards" : 1
},
"mappings" : {
"_doc" : {
"properties" : {
"field1" : { "type" : "text" }
}
}
}
}
相应结果:
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "test"
}
2.10 索引别名
索引别名不仅仅可以关联一个索引,它能聚合多个索引。此外,一个别名也可以与一个过滤器相关联, 这个过滤器在搜索和路由的时候被自动应用。
(1)创建多个索引
PUT index1
PUT index2
(2)创建index1的别名alias1
POST _aliases
{
"actions": [
{
"add": {
"index": "index1",
"alias": "alias1"
}
}
]
}
{
"acknowledged" : true
}
说明:此时别名alias1和index1一对一。
(3)添加多个索引的别名
POST _aliases
{
"actions": [
{
"add": {
"indices": ["index1","index2","test"],
"alias": "alias1"
}
}
]
}
{
"acknowledged" : true
}
说明:我们是不能对alias1进行写操作,当有多个索引时的别名,不能区分到底操作哪一个索引。
(4)移除别名
POST _aliases
{
"actions": [
{
"remove": {
"index": "test",
"alias": "alias1"
}
}
]
}
{
"acknowledged" : true
}
(5)查看别名
GET alias1
{
"index1" : {
"aliases" : {
"alias1" : { }
},
"mappings" : { },
"settings" : {
"index" : {
"creation_date" : "1546852808887",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "m6zs2f3-RfWfq_uX9uSvvw",
"version" : {
"created" : "7000099"
},
"provided_name" : "index1"
}
}
},
"index2" : {
"aliases" : {
"alias1" : { }
},
"mappings" : { },
"settings" : {
"index" : {
"creation_date" : "1546852810604",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "wer0Goo6S2KrBPVIUbQ0wA",
"version" : {
"created" : "7000099"
},
"provided_name" : "index2"
}
}
}
}