source:https://www.elastic.co/guide/en/elasticsearch/reference/6.4/api-conventions.html
es的API使用http的json格式進行請求(https://www.elastic.co/guide/en/elasticsearch/reference/6.4/modules-http.html)
這一張列出來的協議可以適用於所有的API,除非有其他另外的說明。
多重索引:
大多數的AP都支持跨多個索引執行一個索引參數操作
用簡單的test1,test2,test3表示(或者_all來表示所有的索引)
這同樣支持wildcards,比如:test* / *test / te*t / *test*
所有的索引API支持下面的url字符串變量查詢
ignore_unavailable
控制如果特定的索引不可用是否可以忽略,包括不存在的或者關閉的索引,可以指定true或者false
allow_no_indices:
控制通過通配符索引表達式查詢的當前索引是否失敗。可以指定true/false。
舉個栗子:
如果指定通配符表達式foo*,但是沒有對應的索引匹配,按照這個設定這個請求將會失敗,這個設置同時適用於別名,一個別名指向一個關閉的索引
expand_wildcards
控制擴展到哪種索引通配符表達式,指定open的話通配符表達式就只擴展到open的索引;指定closed通配符表達式就只擴展到關閉的索引,同時指定兩個值(open,closed)就可以擴展到所有索引
如果什么也不指定(指定none) 通配符表達式就會不可用,如果指定了all就會擴展到所有索引。
默認參數取決於正在使用的api
一個索引API比如說 documentAPI single-index 別名api不支持多重索引
索引名中支持的日期數學
日期數學索引名解決方法使你能對一個區間的時間系列索引進行搜索,而不是搜索所有的時間系列索引再過濾結果或者是維護一些別名。限制索引的數量可以降低搜索對集群的負載提高查詢性能。舉個栗子,如果在你的日志中發現了搜索錯誤,你可以使用日期數學名字模板去限制搜索過去兩天的數據。
幾乎所有的API有索引變量索引變量值中支持日期數學
一個日期數學索引名稱是以下的格式:
<static_name{date_math_expr{date_format|time_zone}}>
static_name:名稱的靜態文本部分
date_math_expr:動態日期數學表達式用來動態計算日期
date_format:日期可選格式,默認 YYYY.MM.dd
time_zone:可選的zone,默認utc
你必須將日期數學索引名稱表達式放入到尖括號內,所有特殊的字符都應該用URI編碼,比如:
# GET /<logstash-{now/d}>/_search
curl -X GET "localhost:9200/%3Clogstash-%7Bnow%2Fd%7D%3E/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query" : {
"match": {
"test": "data"
}
}
}
'
日期數學字符的百分比編碼:

下面的例子展示了日期數學索引名稱的不同格式,在給定當前時間的情況下給定的最終索引名稱是
22nd March 2024 noon utc

