Elasticsearch對於文檔操作,提供了以下幾種API,本文就說明如何使用curl方式來調用這些API。
API種類
單文檔操作API
1、* Index API 索引文檔 *
為文檔創建索引
curl -XPUT "http://localhost:9200/twitter/tweet/1"; -H 'Content-Type: application/json' -d'
{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}'
返回結果
{
"_shards" : {
"total" : 2, #表示應該在多少個節點執行操作
"failed" : 0, #表示失敗的個數
"successful" : 2 #表示成功的個數,正常情況最小應該是1
},
"_index" : "twitter",
"_type" : "tweet",
"_id" : "1",
"_version" : 1,
"created" : true,
"result" : created
}
上面的例子,如果索引不存在,則會自動創建索引及動態映射關系。如果想要關掉這兩個特性,可以修改節點上配置文件中
action.auto_create_index
以及index.mapper.dynamic
兩項的值為 false。
使用這個API發送兩次請求,即便插入的數據一模一樣,仍然會在索引中創建兩個文檔。如果不能接受這個結果,那就需要使用 _update API,並將
detect_noop
參數打開。
2、* GET API 獲取文檔 *
該API能夠基於文檔ID獲取一份格式化的JSON文檔。除了支持通過GET獲取文檔信息,也支持通過HEAD方法檢查文檔是否存在。
curl -XGET 'localhost:9200/twitter/tweet/0?pretty'
curl -XHEAD 'localhost:9200/twitter/tweet/0?pretty'
返回結果如下
{
"_index" : "twitter",
"_type" : "tweet",
"_id" : "0",
"_version" : 1,
"found": true,
"_source" : {
"user" : "kimchy",
"date" : "2009-11-15T14:12:12",
"likes": 0,
"message" : "trying out Elasticsearch"
}
}
3、* Delete API 刪除文檔 *
該API允許我們根據ID刪除某個索引中的文檔。
curl -XDELETE 'localhost:9200/twitter/tweet/1?pretty'
結果如下
{
"_shards" : {
"total" : 2,
"failed" : 0,
"successful" : 2
},
"found" : true,
"_index" : "twitter",
"_type" : "tweet",
"_id" : "1",
"_version" : 2,
"result": "deleted"
}
刪除文檔時,如果該索引不存在,則Elasticsearch會自動創建索引和自動映射關系。這個官方文檔中有這個文字,但是我自己實驗的卻沒有這樣的結果,而是收到 index_not_found_exception 的錯誤。
4、* Delete By Query API 根據條件刪除 *
該API會對滿足查詢條件的所有文檔執行刪除操作。示例如下
curl -XPOST 'localhost:9200/twitter/_delete_by_query?pretty' -H 'Content-Type: application/json' -d'
{
"query": {
"match": {
"message": "some message"
}
}
}
'
返回結果
{
"took" : 147, #
"timed_out": false,
"deleted": 119,
"batches": 1,
"version_conflicts": 0,
"noops": 0,
"retries": {
"bulk": 0,
"search": 0
},
"throttled_millis": 0,
"requests_per_second": -1.0,
"throttled_until_millis": 0,
"total": 119,
"failures" : [ ]
}
該API接受的URL參數pretty
、refresh
、wait_for_completion
、wait_for_active_shards
、timeout
。如果想要獲取正在執行的刪除人物,可以通過Task API。
curl -XGET 'localhost:9200/_tasks?detailed=true&actions=*/delete/byquery&pretty'
也可以對刪除操作進行取消。
curl -XPOST 'localhost:9200/_tasks/task_id:1/_cancel?pretty'
5、* Update API 更新API *
我覺得這是很多人對ELK誤解最深的地方,以為ELK不支持更新功能,數據只能一次性導入,其實ELK是有更新API的。更新API首先從ES獲取文檔,然后根據請求對文檔進行更新,最后將更新保存至服務器。這個過程中使用版本號 Version 來確保文檔沒有被其他人修改過。
curl -XPUT 'localhost:9200/test/type1/1?pretty' -H 'Content-Type: application/json' -d'
{
"counter" : 1,
"tags" : ["red"]
}
'
也可以通過Script進行更新
curl -XPOST 'localhost:9200/test/type1/1/_update?pretty' -H 'Content-Type: application/json' -d'
{
"script" : {
"source": "ctx._source.counter += params.count",
"lang": "painless",
"params" : {
"count" : 4
}
}
}
'
6、* Update by Query API 根據條件更新 *
類似於根據條件查詢,這個API可以根據條件對多個文檔進行更新。
curl -XPOST 'localhost:9200/twitter/_update_by_query?conflicts=proceed&pretty' -H 'Content-Type: application/json' -d'
{
"query": {
"term": {
"user": "kimchy"
}
}
}
'
多文檔操作API
1、* MULTI GET API 獲取多個文檔*
MULTI GET API允許我們根據索引、類型和ID來獲取多個文檔,返回結果放在docs數組中。
curl -XGET 'localhost:9200/_mget?pretty' -H 'Content-Type: application/json' -d'
{
"docs" : [
{
"_index" : "test",
"_type" : "type",
"_id" : "1"
},
{
"_index" : "test",
"_type" : "type",
"_id" : "2"
}
]
}
'
2、BULK API
BULK API提供了在一次請求中更新大量文檔的可能,這將極大的提高索引的速度。
3、Reindex API 重建索引
_reindex
的基本工作方式是將一個索引拷貝到新的索引中。
curl -XPOST 'localhost:9200/_reindex?pretty' -H 'Content-Type: application/json' -d'
{
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter"
}
}
'
3、Term Vectors
TODO 待補充
4、Multi termvectors API
TODO 待補充
5、?refresh
TODO 待補充
本文所有示例基於ELK 5.6。
本文為作者原創,未經允許不得轉載。如果您覺得本文對您有幫助,請隨意打賞,您的支持將鼓勵我繼續創作。