教程 > elastic search > 阅读:17

elasticsearch index api——迹忆客-ag捕鱼王app官网

这些 api 负责管理索引的所有方面,如设置、别名、映射、索引模板。


创建 index

此 api 可帮助我们创建 index。 索引可以在用户将 json 对象传递给任何索引时自动创建,也可以在此之前创建。 要创建索引,我们只需要发送一个带有设置、映射和别名的 put 请求,或者只是一个没有正文的简单请求。

put colleges

运行上面的代码,我们得到如下所示的输出

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

我们还可以在上面的命令中添加一些设置

put colleges
{
  "settings" : {
      "index" : {
         "number_of_shards" : 3,
         "number_of_replicas" : 2
      }
   }
}

运行上面的代码,我们得到如下所示的输出

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

删除 index

此 api 可帮助我们删除任何索引。 只需要传递一个带有该特定索引名称的删除请求。

delete /colleges

我们只需使用 _all* 即可删除所有索引。


获取 index

只需向一个或多个索引发送 get 请求即可调用此 api。 这将返回有关索引的信息。

get colleges

运行上面的代码,我们得到如下所示的输出

{
   "colleges" : {
      "aliases" : {
         "alias_1" : { },
         "alias_2" : {
            "filter" : {
               "term" : {
                  "user" : "pkay"
               }
            },
            "index_routing" : "pkay",
            "search_routing" : "pkay"
         }
      },
      "mappings" : { },
      "settings" : {
         "index" : {
            "creation_date" : "1556245406616",
            "number_of_shards" : "1",
            "number_of_replicas" : "1",
            "uuid" : "3exjbdl2r1qdlssikwdaug",
            "version" : {
               "created" : "7000099"
            },
            "provided_name" : "colleges"
         }
      }
   }
}

我们可以使用 _all* 获取所有索引的信息。


检查 index 是否存在

可以通过向该索引发送获取请求来确定索引的存在。 如果 http 响应为 200,则存在; 如果是 404,则不存在。

head colleges

运行上面的代码,我们得到如下所示的输出

200-ok

index 设置

我们只需在 url 末尾附加 _settings 关键字即可获取索引设置。

get /colleges/_settings

运行上面的代码,我们得到如下所示的输出

{
   "colleges" : {
      "settings" : {
         "index" : {
            "creation_date" : "1556245406616",
            "number_of_shards" : "1",
            "number_of_replicas" : "1",
            "uuid" : "3exjbdl2r1qdlssikwdaug",
            "version" : {
               "created" : "7000099"
            },
            "provided_name" : "colleges"
         }
      }
   }
}

index 状态

此 api 可帮助我们提取有关特定索引的统计信息。 我们只需要在末尾发送带有索引 url 和 _stats 关键字的 get 请求。

get /_stats

运行上面的代码,我们得到如下所示的输出

………………………………………………
},
   "request_cache" : {
      "memory_size_in_bytes" : 849,
      "evictions" : 0,
      "hit_count" : 1171,
      "miss_count" : 4
   },
   "recovery" : {
      "current_as_source" : 0,
      "current_as_target" : 0,
      "throttle_time_in_millis" : 0
   }
} ………………………………………………

flush 刷新

索引的刷新过程确保当前仅保存在事务日志中的任何数据也永久保存在 lucene 中。 这减少了恢复时间,因为在打开 lucene 索引后不需要从事务日志中重新索引数据。

post colleges/_flush

运行上面的代码,我们得到如下所示的输出

{
   "_shards" : {
      "total" : 2,
      "successful" : 1,
      "failed" : 0
   } 
}

查看笔记

扫码一下
查看教程更方便
网站地图