內(nèi)容來(lái)自《ES 權(quán)威指南》
[TOC]
一置逻、索引文檔
文檔通過(guò) index API 被索引--使數(shù)據(jù)可以被存儲(chǔ)和搜索玩讳。,文檔通過(guò)其 _index刮萌、 _type 驮配、 _id 唯一確定∽湃祝可以自己提供一個(gè) _id 壮锻,或者也使用 index API 生成一個(gè)。
1.1涮阔、使用自己的 ID
如果文檔有自然的標(biāo)識(shí)符(例如user_account字段或者其他值表示文檔)猜绣,就可以提供自己的_id,使用這種形式的indexAPI:
PUT /{index}/{type}/{id}
{
"field": "value",
...
}
例如索引叫做 “website” 敬特,類(lèi)型叫做 “blog” 掰邢, ID 是 “123” ,那么這個(gè)索引請(qǐng)求就像這樣:
curl -H "Content-Type: application/json" -XPUT localhost:9200/website/blog/123 -d '
{
"title": "My first blog entry",
"text": "Just trying this out...",
"date": "2014/01/01"
}'
Elasticsearch 的響應(yīng):
{
"_index": "website",
"_type": "blog",
"_id": "123",
"_version": 1,
"result": "created"
}
響應(yīng)指出請(qǐng)求的索引已經(jīng)被成功創(chuàng)建伟阔,這個(gè)索引中包含 _index 辣之、 _type 和 _id 元數(shù)據(jù),以及一個(gè)新元素: _version 皱炉。Elasticsearch中 每個(gè)文檔都有版本號(hào)怀估,每當(dāng)文檔變化(包括刪除)都會(huì)使 _version 增加。
1.2合搅、自增 ID
如果數(shù)據(jù)沒(méi)有自然 ID多搀,可以讓 Elasticsearch 自動(dòng)生成。請(qǐng)求結(jié)構(gòu)發(fā)生了變化: PUT 方法(“在這個(gè) URL 中存儲(chǔ)文檔”灾部,可以認(rèn)為是更新) 變成了 POST 方法(“在這個(gè)類(lèi)型下存儲(chǔ)文檔”康铭,符合 POST 新增的語(yǔ)義)。
URL 現(xiàn)在只包含 _index 和 _type 兩個(gè)字段:
curl -H "Content-Type: application/json" -XPOST localhost:9200/website/blog -d '
{
"title": "My second blog entry",
"text": "Still trying this out...",
"date": "2014/01/01"
}'
響應(yīng)內(nèi)容與剛才類(lèi)似赌髓,只有 _id 字段變成了自動(dòng)生成的值:
{
"_index": "website",
"_type": "blog",
"_id": "zqIzcm0BgOKExS3TRFoX",
"_version": 1,
"result": "created"
}
1.3从藤、創(chuàng)建一個(gè)新文檔
當(dāng)索引一個(gè)文檔催跪,如何確定是完全創(chuàng)建了一個(gè)新的還是覆蓋了一個(gè)已經(jīng)存在的呢?
_index 呛哟、 _type 、 _id 三者唯一確定一個(gè)文檔匿沛。所以要想保證文檔是新加入的扫责,最簡(jiǎn)單的方式是使用 POST 方法讓 Elasticsearch 自動(dòng)生成唯一 _id :
POST /website/blog/
如果想使用自定義的 _id ,必須告訴 Elasticsearch 應(yīng)該在 _index 逃呼、 _type 鳖孤、 _id 三者都不同時(shí)才接受請(qǐng)求。為了做到這點(diǎn)有兩種方法:
-
第一種方法使用 op_type 查詢(xún)參數(shù):
PUT /website/blog/123?op_type=create
-
第二種方法是在 URL 后加 /_create 做為端點(diǎn):
PUT /website/blog/123/_create
如果成功創(chuàng)建了一個(gè)新文檔抡笼,Elasticsearch 將返回正常的元數(shù)據(jù)且響應(yīng)狀態(tài)碼是 201 Created 苏揣。
如果包含相同的 _index 、 _type 和 _id 的文檔已經(jīng)存在推姻,Elasticsearch 將返
回 409 Conflict 響應(yīng)狀態(tài)碼平匈,并提示 document already exists
。
二藏古、檢索文檔
2.1增炭、檢索文檔的全部
想要從 Elasticsearch 中獲取文檔,使用同樣的 _index拧晕、 _type隙姿、 _id,但是 HTTP 方法改為 GET (在任意的查詢(xún)字符串中增加 pretty 參數(shù)厂捞,會(huì)讓 Elasticsearch 美化輸出 (pretty-print) JSON 響應(yīng)以便更加容易閱讀):
curl -XGET localhost:9200/website/blog/123?pretty
響應(yīng)包含了元數(shù)據(jù)節(jié)點(diǎn)输玷,增加了 _source 字段,它包含了在創(chuàng)建索引時(shí)發(fā)送給Elasticsearch 的原始文檔:
{
"_index" : "website",
"_type" : "blog",
"_id" : "123",
"_version" : 1,
"found" : true,
"_source" : {
"title" : "My first blog entry",
"text" : "Just trying this out...",
"date" : "2014/01/01"
}
}
2.2靡馁、檢索文檔的一部分
通常欲鹏,GET 請(qǐng)求將返回文檔的全部,存儲(chǔ)在 _source 參數(shù)中臭墨。但是可能感興趣的字段只是title貌虾。請(qǐng)求個(gè)別字段可以使用 _source 參數(shù)。多個(gè)字段可以使用逗號(hào)分隔:
curl -i -XGET localhost:9200/website/blog/123?_source=title,text
-i 可以顯示請(qǐng)求頭裙犹,_source 字段現(xiàn)在只包含請(qǐng)求的字段尽狠,過(guò)濾了date字段:
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 148
{
"_index": "website",
"_type": "blog",
"_id": "123",
"_version": 1,
"found": true,
"_source": {
"text": "Just trying this out...",
"title": "My first blog entry"
}
}
或者只想得到 _source 字段而不要其他的元數(shù)據(jù),可以這樣請(qǐng)求:
curl -XGET localhost:9200/website/blog/123/_source?pretty
它僅僅返回:
{
"title" : "My first blog entry",
"text" : "Just trying this out...",
"date" : "2014/01/01"
}
2.3叶圃、檢查文檔是否存在
如果想做的只是檢查文檔是否存在袄膏,使用 HEAD 方法來(lái)代替 GET 。 HEAD 請(qǐng)求不會(huì)返回響應(yīng)體掺冠,只有HTTP頭:
curl --head http://localhost:9200/website/blog/123
Elasticsearch 將會(huì)返回 200 OK 狀態(tài)如果文檔存在:
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 188
2.4沉馆、檢索多個(gè)文檔
檢索多個(gè)文檔依舊非陈氲常快。合并多個(gè)請(qǐng)求可以避免每個(gè)請(qǐng)求單獨(dú)的網(wǎng)絡(luò)開(kāi)銷(xiāo)斥黑。如果需要從 Elasticsearch 中檢索多個(gè)文檔揖盘,相對(duì)于一個(gè)一個(gè)的檢索,更快的方式是在一個(gè)請(qǐng)求中使用 multi-get 或者 mget API锌奴。
mgetAPI 參數(shù)是一個(gè) docs 數(shù)組兽狭,數(shù)組的每個(gè)節(jié)點(diǎn)定義一個(gè)文檔的 _index、 _type鹿蜀、 _id 元數(shù)據(jù)箕慧。如果只想檢索一個(gè)或幾個(gè)確定的字段,也可以定義一個(gè) _source 參數(shù):
curl -H "Content-Type: application/json" -XPOST localhost:9200/_mget?pretty -d '
{
"docs": [
{
"_index": "website",
"_type": "blog",
"_id": 1
},
{
"_index": "website",
"_type": "blog",
"_id": 2,
"_source": "views"
}
]
}'
響應(yīng)體也包含一個(gè) docs 數(shù)組茴恰,每個(gè)文檔還包含一個(gè)響應(yīng)颠焦,它們按照請(qǐng)求定義的順序排列。
{
"docs" : [
{
"_index" : "website",
"_type" : "blog",
"_id" : "1",
"found" : false
},
{
"_index" : "website",
"_type" : "blog",
"_id" : "2",
"_version" : 3,
"found" : true,
"_source" : {
"views" : 3
}
}
]
}
如果想檢索的文檔在同一個(gè) _index
中(甚至在同一個(gè) _type
中)往枣,就可以在 URL 中定義一個(gè)默認(rèn)的 /_index
或者 /_index/_type
:
curl -H "Content-Type: application/json" -XPOST localhost:9200/website/blog/_mget?pretty -d '
{
"docs": [
{
"_id": 1
},
{
"_type": "pageviews", "_id": 1
}
]
}'
如果所有文檔具有相同 _index 和 _type伐庭,可以通過(guò)簡(jiǎn)單的 ids 數(shù)組來(lái)代替完整的 docs 數(shù)組:
curl -H "Content-Type: application/json" -XPOST localhost:9200/website/blog/_mget?pretty -d '
{
"ids": ["2", "1"]
}'
三、更新文檔
3.1分冈、整體文檔更新
文檔在 Elasticsearch 中是不可變的--不能修改他們似忧。如果需要更新已存在的文檔,可以重建索引(reindex)丈秩,或者替換掉它盯捌。
curl -H "Content-Type: application/json" -XPUT localhost:9200/website/blog/123?pretty -d '
{
"title": "My first blog entry",
"text": "I am starting to get the hang of this...",
"date": "2014/01/02"
}'
在響應(yīng)中,可以看到 Elasticsearch 把 _version 增加了:
{
"_index" : "website",
"_type" : "blog",
"_id" : "123",
"_version" : 2,
"result" : "updated"
}
在內(nèi)部蘑秽,Elasticsearch 已經(jīng)標(biāo)記舊文檔為刪除并添加了一個(gè)完整的新文檔饺著。舊版本文檔不會(huì)立即消失,但也不能去訪(fǎng)問(wèn)它肠牲。Elasticsearch 會(huì)在繼續(xù)索引更多數(shù)據(jù)時(shí)清理被刪除的文檔幼衰。
3.2、指定版本更新文檔
Elasticsearch 是分布式的缀雳。當(dāng)文檔被創(chuàng)建渡嚣、更新或刪除,文檔的新版本會(huì)被復(fù)制到集群的其它節(jié)點(diǎn)肥印。Elasticsearch 即是同步的又是異步的识椰,意思是這些復(fù)制請(qǐng)求都是平行發(fā)送的,并無(wú)序(out of sequence)的到達(dá)目的地深碱。這就需要一種方法確保老版本的文檔永遠(yuǎn)不會(huì)覆蓋新的版本腹鹉。
執(zhí)行 index 、 get 敷硅、 delete 請(qǐng)求時(shí)功咒,每個(gè)文檔都有一個(gè) _version 號(hào)碼愉阎,這個(gè)號(hào)碼在文檔被改變時(shí)加一。Elasticsearch 使用這個(gè) _version 保證所有修改都被正確排序力奋。當(dāng)一個(gè)舊版本出現(xiàn)在新版本之后榜旦,它會(huì)被簡(jiǎn)單的忽略。
利用 _version 的這一優(yōu)點(diǎn)確保數(shù)據(jù)不會(huì)因?yàn)樾薷臎_突而丟失景殷〗δ兀可以指定文檔的 version 來(lái)做想要的更改。如果那個(gè)版本號(hào)不是現(xiàn)在的滨彻,請(qǐng)求就失敗了藕届。
創(chuàng)建一個(gè)新的博文:
curl -H "Content-Type: application/json" -XPUT localhost:9200/website/blog/1/_create -d '
{
"title": "My first blog entry",
"text": "Just trying this out..."
}'
首先檢索文檔:
curl -XGET localhost:9200/website/blog/1?pretty
響應(yīng)體包含相同的 _version 是 1:
{
"_index" : "website",
"_type" : "blog",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"title" : "My first blog entry",
"text" : "Just trying this out..."
}
}
現(xiàn)在挪蹭,當(dāng)通過(guò)重新索引文檔保存修改時(shí)亭饵,這樣指定了 version 參數(shù),只希望文檔的_version 是 1 時(shí)更新才生效:
curl -H "Content-Type: application/json" -XPUT localhost:9200/website/blog/1?version=1 -d '
{
"title": "My first blog entry",
"text": "Starting to get the hang of this..."
}'
請(qǐng)求成功梁厉,響應(yīng)體_version 已經(jīng)增加到 2 :
{
"_index": "website",
"_type": "blog",
"_id": "1",
"_version": 2,
"result": "updated"
}
如果重新運(yùn)行相同的索引請(qǐng)求辜羊,依舊指定 version=1,Elasticsearch 將返回 409
Conflict 狀態(tài)的 HTTP 響應(yīng)词顾。
3.3八秃、文檔局部更新
通過(guò)檢索,修改肉盹,然后重建整文檔的索引方法來(lái)更新文檔昔驱。
使用 update API,可以使用一個(gè)請(qǐng)求來(lái)實(shí)現(xiàn)局部更新上忍,用于添加新字段或者更新已有字段骤肛。
文檔是不可變的--它們不能被更改,只能被替換窍蓝。 update API 必須遵循相同的規(guī)則腋颠。表面看來(lái),似乎是局部更新了文檔的位置吓笙,內(nèi)部卻是像之前說(shuō)的一樣簡(jiǎn)單使用update API 處理相同的檢索-修改-重建索引流程淑玫,也減少了其他進(jìn)程可能導(dǎo)致沖突的修改。
最簡(jiǎn)單的 update 請(qǐng)求表單接受一個(gè)局部文檔參數(shù) doc 面睛,它會(huì)合并到現(xiàn)有文檔中--對(duì)象合并在一起絮蒿,存在的標(biāo)量字段被覆蓋,新字段被添加叁鉴。
可以使用以下請(qǐng)求為博客添加一個(gè) tags 字段和一個(gè) views 字段:
curl -H "Content-Type: application/json" -XPOST localhost:9200/website/blog/1/_update?pretty -d '
{
"doc": {
"tags": [
"testing"
],
"views": 0
}
}'
如果請(qǐng)求成功歌径,將看到類(lèi)似 index 請(qǐng)求的響應(yīng)結(jié)果:
{
"_index" : "website",
"_type" : "blog",
"_id" : "1",
"_version" : 3,
"result" : "updated"
檢索文檔文檔顯示被更新的 _source 字段:
{
"_index" : "website",
"_type" : "blog",
"_id" : "1",
"_version" : 3,
"found" : true,
"_source" : {
"title" : "My first blog entry",
"text" : "Starting to get the hang of this...",
"views" : 0,
"tags" : [
"testing"
]
}
}
3.4、使用腳本局部更新
當(dāng) API 不能滿(mǎn)足要求時(shí)亲茅,Elasticsearch 允許使用腳本實(shí)現(xiàn)自己的邏輯回铛。
默認(rèn)的腳本語(yǔ)言是 Groovy狗准,一個(gè)快速且功能豐富的腳本語(yǔ)言,語(yǔ)法類(lèi)似于Javascript茵肃。
腳本能夠使用 update API 改變 _source
字段的內(nèi)容腔长,它在腳本內(nèi)部以 ctx._source
表示。
可以使用腳本增加博客的 views 數(shù)量:
curl -H "Content-Type: application/json" -XPOST localhost:9200/website/blog/1/_update?pretty -d '
{
"script": "ctx._source.views+=1"
}'
還可以使用腳本增加一個(gè)新標(biāo)簽到 tags 數(shù)組中:
curl -H "Content-Type: application/json" -XPOST localhost:9200/website/blog/1/_update?pretty -d '
{
"script": {
"source": "ctx._source.tags.add(params.new_tag)",
"params": {
"new_tag": "search"
}
}
}'
3.5验残、更新可能不存在的文檔
要在 Elasticsearch 中存儲(chǔ)瀏覽量計(jì)數(shù)器捞附。每當(dāng)有用戶(hù)訪(fǎng)問(wèn)頁(yè)面,增加這個(gè)頁(yè)面的瀏覽量您没。但如果這是個(gè)新頁(yè)面鸟召,并不確定這個(gè)計(jì)數(shù)器存在與否,當(dāng)試圖更新一個(gè)
不存在的文檔氨鹏,更新將失敗欧募。
可以使用 upsert 參數(shù)定義文檔來(lái)使其不存在時(shí)被創(chuàng)建。
curl -H "Content-Type: application/json" -XPOST localhost:9200/website/blog/2/_update?pretty -d '
{
"script": "ctx._source.views+=1",
"upsert": {
"views": 1
}
}'
第一次執(zhí)行這個(gè)請(qǐng)求仆抵, upsert 值被索引為一個(gè)新文檔跟继,初始化 views 字段為 1,接下來(lái)文檔已經(jīng)存在镣丑,所以 script 被更新代替舔糖,增加 views 數(shù)量。
對(duì)于多用戶(hù)的局部更新莺匠,兩個(gè)進(jìn)程都要增加頁(yè)面瀏覽量金吗,增加的順序可以不關(guān)心,如果沖突發(fā)生趣竣,可以重新嘗試更新既可摇庙。
可以通過(guò) retry_on_conflict 參數(shù)設(shè)置重試次數(shù)來(lái)自動(dòng)完成,這樣 update 操作將會(huì)在發(fā)生錯(cuò)誤前重試--這個(gè)值默認(rèn)為0期贫。
curl -H "Content-Type: application/json" -XPOST localhost:9200/website/blog/2/_update?retry_on_conflict=5 -d '
{
"script": "ctx._source.views+=1",
"upsert": {
"views": 0
}
}'
3.6跟匆、更新時(shí)的批量操作
就像 mget 允許一次性檢索多個(gè)文檔一樣, bulk API允許使用單一請(qǐng)求來(lái)實(shí)現(xiàn)多個(gè)文檔的 create通砍、 index玛臂、 update 或 delete。這對(duì)索引類(lèi)似于日志活動(dòng)這樣的數(shù)據(jù)流非常有用封孙,它們可以以成百上千的數(shù)據(jù)為一個(gè)批次按序進(jìn)行索引迹冤。
bulk 請(qǐng)求體如下,它有一點(diǎn)不同尋常:
{ action: { metadata }}\n
{ request body }\n
{ action: { metadata }}\n
{ request body }\n
...
這種格式類(lèi)似于用 "\n" 符號(hào)連接起來(lái)的一行一行的 JSON 文檔流(stream)虎忌。
兩個(gè)重要的點(diǎn)需要注意:
這種格式類(lèi)似于用 "\n" 符號(hào)連接起來(lái)的一行一行的 JSON 文檔流(stream)泡徙。
兩個(gè)重要的點(diǎn)需要注意:
- 每行必須以 "\n" 符號(hào)結(jié)尾,包括最后一行膜蠢。這些都是作為每行有效的分離而做的標(biāo)記堪藐。
- 每一行的數(shù)據(jù)不能包含未被轉(zhuǎn)義的換行符莉兰,它們會(huì)干擾分析--這意味著 JSON 不能被美化打印。
3.6.1礁竞、action/metadata
這一行定義了文檔行為(what action)發(fā)生在哪個(gè)文檔(which document)之上糖荒。
行為(action)必須是以下幾種:
- create:當(dāng)文檔不存在時(shí)創(chuàng)建
- index:創(chuàng)建新文檔或替換已有文檔
- update:局部更新文檔
- delete:刪除一個(gè)文檔
在索引、創(chuàng)建模捂、更新或刪除時(shí)必須指定文檔的_index捶朵、 _type、 _id
這些元數(shù)據(jù)(metadata)狂男。
例如刪除請(qǐng)求看起來(lái)像這樣:
{ "delete": { "_index": "website", "_type": "blog", "_id": "123" }}
3.6.2综看、請(qǐng)求體(request body)
由文檔的 _source
組成--文檔所包含的一些字段以及其值。
- 它被 index 和 create 操作所必須岖食,這是有道理的:必須提供文檔用來(lái)索引
- 這些還被 update 操作所必需红碑,而且請(qǐng)求體的組成應(yīng)該與 update API( doc, upsert, script 等等)一致
- 刪除操作不需要請(qǐng)求體(request body)
{ "create": { "_index": "website", "_type": "blog", "_id": "123" }}
{ "title": "My first blog post" }
3.6.3、bulk 請(qǐng)求
為了將這些放在一起县耽,bulk 請(qǐng)求表單是這樣的:
curl -H "Content-Type: application/json" -XPOST localhost:9200/_bulk?pretty -d '
{ "delete": { "_index": "website", "_type": "blog", "_id": "123" }}
{ "create": { "_index": "website", "_type": "blog", "_id": "123" }}
{ "title": "My first blog post" }
{ "index": { "_index": "website", "_type": "blog" }}
{ "title": "My second blog post" }
{ "update": { "_index": "website", "_type": "blog", "_id": "123", "_retry_on_conflict" : 5}}
{ "doc" : {"title" : "My updated blog post"}}
'
Elasticsearch 響應(yīng)包含一個(gè) items 數(shù)組句喷,它羅列了每一個(gè)請(qǐng)求的結(jié)果镣典,結(jié)果的順序與我們請(qǐng)求的順序相同:
{
"took" : 1069,
"errors" : false,
"items" : [
{
"delete" : {
"_index" : "website",
"_type" : "blog",
"_id" : "123",
"_version" : 1,
"result" : "not_found",
"_shards" : {
"total" : 1,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1,
"status" : 404
}
},
{
"create" : {
"_index" : "website",
"_type" : "blog",
"_id" : "123",
"_version" : 2,
"result" : "created",
"_shards" : {
"total" : 1,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1,
"status" : 201
}
},
{
"index" : {
"_index" : "website",
"_type" : "blog",
"_id" : "_--jcm0Bsjr6Q3VWz7kw",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 1,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1,
"status" : 201
}
},
{
"update" : {
"_index" : "website",
"_type" : "blog",
"_id" : "123",
"_version" : 3,
"result" : "updated",
"_shards" : {
"total" : 1,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1,
"status" : 200
}
}
]
}
每個(gè)子請(qǐng)求都被獨(dú)立的執(zhí)行兔毙,所以一個(gè)子請(qǐng)求的錯(cuò)誤并不影響其它請(qǐng)求。如果任何一個(gè)請(qǐng)求失敗兄春,頂層的 error 標(biāo)記將被設(shè)置為 true澎剥,然后錯(cuò)誤的細(xì)節(jié)將在相應(yīng)的請(qǐng)求中被報(bào)告。
這些說(shuō)明 bulk 請(qǐng)求不是原子操作--它們不能實(shí)現(xiàn)事務(wù)赶舆。每個(gè)請(qǐng)求操作時(shí)分開(kāi)的哑姚,所以每個(gè)請(qǐng)求的成功與否不干擾其它操作。
為每個(gè)文檔指定相同的元數(shù)據(jù)是多余的芜茵。就像 mget API叙量,bulk 請(qǐng)求也可以在 URL 中使用 /_index
或 /_index/_type
:
POST /website/_bulk
{ "index": { "_type": "log" }}
{ "event": "User logged in" }
依舊可以覆蓋元數(shù)據(jù)行的 _index
和 _type
,在沒(méi)有覆蓋時(shí)它會(huì)使用 URL 中的值作為默認(rèn)值:
POST /website/log/_bulk
{ "index": {}}
{ "event": "User logged in" }
{ "index": { "_type": "blog" }}
{ "title": "Overriding the default type" }
四九串、刪除文檔
刪除文檔的語(yǔ)法模式與之前基本一致绞佩,只不過(guò)要使用 DELETE 方法:
curl -XDELETE localhost:9200/website/blog/123
如果文檔被找到,Elasticsearch 將返回 200 OK 狀態(tài)碼和以下響應(yīng)體猪钮。注意 _version 數(shù)字已經(jīng)增加了:
{
"_index": "website",
"_type": "blog",
"_id": "123",
"_version": 3,
"result": "deleted"
}