新增文档
PUT /<index>
{...}
PUT /<index>/_doc/<_id>
{...}
更新文档
- 更新某个文档
POST /<index>/_update/<_id>
{...}
POST /<index>/_update/<_id>
{
"doc": {
"name": "new_name",
"age": "30"
}
}
- 利用Painless更新文档某个field
POST /<index>/_doc/<_id>/_update
{
"script" : "ctx._source.thumb='http://img.test.com/2020/04/08/test.jpg'"
}
3.批量更新
POST /<index>/_update_by_query
{
"script": {
"source": "ctx._source.count++",
"lang": "painless"
},
"query": {
"term": {
"user.id": "kimchy"
}
}}
#####
{
"script": "if (ctx._source.crawl_time != null){ctx._source.statistics_day=LocalDate.parse(ctx._source.crawl_time.substring(0,10)).minusDays(1).toString()} ",
"query": {
"bool": {
"must_not": [
{
"exists": {
"field": "statistics_day"
}
}
],
"must": [
{
"range": {
"crawl_time": {
"gte": "2021-01-01 00:00:00"
}
}
}
]
}
}
}
可以通过ctx.op指定操作类型,其值包含noop、delete、update。
4.删除某个field
POST test/_update/1
{
"script" : "ctx._source.remove('new_field')"
}
- 删除文档
// 删除整个索引
DELETE /<index>
// 删除某个文档
DELETE /<index>/_doc/<_id>
{...}
// 删除查询结果
POST /<index>/_delete_by_query
{...}
- UPSERT文档
如果key不存在就使用”upsert”语句块执行insert操作,如果存在就使用”script”语句块执行update操作。
POST test/_update/1
{
"script": {
"source": "ctx._source.counter += params.count",
"lang": "painless",
"params": {
"count": 4
}
},
"upsert": {
"counter": 1
}
}
- 完全使用脚本做upsert
POST sessions/_update/dh3sgudg8gsrgl
{
"scripted_upsert": true,
"script": {
"id": "my_web_session_summariser",
"params": {
"pageViewEvent": {
"url": "foo.com/bar",
"response": 404,
"time": "2014-01-01 12:32"
}
}
},
"upsert": {}
}
使用doc作upsert
POST test/_update/1
{
"doc": {
"name": "new_name"
},
"doc_as_upsert": true
}