索引API用于管理单个索引,索引设置,别名,映射和索引模板。
https://www.elastic.co/guide/en/elasticsearch/reference/6.7/indices-rollover-index.html
1.Create Index 创建索引
Create Index API用于在Elasticsearch中手动创建索引。 Elasticsearch中的所有文档都存储在一个或另一个索引中。 最基本的命令如下:
PUT twitter --这将使用所有默认设置创建一个名为twitter的索引。
---------------------
PUT twitter
{
"settings" : {
"index" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
}
}
-------------------创建索引API允许提供类型映射:
PUT test
{
"settings" : {
"number_of_shards" : 1
},
"mappings" : {
"_doc" : {
"properties" : {
"field1" : { "type" : "text" }
}
}
}
}
--------------------------创建索引API还允许提供一组别名:
PUT test
{
"aliases" : {
"alias_1" : {},
"alias_2" : {
"filter" : {
"term" : {"user" : "kimchy" }
},
"routing" : "kimchy"
}
}
}
2. Delete Index 删除索引
DELETE /twitter
3. Get Index 获取索引信息
GET /twitter
类型已从Elasticsearch中删除:在7.0中,默认情况下,mappings元素将不再将类型名称作为顶级键返回。
通过在请求上设置include_type_name = false,您已经可以选择接受这种行为。
GET twitter?include_type_name=false
4. Indices Exists 索引是否存在
HEAD twitter
5.Open / Close Index API 打开/关闭索引
打开和关闭索引API允许先关闭索引,然后再打开索引。封闭索引几乎没有集群开销(除了维护其元数据),并且被禁止进行读/写操作。可以打开一个封闭的索引,然后将通过正常的恢复过程。
POST /my_index/_close
POST /my_index/_open
6. Put Mapping 修改
PUT映射API允许您将字段添加到现有索引或更改现有字段的仅搜索设置。
PUT twitter
{}
PUT twitter/_mapping/_doc
{
"properties": {
"email": {
"type": "keyword"
}
}
}
创建一个没有任何类型映射的名为twitter的索引。 使用PUT映射API将名为email的新字段添加到_doc映射类型。
--------------------------------
PUT映射API可以通过一个请求应用于多个索引。例如,我们可以同时更新twitter-1和twitter-2映射:
# Create the two indices
PUT twitter-1
PUT twitter-2
# Update both mappings
PUT /twitter-1,twitter-2/_mapping/_doc
{
"properties": {
"user_name": {
"type": "text"
}
}
}
-----------------------------------------
PUT my_index/_mapping/_doc
{
"properties": {
"my_field": {
"type": "text",
"fielddata": true
}
}
}
7. Get Mapping 获取mapping
GET /twitter/_mapping/_doc
如果要获取所有索引和类型的映射,则以下两个示例等效:
GET /_all/_mapping
GET /_mapping
8. Get Field Mapping 获取字段mapping
以下仅返回字段标题的映射:
GET publications/_mapping/_doc/field/title
GET /gurp_test/_mapping/_doc/field/address
9. Types Exists 种类检测
用于检查索引中是否存在一个或多个类型。
HEAD gurp_test/_mapping/_doc
10.Index Aliases 索引别名
当使用特定索引时,Elasticsearch中的API接受索引名称,并且在适用时接受多个索引。
索引别名API允许对别名使用名称进行别名,所有API都会自动将别名转换为实际的索引名称。
别名也可以映射到多个索引,并且在指定别名时,别名将自动扩展为别名索引。
别名也可以与过滤器关联,该过滤器将在搜索和路由值时自动应用。别名不能与索引同名。
以下是将别名alias1与索引test1关联的示例:
POST /_aliases
{
"actions" : [
{ "add" : { "index" : "test1", "alias" : "alias1" } }
]
}
-----------------删除
POST /_aliases
{
"actions" : [
{ "remove" : { "index" : "test1", "alias" : "alias1" } }
]
}
--------------将别名与多个索引相关联只是几个添加操作:
POST /_aliases
{
"actions" : [
{ "add" : { "index" : "test1", "alias" : "alias1" } },
{ "add" : { "index" : "test2", "alias" : "alias1" } }
]
}
----------------------可以使用索引数组语法为一个动作指定多个索引:
POST /_aliases
{
"actions" : [
{ "add" : { "indices" : ["test1", "test2"], "alias" : "alias1" } }
]
}
11. Update Indices Settings 更新索引设置
PUT /twitter/_settings
{
"index" : {
"number_of_replicas" : 2
}
}
12.Get Settings 获取设置
获取设置API允许检索索引/索引的设置:
GET /twitter/_settings
13. Flush 刷新
刷新API允许通过API刷新一个或多个索引。索引的刷新过程可确保当前仅保留在事务日志中的所有数据也将永久保留在Lucene中。这减少了恢复时间,因为在打开Lucene索引后,无需从事务日志中重新索引数据。默认情况下,Elasticsearch使用试探法以根据需要自动触发刷新。用户很少需要直接调用API。
POST twitter/_flush
POST /twitter/_refresh