ElasticSearch之Create index API

创建指定名称的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,在超时前,未能完成创建,但创建操作并未停止。

相关资料

posted @   jackieathome  阅读(24)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示