ElasticSearch 索引 API
-
索引 API
这些API负责管理索引的所有方面,例如设置,别名,映射,索引模板。 -
创建索引
该API可帮助您创建索引。当用户将JSON对象传递给任何索引时,可以自动创建索引,也可以在此之前创建索引。要创建索引,您只需要发送带有设置,映射和别名的PUT请求,或者仅发送不带正文的简单请求。PUT http://localhost:9200/colleges
运行上面的代码后,我们得到如下所示的输出-{ "acknowledged" : true, "shards_acknowledged" : true, "index" : "colleges" }
我们还可以向上述命令添加一些设置-PUT http://localhost:9200/colleges { "settings" : { "index" : { "number_of_shards" : 3, "number_of_replicas" : 2 } } }
运行上面的代码后,我们得到如下所示的输出-{ "acknowledged" : true, "shards_acknowledged" : true, "index" : "colleges" }
-
删除索引
此API可帮助您删除任何索引。您只需要传递带有该特定索引名称的delete请求即可。DELETE http://localhost:9200/colleges
您可以仅使用_all或*删除所有索引。 -
获取索引
可以通过仅将get请求发送到一个或多个索引来调用此API。这将返回有关索引的信息。GET http://localhost:9200/colleges
运行上面的代码后,我们得到如下所示的输出-{ "colleges": { "aliases": {}, "mappings": {}, "settings": { "index": { "routing": { "allocation": { "include": { "_tier_preference": "data_content" } } }, "number_of_shards": "3", "provided_name": "colleges", "creation_date": "1612251305756", "number_of_replicas": "2", "uuid": "mQsQCcTJT0qcL8iFMADH2w", "version": { "created": "7100299" } } } } }
您可以使用_all或*获取所有索引的信息。 -
存在索引
索引的存在可以通过仅向该索引发送get请求来确定。如果HTTP响应是索引信息,则存在。如果是status 是 404,则不存在。GET colleges
运行上面的代码后,我们得到如下所示的输出-{ "colleges": { "aliases": {}, "mappings": {}, "settings": { "index": { "routing": { "allocation": { "include": { "_tier_preference": "data_content" } } }, "number_of_shards": "3", "provided_name": "colleges", "creation_date": "1612251305756", "number_of_replicas": "2", "uuid": "mQsQCcTJT0qcL8iFMADH2w", "version": { "created": "7100299" } } } } }
-
索引设定(setting)
您只需在网址末尾附加_settings关键字即可获取索引设置。GET http://localhost:9200/colleges/_settings
运行上面的代码后,我们得到如下所示的输出-{ "colleges" : { "settings" : { "index" : { "creation_date" : "1556245406616", "number_of_shards" : "1", "number_of_replicas" : "1", "uuid" : "3ExJbdl2R1qDLssIkwDAug", "version" : { "created" : "7000099" }, "provided_name" : "colleges" } } } }
-
索引统计
该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 } } ………………………………………………
-
刷新
索引的刷新过程可确保当前仅保留在事务日志中的所有数据也将永久保留在Lucene中。这减少了恢复时间,因为打开Lucene索引后无需从事务日志中重新索引数据。POST colleges/_flush
运行上面的代码后,我们得到如下所示的输出-{ "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 } }