跳到主要内容

十六、Elasticsearch 教程: 索引 API

本章节我们要介绍的这些 API 用于管理索引的所有方面的内容,如设置,别名,映射,索引模板

创建索引

这个API 可以用来创建索引

当用户将 JSON 对象传递给任何索引时,可以自动创建索引

当然,也可以不传递任何数据,而是使用默认设置创建一个索引

要创建索引,只需要发送带有设置,映射和别名的 HTTP POST 请求,或者只是一个没有正文的简单请求

例如下面的请求使用默认设置创建一个索引

PUT http://localhost:9200/colleges?pretty

返回响应

{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "colleges"

或者发起请求的时候可以传递一个设置

PUT http://localhost:9200/colleges2?pretty

请求正文

{
"settings" : {
"index" : {
"number_of_shards" : 5, "number_of_replicas" : 3
}
}

返回响应

{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "colleges2"

传递的配置中还可以携带映射数据

PUT http://localhost:9200/colleges3?pretty

请求正文

{
"settings" : {
"number_of_shards" : 3
},
"mappings" : {
"type1" : {
"_source" : { "enabled" : false },
"properties" : {
"college_name" : { "type" : "string" },
"college_type" : {"type":"string"}
}
}
}