在es7中索引就相當(dāng)于是一張表绢陌,mapping相當(dāng)于表結(jié)構(gòu),doucoument相當(dāng)于是表中的數(shù)據(jù)熔恢。
在es7之前脐湾,索引支持多種type,所以索引相當(dāng)于是一個(gè)數(shù)據(jù)庫叙淌,type相當(dāng)于是一張表秤掌,type下的document相當(dāng)于表中的數(shù)據(jù)。
Document的創(chuàng)建與更新
索引中的數(shù)據(jù)叫做document鹰霍,和數(shù)據(jù)中的一條記錄是一樣的闻鉴,而索引就像數(shù)據(jù)庫中的一張表,我們向索引中添加數(shù)據(jù)茂洒,就像在數(shù)據(jù)庫表中添加一條記錄一樣孟岛。下面我們看看怎么向索引中添加數(shù)據(jù),
PUT /<index>/_doc/<_id>
POST /<index>/_doc/
PUT /<index>/_create/<_id>
POST /<index>/_create/<_id>
在這個(gè)POST請(qǐng)求中督勺,<index>也就是索引的名字是必須的蚀苛,這就好比我們向數(shù)據(jù)庫中插入記錄,要知道往哪張表里插是一樣的玷氏。<index>后面可以是_doc或者_(dá)create,這兩個(gè)是什么意思呢腋舌?咱們慢慢看盏触,除了這兩個(gè)區(qū)別以外,再有就是請(qǐng)求的方法了块饺,分為POST和PUT兩種赞辩。一般情況下,POST用于數(shù)據(jù)的插入授艰,PUT用戶數(shù)據(jù)的修改辨嗽,是不是這樣呢?咱們把這4種方式都試一下淮腾,首先我們看一下POST /<index>/_doc/這種方式的請(qǐng)求糟需。
首先嘗試POST相關(guān)的操作
POST /ik_index/_doc
{
"id": 1,
"title": "蘋果",
"desc": "蘋果真好吃"
}
在這里,索引我們使用的是上一節(jié)創(chuàng)建的ik_index谷朝,執(zhí)行一下洲押。然后我們?cè)俨樵円幌逻@個(gè)索引,
GET /ik_index/_search
返回結(jié)果如下:
{
"took": 1000,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "ik_index",
"_type": "_doc",
"_id": "1",
"_score": 1,
"_source": {
"id": 1,
"title": "大興龐各莊的西瓜",
"desc": "大興龐各莊的西瓜真是好吃圆凰,脆沙瓤杈帐,甜掉牙"
}
},
{
"_index": "ik_index",
"_type": "_doc",
"_id": "fEsN-HEBZl0Dh1ayKWZb",
"_score": 1,
"_source": {
"id": 1,
"title": "蘋果",
"desc": "蘋果真好吃"
}
}
]
}
}
我們重點(diǎn)看一下hits,這是我們查詢出的結(jié)果,第一條是我們上一節(jié)存入的數(shù)據(jù)挑童,不用管它累铅。我們看一下第二條記錄,注意一下_id這個(gè)字段站叼,這個(gè)_id是這條記錄在索引里的唯一標(biāo)識(shí)娃兽,在插入數(shù)據(jù)的請(qǐng)求中,我們沒有指定這個(gè)id大年,ES給我們自動(dòng)生成了fEsN-HEBZl0Dh1ayKWZb换薄。那么我們可不可以指定呢?試一下翔试,
POST /ik_index/_doc/2
{
"id": 1,
"title": "香蕉",
"desc": "香蕉真好吃"
}
注意我們發(fā)送的請(qǐng)求轻要,_doc后面加了2,這樣就指定了id垦缅,執(zhí)行一下冲泥。然后再次查詢,返回的結(jié)果中壁涎,我們只截取hits的部分凡恍,如下:
"hits": [
{
"_index": "ik_index",
"_type": "_doc",
"_id": "1",
"_score": 1,
"_source": {
"id": 1,
"title": "大興龐各莊的西瓜",
"desc": "大興龐各莊的西瓜真是好吃,脆沙瓤怔球,甜掉牙"
}
},
{
"_index": "ik_index",
"_type": "_doc",
"_id": "fEsN-HEBZl0Dh1ayKWZb",
"_score": 1,
"_source": {
"id": 1,
"title": "蘋果",
"desc": "蘋果真好吃"
}
},
{
"_index": "ik_index",
"_type": "_doc",
"_id": "2",
"_score": 1,
"_source": {
"id": 1,
"title": "香蕉",
"desc": "香蕉真好吃"
}
}
]
我們看到插入的香蕉記錄嚼酝,它的_id是2。那么POST請(qǐng)求中指定的id在索引中存在竟坛,會(huì)是什么情況呢闽巩?我們?cè)倏匆幌拢?/p>
POST /ik_index/_doc/1
{
"id": 1,
"title": "香蕉",
"desc": "香蕉真好吃"
}
還是香蕉這條數(shù)據(jù),我們指定id=1担汤,id=1這條數(shù)據(jù)在索引中是存在的涎跨,我們執(zhí)行一下,然后查詢崭歧,返回的結(jié)果如下:
"hits": [
{
"_index": "ik_index",
"_type": "_doc",
"_id": "fEsN-HEBZl0Dh1ayKWZb",
"_score": 1,
"_source": {
"id": 1,
"title": "蘋果",
"desc": "蘋果真好吃"
}
},
{
"_index": "ik_index",
"_type": "_doc",
"_id": "2",
"_score": 1,
"_source": {
"id": 1,
"title": "香蕉",
"desc": "香蕉真好吃"
}
},
{
"_index": "ik_index",
"_type": "_doc",
"_id": "1",
"_score": 1,
"_source": {
"id": 1,
"title": "香蕉",
"desc": "香蕉真好吃"
}
}
]
我們看到之前的那條數(shù)據(jù)被修改了隅很,所以,關(guān)于POST /<index>/_doc/<_id>率碾,這種添加數(shù)據(jù)的方式叔营,我們得出結(jié)論如下:
<_id>不指定時(shí),ES會(huì)為我們自動(dòng)生成id所宰;
指定<_id>時(shí)审编,且id在索引中不存在,ES將添加一條指定id的數(shù)據(jù)歧匈;
指定<_id>時(shí)垒酬,但id在索引中存在,ES將會(huì)更新這條數(shù)據(jù);
接下來我們?cè)倏纯確doc方式的PUT請(qǐng)求方式勘究,我們先不指定id矮湘,看看會(huì)是什么情況,請(qǐng)求如下:
PUT /ik_index/_doc
{
"id": 1,
"title": "葡萄",
"desc": "葡萄真好吃"
}
執(zhí)行一下口糕,返回如下結(jié)果:
{
"error": "Incorrect HTTP method for uri [/ik_index/_doc] and method [PUT], allowed: [POST]",
"status": 405
}
錯(cuò)誤信息說我們的請(qǐng)求不對(duì)缅阳,讓我們使用POST請(qǐng)求,看來PUT請(qǐng)求不指定id是不行的景描。我們?cè)倏纯粗付ㄒ粋€(gè)不存在的id十办,是什么情況,如下:
PUT /ik_index/_doc/3
{
"id": 1,
"title": "葡萄",
"desc": "葡萄真好吃"
}
執(zhí)行成功超棺,再查詢一下向族,
"hits": [
……
{
"_index": "ik_index",
"_type": "_doc",
"_id": "3",
"_score": 1,
"_source": {
"id": 1,
"title": "葡萄",
"desc": "葡萄真好吃"
}
}
]
數(shù)據(jù)添加成功。再看看指定一個(gè)存在的id是什么情況棠绘,那當(dāng)然是修改了件相,我們?cè)僭囈幌拢?/p>
PUT /ik_index/_doc/3
{
"id": 1,
"title": "橘子",
"desc": "橘子真好吃"
}
執(zhí)行成功,再查詢一下氧苍,
"hits": [
……
{
"_index": "ik_index",
"_type": "_doc",
"_id": "3",
"_score": 1,
"_source": {
"id": 1,
"title": "橘子",
"desc": "橘子真好吃"
}
}
]
沒有問題夜矗,修改成功。PUT /<index>/_doc/<_id>這種方式的總結(jié)如下:
<_id>必須指定让虐,不指定會(huì)報(bào)錯(cuò)紊撕;
<_id>在索引中不存在,為添加新數(shù)據(jù)赡突;
<_id>在索引中存在逛揩,為修改數(shù)據(jù);
_doc這種請(qǐng)求的POST和PUT都嘗試過了麸俘,再看看_create這種請(qǐng)求,先看看不指定id是什么情況惧笛,如下:
POST /ik_index/_create
{
"id": 1,
"title": "桃子",
"desc": "桃子真好吃"
}
返回錯(cuò)誤信息如下:
{
"error": {
"root_cause": [
{
"type": "invalid_type_name_exception",
"reason": "mapping type name [_create] can't start with '_' unless it is called [_doc]"
}
],
"type": "invalid_type_name_exception",
"reason": "mapping type name [_create] can't start with '_' unless it is called [_doc]"
},
"status": 400
}
具體內(nèi)容我們也不去解讀了从媚,總之是不可以,然后加個(gè)索引中不存在id試一下患整,
POST /ik_index/_create/4
{
"id": 1,
"title": "桃子",
"desc": "桃子真好吃"
}
返回結(jié)果創(chuàng)建成功拜效,查詢?nèi)缦拢?/p>
"hits": [
……
{
"_index": "ik_index",
"_type": "_doc",
"_id": "4",
"_score": 1,
"_source": {
"id": 1,
"title": "桃子",
"desc": "桃子真好吃"
}
}
]
如果id在索引中存在呢?再試各谚,
POST /ik_index/_create/3
{
"id": 1,
"title": "桃子",
"desc": "桃子真好吃"
}
返回錯(cuò)誤:
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[3]: version conflict, document already exists (current version [2])",
"index_uuid": "W2X_riHIT4u678p8HZwnEg",
"shard": "0",
"index": "ik_index"
}
],
"type": "version_conflict_engine_exception",
"reason": "[3]: version conflict, document already exists (current version [2])",
"index_uuid": "W2X_riHIT4u678p8HZwnEg",
"shard": "0",
"index": "ik_index"
},
"status": 409
}
大致的意思是紧憾,數(shù)據(jù)已經(jīng)存在了,不能再添加新記錄昌渤,看來_create這種方式還是比較嚴(yán)格的赴穗,總結(jié)如下:
id必須指定;
指定的id如果在索引中存在,報(bào)錯(cuò)般眉,添加不成功了赵;
指定的id在索引中不存在,添加成功甸赃,符合預(yù)期柿汛;
POST的_create只適用于增加數(shù)據(jù);
再看看_create的PUT埠对,應(yīng)該和POST正好相反吧络断?我們?cè)囈幌拢炔恢付╥d项玛,試一下貌笨,
PUT /ik_index/_create
{
"id": 1,
"title": "火龍果",
"desc": "火龍果真好吃"
}
返回錯(cuò)誤,不指定id肯定是不行的稍计,錯(cuò)誤信息就不給大家貼出來了躁绸,然后再指定一個(gè)不存在的id,
PUT /ik_index/_create/5
{
"id": 1,
"title": "火龍果",
"desc": "火龍果真好吃"
}
創(chuàng)建成功臣嚣,查詢結(jié)果就不給大家展示了净刮,然后再換一個(gè)存在的id,如下:
PUT /ik_index/_create/4
{
"id": 1,
"title": "火龍果",
"desc": "火龍果真好吃"
}
返回了錯(cuò)誤的信息硅则,如下淹父,和POST請(qǐng)求是一樣的,
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[4]: version conflict, document already exists (current version [1])",
"index_uuid": "W2X_riHIT4u678p8HZwnEg",
"shard": "0",
"index": "ik_index"
}
],
"type": "version_conflict_engine_exception",
"reason": "[4]: version conflict, document already exists (current version [1])",
"index_uuid": "W2X_riHIT4u678p8HZwnEg",
"shard": "0",
"index": "ik_index"
},
"status": 409
}
我們得出如下的結(jié)論:
_create這種形式的POST和PUT是一樣的怎虫,沒有區(qū)別暑认;
id必須指定;
id必須在索引中不存在,不存在時(shí)為添加操作大审;
id若存在蘸际,則報(bào)錯(cuò);
Document的刪除
有了添加徒扶,肯定會(huì)有刪除粮彤,刪除的方式很簡(jiǎn)單,請(qǐng)求格式如下:
DELETE /<index>/_doc/<_id>
發(fā)送delete請(qǐng)求姜骡,指定數(shù)據(jù)的id导坟,就可以了,我們?cè)囈幌氯Τ海瑒h除剛剛添加的火龍果數(shù)據(jù)惫周,它的id是5,我們發(fā)送請(qǐng)求如下:
DELETE /ik_index/_doc/5
執(zhí)行成功康栈,數(shù)據(jù)被成功的刪除递递。
根據(jù)id查詢Document
根據(jù)id查詢數(shù)據(jù)也很簡(jiǎn)單喷橙,發(fā)送如下請(qǐng)求就可以完成查詢,
GET <index>/_doc/<_id>
我們需要指定索引的名稱漾狼,以及要查詢數(shù)據(jù)的id重慢,如下:
GET ik_index/_doc/3
返回結(jié)果如下:
{
"_index": "ik_index",
"_type": "_doc",
"_id": "3",
"_version": 2,
"_seq_no": 5,
"_primary_term": 3,
"found": true,
"_source": {
"id": 1,
"title": "橘子",
"desc": "橘子真好吃"
}
}
根據(jù)id成功的查詢出來結(jié)果。
總結(jié):POST的_doc模式既能增加數(shù)據(jù)逊躁,也能修改數(shù)據(jù)似踱。PUT的_doc模式既能增加數(shù)據(jù),也能修改數(shù)據(jù)稽煤。POST和PUT的_create模式只能指定id增加數(shù)據(jù)核芽。