在索引名模板中用字符 { 和 } 在靜態部分中,用 反斜杠\來進行轉義
<elastic\\{ON\\}-{now/M}>resolves toelastic{ON}-2024.03.01
下面的例子展示了一個搜索過去三天日志索引的搜索請求,假設索引用默認的日志索引名稱格式,logstash-YYYY.MM.dd
# GET /<logstash-{now/d-2d}>,<logstash-{now/d-1d}>,<logstash-{now/d}>/_search
curl -X GET "localhost:9200/%3Clogstash-%7Bnow%2Fd-2d%7D%3E%2C%3Clogstash-%7Bnow%2Fd-1d%7D%3E%2C%3Clogstash-%7Bnow%2Fd%7D%3E/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query" : {
"match": {
"test": "data"
}
}
}
'
公用操作
下面的操作可以適用於所有的REST APIS
好看的結果
在請求后面加上?pretty=true, JSON返回一個好看一點的結果,另一個操作就是設置?format=yaml
這將會返回一個易讀的yaml格式
人類可讀輸出
統計數據返回一個人類易閱讀的格式("exists_time":"1h" or "size":"1kb") 和對電腦友好格式("exists_time_in_millis":3600000 or "size_in_bytes":1024) 人類友好閱讀方式可以通過在查詢語句后增加?human=false關掉,方便監控工具進行結果獲取,默認的human標志是未開啟false
返回過濾:
請求:
curl -X GET "localhost:9200/_search?q=elasticsearch&filter_path=took,hits.hits._id,hits.hits._score&pretty"
返回:
{ "took" : 3, "hits" : { "hits" : [ { "_id" : "0", "_score" : 1.6375021 } ] } }
請求:
curl -X GET "localhost:9200/_cluster/state?filter_path=metadata.indices.*.stat*&pretty"
返回:
{ "metadata" : { "indices" : { "twitter": {"state": "open"} } } }
請求:
curl -X GET "localhost:9200/_cluster/state?filter_path=routing_table.indices.**.state&pretty"
返回:
{ "routing_table": { "indices": { "twitter": { "shards": { "0": [{"state": "STARTED"}, {"state": "UNASSIGNED"}], "1": [{"state": "STARTED"}, {"state": "UNASSIGNED"}], "2": [{"state": "STARTED"}, {"state": "UNASSIGNED"}], "3": [{"state": "STARTED"}, {"state": "UNASSIGNED"}], "4": [{"state": "STARTED"}, {"state": "UNASSIGNED"}] } } } } }
可以用字符 - 來對結果進行過濾
curl -X GET "localhost:9200/_count?filter_path=-_shards&pretty"
返回:
{ "count" : 5 }
包括語句和排除語句都能在同樣的表達式中組合,下面的例子中,先排除后包括過濾
curl -X GET "localhost:9200/_cluster/state?filter_path=metadata.indices.*.state,-metadata.indices.logstash-*&pretty"
返回:
{ "metadata" : { "indices" : { "index-1" : {"state" : "open"}, "index-2" : {"state" : "open"}, "index-3" : {"state" : "open"} } } }
注意es有時候直接返回字段的原始值,比如"_source",如果你想過濾_source,你可以考慮組合已存在的_source 元素,(https://www.elastic.co/guide/en/elasticsearch/reference/6.4/docs-get.html#get-source-filtering)
通過filter_path
curl -X POST "localhost:9200/library/book?refresh&pretty" -H 'Content-Type: application/json' -d'
{"title": "Book #1", "rating": 200.1}
'
curl -X POST "localhost:9200/library/book?refresh&pretty" -H 'Content-Type: application/json' -d'
{"title": "Book #2", "rating": 1.7}
'
curl -X POST "localhost:9200/library/book?refresh&pretty" -H 'Content-Type: application/json' -d'
{"title": "Book #3", "rating": 0.1}
'
curl -X GET "localhost:9200/_search?filter_path=hits.hits._source&_source=title&sort=rating:desc&pretty"
返回:
{ "hits" : { "hits" : [ { "_source":{"title":"Book #1"} }, { "_source":{"title":"Book #2"} }, { "_source":{"title":"Book #3"} } ] } }
平面設置
flat_settings 標志影響設置列表的呈現,當flag_settings標志是true的時候以平面模式返回數據
curl -X GET "localhost:9200/twitter/_settings?flat_settings=true&pretty"
返回:
{ "twitter" : { "settings": { "index.number_of_replicas": "1", "index.number_of_shards": "1", "index.creation_date": "1474389951325", "index.uuid": "n6gzFZTgS664GUfx0Xrpjw", "index.version.created": ..., "index.provided_name" : "twitter" } } }
當flag_settings 標志是false的時候以人類可閱讀的方式返回
curl -X GET "localhost:9200/twitter/_settings?flat_settings=false&pretty"
返回:
"twitter" : { "settings" : { "index" : { "number_of_replicas": "1", "number_of_shards": "1", "creation_date": "1474389951325", "uuid": "n6gzFZTgS664GUfx0Xrpjw", "version": { "created": ... }, "provided_name" : "twitter" } } } }
默認情況下flag_settings設置為false
參數:
rest參數遵循大小寫試用下划線的約定
布爾值:
所有的restAPI參數(請求參數和json體)都支持提供布爾值'false'當做false和“true”當做true,其他的值會報錯。
數值:
所有的rest api都支持以字符串形式提供數值在支持json數字類型的時候
時間單元:
當需要指定持續時間時,比如timeout元素,間隔必須指定單位,比如2d代表2天,支持單元如下:

假設當前時間now是 2001-01-01 12:00:00,有以下使用方式:
now+1h :2001-01-01 13:00:00
now-1h:2001-01-01 11:00:00
now-1h/d: 2001-01-01 00:00:00
2001.02.01\|\|+1M/d :2001-03-01 00:00:00
結果過濾:
所有的rest API都接收filter_path參數 這個可以用來減少es返回的結果,這個參數接收一些用逗號分割的過濾條件列表
curl -X GET "localhost:9200/_search?q=elasticsearch&filter_path=took,hits.hits._id,hits.hits._score&pretty"
{ "took" : 3, "hits" : { "hits" : [ { "_id" : "0", "_score" : 1.6375021 } ] } }
這同樣支持 * 匹配查詢參數,用來匹配field需要過濾的名稱。
curl -X GET "localhost:9200/_cluster/state?filter_path=metadata.indices.*.stat*&pretty"
{ "metadata" : { "indices" : { "twitter": {"state": "open"} } } }
**匹配可以用來匹配一些不知道絕對路徑的field,我們可以用下面的請求獲取每個segment的lucene版本:
curl -X GET "localhost:9200/_cluster/state?filter_path=routing_table.indices.**.state&pretty"
返回{
"routing_table": { "indices": { "twitter": { "shards": { "0": [{"state": "STARTED"}, {"state": "UNASSIGNED"}], "1": [{"state": "STARTED"}, {"state": "UNASSIGNED"}], "2": [{"state": "STARTED"}, {"state": "UNASSIGNED"}], "3": [{"state": "STARTED"}, {"state": "UNASSIGNED"}],
"4": [{"state": "STARTED"}, {"state": "UNASSIGNED"}] } } } } }
這同樣可以用字符'-'來對結果進行過濾
