Elasticsearch提供單文檔API和多文檔API,其中API調用分別針對單個文檔和多個文檔。html
當使用特定映射對相應索引起出請求時,它有助於在索引中添加或更新JSON文檔。 例如,如下請求將JSON對象添加到索引爲schools
和映射爲school
下。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" }
響應結果以下圖所示:
api
當請求將JSON對象添加到特定索引時,若是該索引不存在,那麼此API會自動建立該索引以及該特定JSON對象的基礎映射。 能夠經過將如下參數的值更改成false
來禁用此功能,這個值是存在於elasticsearch.yml
文件中,打開elasticsearch.yml
文件設置以下 。數組
action.auto_create_index:false //限制了自動建立索引 index.mapper.dynamic:false //限制了自動建立基礎映射
還能夠限制自動建立索引,其中經過更改如下參數的值只容許指定模式的索引名稱 (其中+
表示容許, -
表示不容許)markdown
action.auto_create_index:+acc*,-bank*
Elasticsearch還提供版本控制功能。咱們可使用版本查詢參數指定特定文檔的版本。 咱們舉個舉例來講明併發
先構造一條數據出來app
PUT /test_index/test_type/1 { "test_field": "test test" }
模擬兩個客戶端,都獲取到了同一條數據yii
GET test_index/test_type/1
返回結果:elasticsearch
{ "_index": "test_index", "_type": "test_type", "_id": "1", "_version": 1, //此時版本號爲1 "found": true, "_source": { "test_field": "test test" } }
其中一個客戶端執行下面的代碼,先更新了一下這個數據,帶上數據的版本號,確保說,es中的數據的版本號,跟客戶端中的數據的版本號是相同的,才能修改。ide
PUT /test_index/test_type/1?version=1 { "test_field": "test client 1" }
執行完後,返回結果以下(更新完後,版本號會變成2)
{ "_index": "test_index", "_type": "test_type", "_id": "1", "_version": 2, //此時版本號已經變成了2 "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "created": false }
另一個客戶端,嘗試基於version=1的數據去進行修改,一樣帶上version版本號,進行樂觀鎖的併發控制。
PUT /test_index/test_type/1?version=1 { "test_field": "test client 2" }
會發現會返回錯誤信息:(由於版本號已經變成了2,不是1了,要想修改,就要令version=2才行)
{ "error": { "root_cause": [ { "type": "version_conflict_engine_exception", "reason": "[test_type][1]: version conflict, current version [2] is different than the one provided [1]", "index_uuid": "ys-Y1QWrRmWTK2JSGuXgww", "shard": "3", "index": "test_index" } ], "type": "version_conflict_engine_exception", "reason": "[test_type][1]: version conflict, current version [2] is different than the one provided [1]", "index_uuid": "ys-Y1QWrRmWTK2JSGuXgww", "shard": "3", "index": "test_index" }, "status": 409 }
版本號能夠在外部設置。要啓用此功能,咱們須要將version_type
設置爲external
。(具體是怎麼樣的,能夠自行百度)
版本控制是一個實時過程,它不受實時搜索操做的影響。
當在建立索引操做中未指定ID
時,Elasticsearch自動爲文檔生成ID
。
默認狀況下,索引操做將在主分片上最多等待1分鐘,超事後就會失敗並響應錯誤。 能夠經過將值傳遞給timeout
參數來顯式更改這個超時值。
//索引名爲「tutorials」,映射名爲「chapter」 POST http://localhost:9200/tutorials/chapter/2?timeout = 3m { "Text":"This is chapter 2 waiting for primary shard for 3 minutes" }
API經過對特定文檔執行get
請求來幫助提取JSON對象。 例如,
//索引名爲schools,映射名爲school 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" } }
GET http://localhost:9200/schools/school/1?version=3
,而後Elasticsearch將僅提取該版本的文檔_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" }
能夠經過向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
)選項。腳本用於執行此操做,版本控制用於確保在獲取和重建索引期間沒有發生更新。 例如,使用下面腳本更新學校的費用
//URL後面跟了update表示「更新」操做 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} }
能夠經過向更新的文檔發送獲取請求來檢查更新。
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相同。
本文轉載自:https://www.yiibai.com/elasticsearch/elasticsearch_document_apis.html