前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Elasticsearch 7.x:2、索引管理

Elasticsearch 7.x:2、索引管理

作者头像
程裕强
发布2019-05-27 09:07:07
2.6K0
发布2019-05-27 09:07:07
举报

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
}

(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 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"
}

2.4 查看索引

(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

2.5 更新副本数

PUT blog/_settings
{
  "number_of_replicas": 1
}

相应结果:

{
  "acknowledged" : true
}

2.6 查看索引配置信息

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"
      }
    }
  }
}

2.7 删除索引

可以直接使用DELETE命令删除索引

DELETE test

相应结果:

{
  "acknowledged" : true
}

2.8 索引的关闭与打开

一个关闭的索引几乎不占用系统资源。我们可以临时关闭某个索引,在需要时再重新打开该索引。 (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
}

2.9 指定type的mapping

创新索引时,允许提供一个type的映射。

创新创建索引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": ["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"
      }
    }
  }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年01月07日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 2.1 索引名规范
  • 2.2 新建索引
  • 2.3 索引配置
  • 2.4 查看索引
  • 2.5 更新副本数
  • 2.6 查看索引配置信息
  • 2.7 删除索引
  • 2.8 索引的关闭与打开
  • 2.9 指定type的mapping
  • 2.10 索引别名
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档