elasticsearch从入门到精通(三)—— 简单CRUD
1、文档通过其_index
、_type
、_id
唯一确定
PUT /{index}/{type}/{id} { "field": "value", ... }
2、不指定id时es会为我们生成一个唯一值UUIDs作为ID
POST /website/blog/ { "title": "My second blog entry", "text": "Still trying this out...", "date": "2014/01/01" }
3、运行结果;
{ "_index" : "website", "_type" : "blog", "_id" : "lHzAlGwBcJMdeOw1bdOy", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 1 }
自动生成的ID有22个字符长,URL-safe, Base64-encoded string universally unique identifiers, 或者叫 UUIDs。
4、查询
GET /website/blog/123?pretty
查询结果:
{ "_index" : "website", "_type" : "blog", "_id" : "123", "_version" : 1, "_seq_no" : 1, "_primary_term" : 1, "found" : true, "_source" : { "title" : "My first blog entry", "text" : "Just trying this out...", "date" : "2014/01/01" } }
5、更新
PUT /website/blog/123 { "title": "My first blog entry", "text": "I am starting to get the hang of this...", "date": "2014/01/02" }
运行结果:
{ "_index" : "website", "_type" : "blog", "_id" : "123", "_version" : 2, "result" : "updated", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 2, "_primary_term" : 1 }
在响应中,我们可以看到Elasticsearch把_version
增加了。在内部,Elasticsearch已经标记旧文档为删除并添加了一个完整的新文档。旧版本文档不会立即消失,但你也不能去访问它。Elasticsearch会在你继续索引更多数据时清理被删除的文档。
6、删除
DELETE /website/blog/123
运行结果:
{ "_index" : "website", "_type" : "blog", "_id" : "123", "_version" : 3, "result" : "deleted", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 3, "_primary_term" : 1 }
7、局部更新
POST /website/blog/123/_update { "doc" : { "tags" : [ "testing" ], "views": 0 } }
运行结果:
{ "_index" : "website", "_type" : "blog", "_id" : "123", "_version" : 2, "result" : "updated", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 5, "_primary_term" : 1 }
查询结果:
{ "_index" : "website", "_type" : "blog", "_id" : "123", "_version" : 2, "_seq_no" : 5, "_primary_term" : 1, "found" : true, "_source" : { "title" : "My first blog entry", "text" : "Just trying this out...", "date" : "2014/01/01", "views" : 0, "tags" : [ "testing" ] } }
我们新添加的字段已经被添加到`_source`字段中
8、检索多个文档
POST /_mget { "docs" : [ { "_index" : "website", "_type" : "blog", "_id" : 123 }, { "_index" : "website", "_type" : "pageviews", "_id" : 1, "_source": "views" } ] }
运行结果
{ "docs" : [ { "_index" : "website", "_type" : "blog", "_id" : "123", "_version" : 2, "_seq_no" : 5, "_primary_term" : 1, "found" : true, "_source" : { "title" : "My first blog entry", "text" : "Just trying this out...", "date" : "2014/01/01", "views" : 0, "tags" : [ "testing" ] } }, { "_index" : "website", "_type" : "pageviews", "_id" : "1", "found" : false } ] }
9、批量操作
POST _bulk { "index" : { "_index" : "test", "_id" : "1" } } { "field1" : "value1" } { "delete" : { "_index" : "test", "_id" : "2" } } { "create" : { "_index" : "test", "_id" : "3" } } { "field1" : "value3" } { "update" : {"_id" : "1", "_index" : "test"} } { "doc" : {"field2" : "value2"} }
运行结果;
{ "took" : 654, "errors" : false, "items" : [ { "index" : { "_index" : "test", "_type" : "_doc", "_id" : "1", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 1, "status" : 201 } }, { "delete" : { "_index" : "test", "_type" : "_doc", "_id" : "2", "_version" : 1, "result" : "not_found", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 1, "_primary_term" : 1, "status" : 404 } }, { "create" : { "_index" : "test", "_type" : "_doc", "_id" : "3", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 2, "_primary_term" : 1, "status" : 201 } }, { "update" : { "_index" : "test", "_type" : "_doc", "_id" : "1", "_version" : 2, "result" : "updated", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 3, "_primary_term" : 1, "status" : 200 } } ] }
文章参考:《Elasticsearch 权威指南(中文版)》