ELasticsearch文档的CRUD主要包括以下2个大的方面:单文档和多文档,翻译如下:
一、单文档API
1.1 Index API
写入文档,索引为twitter,type为tweet,id为1:
PUT twitter/tweet/1
{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}
返回结果:
{
"_index": "twitter",
"_type": "tweet",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
1.2 Get API
get api可以通过id查看文档:
GET twitter/tweet/0
查看文档是否存在:
HEAD twitter/tweet/0
1.3 Delete API
根据ID删除:
DELETE twitter/tweet/1
1.4 Update API
Update API允许通过脚本更新文档,更新操作会先读取文档,执行脚本,最后重新索引。更新操作意味着重新索引文档,当然执行更新操作不能关闭_source字段。
写入一条文档做测试:
PUT test/type1/1
{
"counter" : 1,
"tags" : ["red"]
}
把counter的值更新为5,执行脚本如下:
POST test/type1/1/_update
{
"script" : {
"inline": "ctx._source.counter += params.count",
"lang": "painless",
"params" : {
"count" : 4
}
}
}
给tags增加一个值:
POST test/type1/1/_update
{
"script" : {
"inline": "ctx._source.tags.add(params.tag)",
"lang": "painless",
"params" : {
"tag" : "blue"
}
}
}
通过ctx可以操作_index
, _type
, _id,
_version
, _routing
, _parent
, and _now
(当前的timestamp)。
给文档增加一个新的字段:
POST test/type1/1/_update
{
"script" : "ctx._source.new_field = \"value_of_new_field\""
}
删除一个字段:
POST test/type1/1/_update
{
"script" : "ctx._source.remove(\"new_field\")"
}
脚本中还可以执行逻辑语句,以下脚本将会删除tag字段中含有green
的文档。
POST test/type1/1/_update
{
"script" : {
"inline": "if (ctx._source.tags.contains(params.tag)) { ctx.op = \"delete\" } else { ctx.op = \"none\" }",
"lang": "painless",
"params" : {
"tag" : "green"
}
}
}
也可以通过传递文档的一部分,新增的内容会合并到原始文档中,例如增加字段:
POST test/type1/1/_update
{
"doc" : {
"name" : "new_name"
}
}
如果同时又doc
和script
,doc
会被忽略。
如果新传入的文档原始文档中已经存在,再次更新会被忽略,也就是覆盖一模一样的字段内容会被忽略。把detect_noop
设为false,即使文档内容一样,也会执行。
POST test/type1/1/_update
{
"doc" : {
"name" : "new_name"
},
"detect_noop": false
}
二、多文档API
2.1 Multi Get API
通过ID一次获取多个文档的方式:
GET _mget
{
"docs" : [
{
"_index" : "test",
"_type" : "type",
"_id" : "1"
},
{
"_index" : "test",
"_type" : "type",
"_id" : "2"
}
]
}
如果索引相同:
GET test/_mget
{
"docs" : [
{
"_type" : "type",
"_id" : "1"
},
{
"_type" : "type",
"_id" : "2"
}
]
}
如果type相同:
GET test/type/_mget
{
"docs" : [
{
"_id" : "1"
},
{
"_id" : "2"
}
]
}
简写:
GET test/type/_mget
{
"ids" : ["1", "2"]
}
缺省type会返回索引下所有的type下的所有符合id的文档:
GET test/_mget
{
"ids" : ["1", "1"]
}
如果文档不存在,使用使用upserts插入新文档:
POST test/type1/1/_update
{
"script" : {
"inline": "ctx._source.counter += params.count",
"lang": "painless",
"params" : {
"count" : 4
}
},
"upsert" : {
"counter" : 1
}
}
2.2 Bulk API
Bulk API可以批量插入:
POST _bulk
{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_type" : "type1", "_id" : "2" } }
{ "create" : { "_index" : "test", "_type" : "type1", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_type" : "type1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }
2.3 Delete By Query API
通过查询条件删除:
POST twitter/_delete_by_query
{
"query": {
"match": {
"message": "some message"
}
}
}
2.4 Update By Query API
通过查询更新文档:
POST twitter/_update_by_query
{
"script": {
"inline": "ctx._source.likes++",
"lang": "painless"
},
"query": {
"term": {
"user": "kimchy"
}
}
}
2.5 Reindex API
reindex api用于从一个索引拷贝文档到另外一个索引,注意,mapping、setting中的分片数和副本数都不会被拷贝。
把索引twitter中的文档拷贝到new_twitter:
POST _reindex
{
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter"
}
}