Elasticsearch(009):es中index(索引)的新增、修改、删除、关闭等操作

索引(Index)

本篇文章主要学习索引的相关操作。

1. 添加索引

PUT example
{
    "settings" : {
        "index" : {
            "number_of_shards" : 2, #设置分片的数量,在集群中通常设置多个分片,表示一个索引库将拆分成多片分别存储不同的结点,提高了ES的处理能力和高可用性,这里设置为2。
            "number_of_replicas" : 1 #设置副本的数量,设置副本是为了提高ES的高可靠性,这里设置成设置为1
        }
    }
}

返回值

{
  "acknowledged" : true, #表示创建成功
  "shards_acknowledged" : true,
  "index" : "example"
}

当然还有不止一个参数针对Index,更多的可以参考这里。 todo

2. 获取索引

GET example

返回值

{
  "example" : {
    "aliases" : { },
    "mappings" : { },
    "settings" : {
      "index" : {
        "creation_date" : "1573387465030",
        "number_of_shards" : "2",
        "number_of_replicas" : "1",
        "uuid" : "yw-ZmC4ATjeukZb6N-ub8A",
        "version" : {
          "created" : "6050499"
        },
        "provided_name" : "example"
      }
    }
  }
}

上面的示例获取名为的索引的信息example。需要指定索引,别名或通配符表达式。

通过使用_all*作为索引,get index API也可以应用于多个索引,或者应用于所有索引

3. 修改索引

修改example索引的max_result_window的值,调大一些。默认是10000。使用ES的人肯定知道其分页在超过10000条数据后会报错

Result window is too large, from + size must be less than or equal to: [10000] but was [78020]. See the scroll api for a more efficient way to request large data sets. This limit can be set by changing the [index.max_result_window] index level setting.

所以这里我们以此为例来动态修改其值,来解决这个问题

PUT example/_settings
{
  "index.max_result_window": 1000000000
}

返回结果

{
  "acknowledged" : true
}

我们通过刚刚的查询方法,获取索引,来查看我们的修改操作是否已经生效。

4. 删除索引

DELETE example

返回结果

{
  "acknowledged" : true
}

这时我们通过查询索引方法就会报错。如下

{
  "error" : {
    "root_cause" : [
      {
        "type" : "index_not_found_exception",
        "reason" : "no such index",
        "index_uuid" : "_na_",
        "resource.type" : "index_or_alias",
        "resource.id" : "example",
        "index" : "example"
      }
    ],
    "type" : "index_not_found_exception",
    "reason" : "no such index",
    "index_uuid" : "_na_",
    "resource.type" : "index_or_alias",
    "resource.id" : "example",
    "index" : "example"
  },
  "status" : 404
}

5. 打开/关闭索引

打开和关闭索引API允许先关闭索引,然后再打开索引。封闭索引几乎没有集群开销(除了维护其元数据),并且被禁止进行读/写操作。可以打开一个封闭的索引,然后将通过正常的恢复过程。

REST端点为/{index}/_close/{index}/_open。例如

#关闭索引
POST /example/_close

返回值

{
  "acknowledged" : true
}
#打开索引
POST /example/_open

返回值

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

6. 获取所有索引列表

#获取所有索引列表
GET _all

其实创建索引时也可以同时创建映射,也可以后面添加创建。我们下一小节将研究映射的问题。

发布了158 篇原创文章 · 获赞 147 · 访问量 27万+

猜你喜欢

转载自blog.csdn.net/weixin_39723544/article/details/103825051