web中的應(yīng)用編程接口(API)是一組函數(shù)調(diào)用或其他編程指令以訪問該特定web應(yīng)用中的軟件組件。 例如,Facebook API幫助開發(fā)者通過從Facebook訪問數(shù)據(jù)或其他功能來創(chuàng)建應(yīng)用程序; 它可以是出生日期或狀態(tài)更新。
Elasticsearch提供了一個(gè)REST API,通過HTTP通過JSON訪問。 Elasticsearch使用以下約定 -
API中的大多數(shù)操作(主要是搜索和其他操作)用于一個(gè)或多個(gè)索引。 這有助于用戶通過只執(zhí)行一次查詢來搜索多個(gè)位置或所有可用數(shù)據(jù)。 許多不同的符號用于在多個(gè)索引中執(zhí)行操作。 我們將在本節(jié)討論其中的一些。
POST http://localhost:9200/index1,index2,index3/_search
請求正文
{
"query":{
"query_string":{
"query":"any_string"
}
}
}
響應(yīng)
來自index1
,index2
,index3
的JSON對象,其中包含any_string
。
POST http://localhost:9200/_all/_search
請求正文
{
"query":{
"query_string":{
"query":"any_string"
}
}
}
來自所有索引的JSON對象,并且有any_string
。
POST http://localhost:9200/school*/_search
請求正文
{
"query":{
"query_string":{
"query":"CBSE"
}
}
}
響應(yīng)
來自所有索引的JSON對象,從school
開始,有CBSE。
或者,也可以使用以下代碼 -
POST http://localhost:9200/school*,-schools_gov /_search
請求正文
{
"query":{
"query_string":{
"query":"CBSE"
}
}
}
來自所有索引的JSON對象,它們以“school
”開頭,但不是schools_gov
并且在其中有CBSE。
還有一些URL查詢字符串參數(shù) -
ignore_unavailable
- 如果URL中存在的一個(gè)或多個(gè)索引不存在,則不會發(fā)生錯誤或操作不會停止。 例如,schools
索引存在,但book_shops
不存在 -POST http://localhost:9200/school*,book_shops/_search
請求正文
{
"query":{
"query_string":{
"query":"CBSE"
}
}
}
響應(yīng)
{
"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 http://localhost:9200/school*,book_shops/_search?ignore_unavailable = true
請求正文
{
"query":{
"query_string":{
"query":"CBSE"
}
}
}
響應(yīng)(無錯誤)
來自所有索引的JSON對象,從 school
開始,有CBSE
。
allow_no_indices
如果帶有通配符的網(wǎng)址沒有索引,這個(gè)參數(shù)是true
值時(shí)將防止錯誤。
例如,不是以schools_pri
開頭的索引 -
POST
http://localhost:9200/schools_pri*/_search?allow_no_indices = true
請求正文
{
"query":{
"match_all":{}
}
}
響應(yīng)(無錯誤)
{
"took":1,"timed_out": false, "_shards":{"total":0, "successful":0, "failed":0},
"hits":{"total":0, "max_score":0.0, "hits":[]}
}
expand_wildcards
此參數(shù)確定通配符是否需要擴(kuò)展為打開索引或閉合索引或兩者。 此參數(shù)的值可以是打開和關(guān)閉或無和全部。
例如,關(guān)閉索引schools
-
POST http://localhost:9200/schools/_close
響應(yīng)
{"acknowledged":true}
看看下面的代碼 -
POST http://localhost:9200/school*/_search?expand_wildcards = closed
請求正文
{
"query":{
"match_all":{}
}
}
響應(yīng)
{
"error":{
"root_cause":[{
"type":"index_closed_exception", "reason":"closed", "index":"schools"
}],
"type":"index_closed_exception", "reason":"closed", "index":"schools"
}, "status":403
}
Elasticsearch提供了根據(jù)日期和時(shí)間搜索索引的功能。我們需要以特定格式指定日期和時(shí)間。 例如,accountdetail-2015.12.30
,索引將存儲2015年12月30日的銀行帳戶詳細(xì)信息??梢詧?zhí)行數(shù)學(xué)操作以獲取特定日期或日期和時(shí)間范圍的詳細(xì)信息。
日期數(shù)字索引名稱的格式 -
<static_name{date_math_expr{date_format|time_zone}}>
http://localhost:9200/<accountdetail-{now-2d{YYYY.MM.dd|utc}}>/_search
static_name
是表達(dá)式的一部分,在每個(gè)日期數(shù)學(xué)索引(如帳戶詳細(xì)信息)中保持相同。 date_math_expr包含動態(tài)確定日期和時(shí)間的數(shù)學(xué)表達(dá)式,如now-2d
。date_format
包含日期在索引中寫入的格式,如YYYY.MM.dd
。 如果今天的日期是2015年12月30日,則<accountdetail- {now-2d {YYYY.MM.dd}}>
將返回accountdetail-2015.12.28
。
表達(dá)式 | 解析為 |
---|---|
<accountdetail-{now-d}> |
accountdetail-2016.12.29 |
<accountdetail-{now-M}> |
accountdetail-2015.11.30 |
<accountdetail-{now{YYYY.MM}}> |
accountdetail-2015.12 |
現(xiàn)在將看到Elasticsearch
中可用于獲取指定格式的響應(yīng)的一些常見選項(xiàng)。
可以通過附加一個(gè)網(wǎng)址查詢參數(shù)(即pretty = true
),獲得格式正確的JSON對象的響應(yīng)。
POST http://localhost:9200/schools/_search?pretty = true
請求正文
{
"query":{
"match_all":{}
}
}
響應(yīng)
……………………..
{
"_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"
}
}
………………….
人類可讀輸出
此選項(xiàng)可以將統(tǒng)計(jì)響應(yīng)更改為人類可讀的形式(如果human = true
)或計(jì)算機(jī)可讀形式(如果human = false
)。 例如,如果human = true
那么distance_kilometer = 20KM
,如果human = false
那么distance_meter = 20000
,則是響應(yīng)需要被另一個(gè)計(jì)算機(jī)程序使用。
響應(yīng)過濾
可以通過將其添加到field_path
參數(shù)中來過濾對較少字段的響應(yīng)。 例如,
POST http://localhost:9200/schools/_search?filter_path = hits.total
請求正文
{
"query":{
"match_all":{}
}
}
響應(yīng)
{"hits":{"total":3}}