ElasticSearch之Create index API

发布时间 2023-12-02 15:27:21作者: jackieathome

创建指定名称的index

命令样例如下:

curl -X PUT "https://localhost:9200/testindex_002?pretty" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "index": {
      "number_of_shards": 3,
      "number_of_replicas": 2
    }
  }
}
' --cacert $ES_HOME/config/certs/http_ca.crt -u "elastic:ohCxPH=QBE+s5=*lo7F9"

或者

curl -X PUT "https://localhost:9200/testindex_002?pretty" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 2
  }
}
' --cacert $ES_HOME/config/certs/http_ca.crt -u "elastic:ohCxPH=QBE+s5=*lo7F9"

执行结果的样例,如下:

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

index的名称
有如下规范:

  • 只允许使用小写字母。
  • 不允许使用特殊字符,如下:
    • \
    • /
    • *
    • ?
    • "
    • <
    • >
    • |
    • 空格
    • ,
    • #
    • 仅在7.0之前的版本,允许使用:
  • 不允许使用如下字符作为开头:
    • -
    • _
    • +
  • 不允许使用...
  • 名称的长度小于255个字节,注意不是字符。
  • 使用.作为开头的index,仅供ElasticSearch内部使用。

方法参数
wait_for_active_shards,默认值为1,即主分片。
可能取值为all或者大于0小于number_of_replicas+1的正整数,表示等待收到响应的分片的数量。

命令样例如下:

curl -X PUT "https://localhost:9200/testindex_003?pretty" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "index.number_of_shards": 3,
    "index.number_of_replicas": 2,
    "index.write.wait_for_active_shards": "2"
  }
}
' --cacert $ES_HOME/config/certs/http_ca.crt -u "elastic:ohCxPH=QBE+s5=*lo7F9"

或者

curl -X PUT "https://localhost:9200/testindex_003?wait_for_active_shards=2&pretty" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "index.number_of_shards": 3,
    "index.number_of_replicas": 2
  }
}
' --cacert $ES_HOME/config/certs/http_ca.crt -u "elastic:ohCxPH=QBE+s5=*lo7F9"

执行结果的样例,如下:

{
  "acknowledged" : true,
  "shards_acknowledged" : false,
  "index" : "testindex_003"
}

方法的请求消息体
在创建index时,允许指定:

  • index的工作参数。
  • index的字段的映射及参数。
  • index的别名。

方法的响应消息体
acknowledged
在超时前,创建请求的结果。

  • true,创建成功。
  • false,在超时前,未能完成创建,但创建操作并未停止。

shards_acknowledged
在超时前,创建请求的结果。

  • true,各分片均返回创建成功。
  • false,在超时前,未能完成创建,但创建操作并未停止。

相关资料