ES[7.6.x]学习笔记(八)数据的增删改 (2)

错误信息说我们的请求不对,让我们使用POST请求,看来PUT请求不指定id是不行的。我们再看看指定一个不存在的id,是什么情况,如下:

PUT /ik_index/_doc/3 { "id": 1, "title": "葡萄", "desc": "葡萄真好吃" }

执行成功,再查询一下,

"hits": [ …… { "_index": "ik_index", "_type": "_doc", "_id": "3", "_score": 1, "_source": { "id": 1, "title": "葡萄", "desc": "葡萄真好吃" } } ]

数据添加成功。再看看指定一个存在的id是什么情况,那当然是修改了,我们再试一下,

PUT /ik_index/_doc/3 { "id": 1, "title": "橘子", "desc": "橘子真好吃" }

执行成功,再查询一下,

"hits": [ …… { "_index": "ik_index", "_type": "_doc", "_id": "3", "_score": 1, "_source": { "id": 1, "title": "橘子", "desc": "橘子真好吃" } } ]

没有问题,修改成功。POST /<index>/_doc/<_id>这种方式的总结如下:

<_id>必须指定,不指定会报错;

<_id>在索引中不存在,为添加新数据;

<_id>在索引中存在,为修改数据;

_doc这种请求的POST和PUT都尝试过了,再看看_create这种请求,先看看不指定id是什么情况,如下:

POST /ik_index/_create { "id": 1, "title": "桃子", "desc": "桃子真好吃" }

返回错误信息如下:

{ "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 }

具体内容我们也不去解读了,总之是不可以,然后加个索引中不存在id试一下,

POST /ik_index/_create/4 { "id": 1, "title": "桃子", "desc": "桃子真好吃" }

返回结果创建成功,查询如下:

"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": "桃子真好吃" }

返回错误:

{ "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 }

大致的意思是,数据已经存在了,不能再添加新记录,看来_create这种方式还是比较严格的,总结如下:

id必须指定;

指定的id如果在索引中存在,报错,添加不成功;

指定的id在索引中不存在,添加成功,符合预期;

再看看_create的PUT,应该和POST正好相反吧?我们试一下,先不指定id,试一下,

PUT /ik_index/_create { "id": 1, "title": "火龙果", "desc": "火龙果真好吃" }

返回错误,不指定id肯定是不行的,错误信息就不给大家贴出来了,然后再指定一个不存在的id,

PUT /ik_index/_create/5 { "id": 1, "title": "火龙果", "desc": "火龙果真好吃" }

创建成功,查询结果就不给大家展示了,然后再换一个存在的id,如下:

PUT /ik_index/_create/4 { "id": 1, "title": "火龙果", "desc": "火龙果真好吃" }

返回了错误的信息,如下,和POST请求是一样的,

{ "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 }

我们得出如下的结论:

_create这种形式的POST和PUT是一样的,没有区别;

id必须指定;

id必须在索引中不存在;

Document的删除

有了添加,肯定会有删除,删除的方式很简单,请求格式如下:

DELETE /<index>/_doc/<_id>

发送delete请求,指定数据的id,就可以了,我们试一下,删除刚刚添加的火龙果数据,它的id是5,我们发送请求如下:

DELETE /ik_index/_doc/5

执行成功,数据被成功的删除。

根据id查询Document

根据id查询数据也很简单,发送如下请求就可以完成查询,

GET <index>/_doc/<_id>

我们需要指定索引的名称,以及要查询数据的id,如下:

GET ik_index/_doc/3

返回结果如下:

{ "_index": "ik_index", "_type": "_doc", "_id": "3", "_version": 2, "_seq_no": 5, "_primary_term": 3, "found": true, "_source": { "id": 1, "title": "橘子", "desc": "橘子真好吃" } }

根据id成功的查询出来结果。

好了~ 到这里,ES数据的增删改都介绍了,下节开始,我们看看ES的核心功能——搜索。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpszdj.html