Elasticsearch 7.x:2、索引管理

版权声明:本文为博主原创文章,欢迎转载。 https://blog.csdn.net/chengyuqiang/article/details/86000472

https://www.elastic.co/guide/en/elasticsearch/reference/7.x/index.html
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
}

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

没有更多推荐了,返回首页