ElasticSearch索引数据更删改(DML)

新增文档

PUT /<index>
{...}

PUT /<index>/_doc/<_id>
{...}

更新文档

  1. 更新某个文档
POST /<index>/_update/<_id>
{...}

POST /<index>/_update/<_id>
{
  "doc": {
  "name": "new_name",
  "age": "30"
  }
}
  1. 利用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')"
}
  1. 删除文档
// 删除整个索引
DELETE /<index>
// 删除某个文档
DELETE /<index>/_doc/<_id>
{...}
// 删除查询结果
POST /<index>/_delete_by_query
{...}
  1. 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
  }
}
  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
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值