这些API负责管理索引的所有方面,如设置,别名,映射,索引模板.
创建索引
此API帮助您创建索引.当用户将JSON对象传递给任何索引时,可以自动创建索引,也可以在此之前创建索引.要创建索引,您只需要发送带有设置,映射和别名的发布请求,或者只需要一个没有正文的简单请求.例如,
POST http://localhost:9200/acade
回复
{"acknowledged":true}
或,有一些设置 -
POST http://localhost:9200/colleges
请求正文
{ "settings" : { "index" : { "number_of_shards" : 5, "number_of_replicas" : 3 } }}
Response
{"acknowledged":true}
或带映射 -
POST http://localhost:9200/colleges
Request Body
{ "settings" : { "number_of_shards" : 3 }, "mappings" : { "type1" : { "_source" : { "enabled" : false }, "properties" : { "college_name" : { "type" : "string" }, "college type" : {"type":"string"} } } }}
Response
{"acknowledged":true}
或者,使用别名 -
POST http://localhost:9200/colleges
Request Body
{ "aliases" : { "alias_1" : {}, "alias_2" : { "filter" : { "term" : {"user" : "manu" } }, "routing" : "manu" } }}
Response
{"acknowledged":true}
删除索引
此API可帮助您删除任何索引.您只需要传递具有该特定索引的URL的删除请求.例如,
DELETE http://localhost:9200/colleges
只需使用_all,*即可删除所有索引.
获取索引
只需向一个请求发送get请求即可调用此API或多个指数.这将返回有关索引的信息.
GET http://localhost:9200/schools
Response
{ "schools":{ "aliases":{}, "mappings":{ "school":{ "properties":{ "city":{"type":"string"}, "description":{"type":"string"}, "fees":{"type":"long"}, "location":{"type":"double"}, "name":{"type":"string"}, "rating":{"type":"string"}, "state":{"type":"string"}, "street":{"type":"string"}, "tags":{"type":"string"}, "zip":{"type":"string"} } } }, "settings":{ "index":{ "creation_date":"1454409831535", "number_of_shards":"5", "number_of_replicas":"1", "uuid":"iKdjTtXQSMCW4xZMhpsOVA", "version":{"created":"2010199"} } }, "warmers":{} }}
您可以获取信息所有索引的使用_all或*.
索引存在
索引的存在性可以通过向该索引发送get请求来确定.如果HTTP响应为200,则存在;如果它是404,则它不存在.
打开/关闭索引API
只需关闭或打开一个或多个索引就可以了在post中添加_close或_open以请求该索引.例如,
POST http://localhost:9200/schools/_close
或
POST http://localhost:9200/schools/_open
索引别名
此API有助于使用_aliases关键字为任何索引提供别名.单个别名可以映射到多个别名,别名不能与索引同名.例如,
POST http://localhost:9200/_aliases
请求正文
{ "actions" : [ { "add" : { "index" : "schools", "alias" : "schools_pri" } } ]}
Response
{"acknowledged":true}
然后,
GET http://localhost:9200/schools_pri
Response
………………………………………………{"schools":{"aliases":{"schools_pri":{}},"}}………………………………………………
Index Settings
您只需在网址末尾附加_settings关键字即可获取索引设置。 例如,
GET http://localhost:9200/schools/_settings
Response
{ "schools":{ "settings":{ "index":{ "creation_date":"1454409831535", "number_of_shards":"5", "number_of_replicas":"1", "uuid":"iKdjTtXQSMCW4xZMhpsOVA", "version":{"created":"2010199"} } } }}
Analyze
此API帮助分析文本并发送具有偏移值和数据类型的令牌。 例如,
POST http://localhost:9200/_analyze
Request Body
{ "analyzer" : "standard", "text" : "you are reading this at tutorials point"}
Response
{ "tokens":[ {"token":"you", "start_offset":0, "end_offset":3, "type":"", "position":0}, {"token":"are", "start_offset":4, "end_offset":7, "type":" ", "position":1}, {"token":"reading", "start_offset":8, "end_offset":15, "type":" ", "position":2}, {"token":"this", "start_offset":16, "end_offset":20, "type":" ", "position":3}, {"token":"at", "start_offset":21, "end_offset":23, "type":" ", "position":4}, {"token":"tutorials", "start_offset":24, "end_offset":33, "type":" ", "position":5}, {"token":"point", "start_offset":34, "end_offset":39, "type":" ", "position":6} ]}
您还可以分析具有任何索引的文本,然后将根据与该索引关联的分析器对文本进行分析。
Index Templates
您还可以使用映射创建索引模板,该映射可以应用于新索引。 例如,
POST http://localhost:9200/_template/template_a
Request Body
{ "template" : "tu*", "settings" : { "number_of_shards" : 3 }, "mappings" : { "chapter" : { "_source" : { "enabled" : false } } }}
任何以" tu"开头的索引将具有与template_a相同的设置。
Index Stats
该API可帮助您提取有关特定索引的统计信息。 您只需要在结尾发送带有索引URL和_stats关键字的get请求。
GET http://localhost:9200/schools/_stats
Response
………………………………………………{"_shards":{"total":10, "successful":5, "failed":0}, "_all":{"primaries":{"docs":{ "count":3, "deleted":0}}}, "store":{"size_in_bytes":16653, "throttle_time_in_millis":0},………………………………………………
Flush
该API有助于清除索引存储器中的数据并将其迁移到索引存储,还可以清除内部事务日志。 例如,
GET http://localhost:9200/schools/_flush
Response
{"_shards":{"total":10, "successful":5, "failed":0}}
Refresh
默认情况下,刷新是在Elasticsearch中计划的,但是您可以使用_refresh显式刷新一个或多个索引。 例如,
GET http://localhost:9200/schools/_refresh
Response
{"_shards":{"total":10, "successful":5, "failed":0}}