Elasticsearch提供單文件API和多文件API,其中API呼叫分別針對單個文件和多個文件。
當使用特定對映對相應索引發出請求時,它有助於在索引中新增或更新JSON文件。 例如,以下請求將JSON物件新增到索引學校和學校對映下。
POST http://localhost:9200/schools/school/4
請求正文
{
"name":"City School", "description":"ICSE", "street":"West End", "city":"Meerut",
"state":"UP", "zip":"250002", "location":[28.9926174, 77.692485], "fees":3500,
"tags":["fully computerized"], "rating":"4.5"
}
響應
{
"_index":"schools", "_type":"school", "_id":"4", "_version":1,
"_shards":{"total":2, "successful":1,"failed":0}, "created":true
}
當請求將JSON物件新增到特定索引時,如果該索引不存在,那麼此API會自動建立該索引以及該特定JSON物件的基礎對映。 可以通過將以下引數的值更改為false
來禁用此功能,這個值是存在於elasticsearch.yml
檔案中,開啟elasticsearch.yml
檔案設定如下 。
action.auto_create_index:false
index.mapper.dynamic:false
還可以限制自動建立索引,其中通過更改以下引數的值只允許指定模式的索引名稱 -
action.auto_create_index:+acc*,-bank*
(其中+
表示允許, -
表示不允許)
Elasticsearch還提供版本控制功能。我們可以使用版本查詢引數指定特定文件的版本。 例如,
POST http://localhost:9200/schools/school/1?version = 1
請求正文
{
"name":"Central School", "description":"CBSE Affiliation", "street":"Nagan",
"city":"paprola", "state":"HP", "zip":"176115", "location":[31.8955385, 76.8380405],
"fees":2200, "tags":["Senior Secondary", "beautiful campus"], "rating":"3.3"
}
響應內容
{
"_index":"schools", "_type":"school", "_id":"1", "_version":2,
"_shards":{"total":2, "successful":1,"failed":0}, "created":false
}
有兩種最重要的版本控制型別: 內部版本控制是以1
開頭的預設版本,每次更新都會增加,包括刪除。版本號可以在外部設定。要啟用此功能,我們需要將version_type
設定為external
。
版本控制是一個實時過程,它不受實時搜尋操作的影響。
操作型別用於強制建立操作,這有助於避免覆蓋現有文件。
POST http://localhost:9200/tutorials/chapter/1?op_type = create
請求正文
{
"Text":"this is chapter one"
}
響應內容
{
"_index":"tutorials", "_type":"chapter", "_id":"1", "_version":1,
"_shards":{"total":2, "successful":1, "failed":0}, "created":true
}
當在索引操作中未指定ID
時,Elasticsearch自動為文件生成ID
。
可以通過在父URL查詢引數中傳遞父文件的ID來定義任何文件的父級。
POST http://localhost:9200/tutorials/article/1?parent = 1
請求正文
{
"Text":"This is article 1 of chapter 1"
}
注意 - 如果在執行此範例時遇到異常,請通過在索引中新增以下內容來重新建立索引。
{ "mappings": { "chapter": {}, "article": { "_parent": { "type": "chapter" } } } }
預設情況下,索引操作將在主分片上最多等待1分鐘,超過後就會失敗並響應錯誤。 可以通過將值傳遞給timeout
引數來顯式更改這個超時值。
POST http://localhost:9200/tutorials/chapter/2?timeout = 3m
請求正文
{
"Text":"This is chapter 2 waiting for primary shard for 3 minutes"
}
API通過對特定文件執行get
請求來幫助提取JSON物件。 例如,
GET http://localhost:9200/schools/school/1
響應
{
"_index":"schools", "_type":"school", "_id":"1", "_version":2,
"found":true, "_source":{
"name":"Central School", "description":"CBSE Affiliation",
"street":"Nagan", "city":"paprola", "state":"HP", "zip":"176115",
"location":[31.8955385,76.8380405], "fees":2200,
"tags":["Senior Secondary", "beautiful campus"], "rating":"3.3"
}
}
_all
,以便Elasticsearch可以在每種型別中搜尋該文件ID
,並且它將返回第一個匹配的文件。GET http://localhost:9200/schools/school/1?fields = name,fees
響應
……………………..
"fields":{
"name":["Central School"], "fees":[2200]
}
……………………..
還可以通過在get
請求中新增_source
欄位來獲取結果中的源部分。
GET http://localhost:9200/schools/school/1/_source
響應
{
"name":"Central School", "description":"CBSE Afiliation", "street":"Nagan",
"city":"paprola", "state":"HP", "zip":"176115", "location":[31.8955385, 76.8380405],
"fees":2200, "tags":["Senior Secondary", "beatiful campus"], "rating":"3.3"
}
還可以在通過將 refresh
引數設定為true
進行get
操作之前重新整理碎片。
可以通過向Elasticsearch傳送HTTP DELETE
請求來刪除指定的索引,對映或文件。 例如,
DELETE http://localhost:9200/schools/school/4
響應
{
"found":true, "_index":"schools", "_type":"school", "_id":"4", "_version":2,
"_shards":{"total":2, "successful":1, "failed":0}
}
refresh
)和超時(timeout
)選項。指令碼用於執行此操作,版本控制用於確保在獲取和重建索引期間沒有發生更新。 例如,使用下面指令碼更新學校的費用 -
POST http://localhost:9200/schools_gov/school/1/_update
請求正文
{
"script":{
"inline": "ctx._source.fees+ = inc", "params":{
"inc": 500
}
}
}
響應結果
{
"_index":"schools_gov", "_type":"school", "_id":"1", "_version":2,
"_shards":{"total":2, "successful":1, "failed":0}
}
注意 - 如果獲取指令碼異常,建議在
elastcisearch.yml
中新增以下行
script.inline: on
script.indexed: on
可以通過向更新的文件傳送獲取請求來檢查更新。
GET http://localhost:9200/schools_gov/school/1
它具有相同的功能,如GET API
,但此get
請求可以返回多個文件。使用doc
陣列來指定需要提取的所有文件的索引,型別和ID。
POST http://localhost:9200/_mget
請求正文
{
"docs":[
{
"_index": "schools", "_type": "school", "_id": "1"
},
{
"_index":"schools_gev", "_type":"school", "_id": "2"
}
]
}
響應結果
{
"docs":[
{
"_index":"schools", "_type":"school", "_id":"1",
"_version":1, "found":true, "_source":{
"name":"Central School", "description":"CBSE Afiliation",
"street":"Nagan", "city":"paprola", "state":"HP", "zip":"176115",
"location":[31.8955385,76.8380405], "fees":2000,
"tags":["Senior Secondary", "beatiful campus"], "rating":"3.5"
}
},
{
"_index":"schools_gev", "_type":"school", "_id":"2", "error":{
"root_cause":[{
"type":"index_not_found_exception", "reason":"no such index",
"index":"schools_gev"
}],
"type":"index_not_found_exception", "reason":"no such index",
"index":"schools_gev"
}
}
]
}
此API用於通過在單個請求中進行多個索引/刪除操作來批次上傳或刪除JSON物件。 需要新增「_bulk
」關鍵字來呼叫此API。此API的範例已在Elasticsearch填充文章中執行。所有其他功能與GET API相同。