Web中的應用程序編程接口(API)是一組函數(shù)調(diào)用或其他編程指令,用于訪問該特定Web應用程序中的軟件組件。例如,F(xiàn)acebook API通過訪問來自Facebook的數(shù)據(jù)或其他功能來幫助開發(fā)人員創(chuàng)建應用程序;它可以是出生日期或狀態(tài)更新。
Elasticsearch提供了一個REST API,JSON可以通過HTTP訪問該API。Elasticsearch使用一些約定,我們現(xiàn)在將對其進行討論。
API中的大多數(shù)操作,主要是搜索和其他操作,都是針對一個或多個索引的。這有助于用戶只需執(zhí)行一次查詢,就可以在多個位置或所有可用數(shù)據(jù)中進行搜索。許多不同的符號用于在多個索引中執(zhí)行操作。我們將在本章中討論其中的一些。
POST /index1,index2,index3/_search
{
"query":{
"query_string":{
"query":"any_string"
}
}
}來自index1, index2, index3的JSON對象中包含any_string。
POST /_all/_search
{
"query":{
"query_string":{
"query":"any_string"
}
}
}來自所有索引的JSON對象,其中包含any_string。
POST /school*/_search
{
"query":{
"query_string":{
"query":"CBSE"
}
}
}來自所有索引的JSON對象,這些索引始于其中包含CBSE的學校。
另外,您也可以使用以下代碼-
POST /school*,-schools_gov /_search
{
"query":{
"query_string":{
"query":"CBSE"
}
}
}JSON對象來自所有以“ school”開頭的索引,但不是來自school_gov并包含CBSE的索引。
還有一些URL查詢字符串參數(shù)-
ignore_unavailable?如果index(es)URL中不存在一個或多個,則不會發(fā)生錯誤或?qū)⑼V谷魏尾僮?。例如,school索引存在,但book_shops不存在。
POST /school*,book_shops/_search
{
"query":{
"query_string":{
"query":"CBSE"
}
}
}{
"error":{
"root_cause":[{
"type":"index_not_found_exception", "reason":"no such index",
"resource.type":"index_or_alias", "resource.id":"book_shops",
"index":"book_shops"
}],
"type":"index_not_found_exception", "reason":"no such index",
"resource.type":"index_or_alias", "resource.id":"book_shops",
"index":"book_shops"
},"status":404
}考慮以下代碼-
POST /school*,book_shops/_search?ignore_unavailable = true
{
"query":{
"query_string":{
"query":"CBSE"
}
}
}來自所有索引的JSON對象,這些索引始于其中包含CBSE的學校。
true如果帶有通配符的URL沒有索引,則此參數(shù)的值將防止錯誤。例如,沒有以schools_pri開頭的索引-
POST /schools_pri*/_search?allow_no_indices = true
{
"query":{
"match_all":{}
}
}{
"took":1,"timed_out": false, "_shards":{"total":0, "successful":0, "failed":0},
"hits":{"total":0, "max_score":0.0, "hits":[]}
}此參數(shù)決定通配符是否需要擴展為開放索引或封閉索引,或同時執(zhí)行這兩者。此參數(shù)的值可以是打開和關(guān)閉的,也可以是沒有。
例如,封閉索引學校-
POST /schools/_close
{"acknowledged":true}考慮以下代碼-
POST /school*/_search?expand_wildcards = closed
{
"query":{
"match_all":{}
}
}{
"error":{
"root_cause":[{
"type":"index_closed_exception", "reason":"closed", "index":"schools"
}],
"type":"index_closed_exception", "reason":"closed", "index":"schools"
}, "status":403
}Elasticsearch提供了根據(jù)日期和時間搜索索引的功能。我們需要以特定格式指定日期和時間。例如,accountdetail-2015.12.30,索引將存儲2015年12月30日的銀行帳戶詳細信息??梢詧?zhí)行數(shù)學運算以獲取特定日期或日期和時間范圍的詳細信息。
日期數(shù)學索引名稱的格式-
<static_name{date_math_expr{date_format|time_zone}}>
/<accountdetail-{now-2d{YYYY.MM.dd|utc}}>/_searchstatic_name是表達式的一部分,在每個日期的數(shù)學索引(如客戶明細)中都保持不變。date_math_expr包含數(shù)學表達式,該數(shù)學表達式像now-2d一樣動態(tài)確定日期和時間。date_format包含將日期寫入諸如YYYY.MM.dd之類的索引中的格式。如果今天是2015年12月30日,則<accountdetail- {now-2d {YYYY.MM.dd}}>將返回accountdetail-2015.12.28。
| 表達 | 解析為 |
|---|---|
| <accountdetail-{now-d}> | accountdetail-2015.12.29 |
| <accountdetail-{now-M}> | accountdetail-2015.11.30 |
| <accountdetail-{now{YYYY.MM}}> | accountdetail-2015.12 |
現(xiàn)在,我們將看到Elasticsearch中提供的一些常用選項,這些選項可用于獲取指定格式的響應。
我們可以通過添加URL查詢參數(shù)(即pretty = true)來在格式良好的JSON對象中獲得響應。
POST /schools/_search?pretty = true
{
"query":{
"match_all":{}
}
}……………………..
{
"_index" : "schools", "_type" : "school", "_id" : "1", "_score" : 1.0,
"_source":{
"name":"Central School", "description":"CBSE Affiliation",
"street":"Nagan", "city":"paprola", "state":"HP", "zip":"176115",
"location": [31.8955385, 76.8380405], "fees":2000,
"tags":["Senior Secondary", "beautiful campus"], "rating":"3.5"
}
}
………………….此選項可以將統(tǒng)計響應更改為人類可讀形式(如果human = true)或計算機可讀形式(如果human = false)。例如,如果human = true,則distance_kilometer = 20KM;如果human = false,則distance_meter = 20000,此時需要其他計算機程序使用響應。
通過將它們添加到field_path參數(shù)中,我們可以過濾對較少字段的響應。例如,
POST /schools/_search?filter_path = hits.total
{
"query":{
"match_all":{}
}
}{"hits":{"total":3}}