索引命名有如下限制:
\
、/
、 *
、?
、"
、<
、>
、|
、#以及空格符等特殊符号-
、_
或+
开头(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
}
(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
}
创建索引时,可以制定相关设置,比如设置索引的分片数number_of_shards和副本数number_of_replicas
PUT blog
{
"settings" : {
"index" : {
"number_of_shards" : 2,
"number_of_replicas" : 2
}
}
}
也可以简化为
PUT blog
{
"settings" : {
"number_of_shards" : 2,
"number_of_replicas" : 2
}
}
也就是说,不必在settings部分中明确指定索引部分。
相应结果:
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "blog"
}
(1)查看制定索引
GET blog
相应结果:
{
"blog" : {
"aliases" : { },
"mappings" : { },
"settings" : {
"index" : {
"creation_date" : "1547012455291",
"number_of_shards" : "2",
"number_of_replicas" : "2",
"uuid" : "RorVlzACQIOpGDCW9LJ1cA",
"version" : {
"created" : "6050499"
},
"provided_name" : "blog"
}
}
}
}
(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 blog RorVlzACQIOpGDCW9LJ1cA 2 2 0 0 522b 522b
green open .kibana_1 vfaeT7wQSgyxXOlZioCFDQ 1 0 3 0 11.9kb 11.9kb
yellow open test k4WAeNOqSpGzQbO3euWy2w 1 1 0 0 230b 230b
需要注意:我们新建的索引,默认分片和副本都是1。
(3)判定索引是否存在
HEAD blog
相应结果:
200 - OK
PUT blog/_settings
{
"number_of_replicas": 1
}
相应结果:
{
"acknowledged" : true
}
GET blog/_settings
相应结果:
{
"blog" : {
"settings" : {
"index" : {
"creation_date" : "1547012455291",
"number_of_shards" : "2",
"number_of_replicas" : "1",
"uuid" : "RorVlzACQIOpGDCW9LJ1cA",
"version" : {
"created" : "6050499"
},
"provided_name" : "blog"
}
}
}
}
可以直接使用DELETE命令删除索引
DELETE test
相应结果:
{
"acknowledged" : true
}
一个关闭的索引几乎不占用系统资源。我们可以临时关闭某个索引,在需要时再重新打开该索引。 (1)关闭blog
POST blog/_close
相应结果:
{
"acknowledged" : true
}
(2)查看索引
GET /_cat/indices?v
相应结果:
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
close blog RorVlzACQIOpGDCW9LJ1cA
green open .kibana_1 vfaeT7wQSgyxXOlZioCFDQ 1 0 3 0 11.9kb 11.9kb
(3)重新打开blog
POST blog/_open
相应结果:
{
"acknowledged" : true,
"shards_acknowledged" : true
}
创新索引时,允许提供一个type的映射。
创新创建索引test,并制定默认_doc类型的mappings
PUT test
{
"settings" : {
"number_of_shards" : 1
},
"mappings" : {
"_doc" : {
"properties" : {
"field1" : { "type" : "text" }
}
}
}
}
相应结果:
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "test"
}
索引别名不仅仅可以关联一个索引,它能聚合多个索引。此外,一个别名也可以与一个过滤器相关联, 这个过滤器在搜索和路由的时候被自动应用。
(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": ["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" : "1547013859010",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "sbbArnOvTYqf07rXlTpFtA",
"version" : {
"created" : "6050499"
},
"provided_name" : "index1"
}
}
},
"index2" : {
"aliases" : {
"alias1" : { }
},
"mappings" : { },
"settings" : {
"index" : {
"creation_date" : "1547013863297",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "qqRkUXOcQfuBTf1eBqvMPA",
"version" : {
"created" : "6050499"
},
"provided_name" : "index2"
}
}
}
}