elasticsearch的CURD
CURD之C
让我们将镜头切换到北宋时期某位官人的府邸,府里男主人是:
PUT zhifou/doc/1 { "name":"顾老二", "age":30, "from": "gu", "desc": "皮肤黑、武器长、性格直", "tags": ["黑", "长", "直"] }
他明处貌似还有俩老婆:
PUT zhifou/doc/2 { "name":"大娘子", "age":18, "from":"sheng", "desc":"肤白貌美,娇憨可爱", "tags":["白", "富","美"] } PUT zhifou/doc/3 { "name":"龙套偏房", "age":22, "from":"gu", "desc":"mmp,没怎么看,不知道怎么形容", "tags":["造数据", "真","难"] }
家里红旗不倒,家外彩旗飘摇:
PUT zhifou/doc/4 { "name":"石头", "age":29, "from":"gu", "desc":"粗中有细,狐假虎威", "tags":["粗", "大","猛"] } PUT zhifou/doc/5 { "name":"魏行首", "age":25, "from":"广云台", "desc":"仿佛兮若轻云之蔽月,飘飘兮若流风之回雪,mmp,最后竟然没有嫁给顾老二!", "tags":["闭月","羞花"] }
注意:当执行PUT
命令时,如果数据不存在,则新增该条数据,如果数据存在则修改该条数据。
咱们通过GET
命令查询一下:
GET zhifou/doc/1
结果如下:
{ "_index" : "zhifou", "_type" : "doc", "_id" : "1", "_version" : 1, "found" : true, "_source" : { "name" : "顾老二", "age" : 30, "from" : "gu", "desc" : "皮肤黑、武器长、性格直", "tags" : [ "黑", "长", "直" ] } }
查询也没啥问题,但是你可能说了,人家老二是黄种人,怎么是黑的呢?好吧咱改改desc
和tags
:
PUT zhifou/doc/1 { "desc":"皮肤很黄,武器很长,性格很直", "tags":["很黄","很长", "很直"] }
上例,我们仅修改了desc
和tags
两处,而name
、age
和from
三个属性没有变化,我们可以忽略不写吗?查查看:
GET zhifou/doc/1
结果如下:
{ "_index" : "zhifou", "_type" : "doc", "_id" : "1", "_version" : 3, "found" : true, "_source" : { "desc" : "皮肤很黄,武器很长,性格很直", "tags" : [ "很黄", "很长", "很直" ] } }
CURD之U
让我们首先恢复一下事故现场:
PUT zhifou/doc/1 { "name":"顾老二", "age":30, "from": "gu", "desc": "皮肤黑、武器长、性格直", "tags": ["黑", "长", "直"] }
我们要将黑修改成黄:
POST zhifou/doc/1/_update { "doc": { "desc": "皮肤很黄,武器很长,性格很直", "tags": ["很黄","很长", "很直"] } }
上例中,我们使用POST
命令,在id
后面跟_update
,要修改的内容放到doc
文档(属性)中即可。
我们再来查询一次:
GET zhifou/doc/1
结果如下:
{ "_index" : "zhifou", "_type" : "doc", "_id" : "1", "_version" : 5, "found" : true, "_source" : { "name" : "顾老二", "age" : 30, "from" : "gu", "desc" : "皮肤很黄,武器很长,性格很直", "tags" : [ "很黄", "很长", "很直" ] } }
结果如上例所示,现在其他的属性没有变化,只有desc
和tags
属性被修改。
注意:POST
命令,这里可用来执行修改操作(还有其他的功能),POST
命令配合_update
完成修改操作,指定修改的内容放到doc
中。
写了这么多,我也发现我上面有讲的不对
的地方——石头不是跟顾老二不清不楚,石头是跟小桃不清不楚!好吧,刚才那个数据是一个错误示范!我们这就把它干掉!
CURD之D
DELETE zhifou/doc/4
很简单,通过DELETE
命令,就可以删除掉那个错误示范了!
删除效果如下:
{ "_index" : "zhifou", "_type" : "doc", "_id" : "4", "_version" : 4, "result" : "deleted", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 4, "_primary_term" : 1 }
我们再来查询一遍:
GET zhifou/doc/4
结果如下:
{ "_index" : "zhifou", "_type" : "doc", "_id" : "4", "found" : false }
上例中,found:false
表示查询数据不存在。
CURD之R
我们上面已经不知不觉的使用熟悉这种简单查询方式,通过 GET
命令查询指定文档:
GET zhifou/doc/1
结果如下:
{ "_index" : "zhifou", "_type" : "doc", "_id" : "1", "_version" : 5, "found" : true, "_source" : { "name" : "顾老二", "age" : 30, "from" : "gu", "desc" : "皮肤很黄,武器很长,性格很直", "tags" : [ "很黄", "很长", "很直" ] } }