ES 批处理命令

操作流程流程

以批量更新数据为例

1、 数据准备

  • data.json
    1
    2
    {"update": {"_index": "cdr_20191108", "_type": "cdr", "_id": "1100036-f773d51bdac2461eb6b4e66579b33e6c-1573179699.5"}}
    {"script": {"source": "def hitDeathItem = false;ctx._source.next_cdr_delayed = params.nextCdrDelayed;if (params.nextCdrDelayed == 1) {ctx._source.auto_items.add(params.autoItem);for (result in ctx._source.qc_result_score) {if (result.hit !== null && result.hit == 1) {hitDeathItem = true;}if (result.item == params.autoItem.item) {result.score = result.score + params.autoItem.score;}}ctx._source.qc_score = hitDeathItem ? 0 : ctx._source.qc_score + params.autoItem.score;}", "params": {"nextCdrDelayed": 1, "autoItem": {"item": "过程语", "message": "评语:话单延迟追加", "record_type": 0, "revise_type": 3, "score": -1, "type": 8, "weight": 27}, "status": 32}}}

2、命令执行

  • ssh
    1
    curl 'localhost:9200/_bulk?pretty' --data-binary @data.json -H 'Content-Type:application/json' > result.json

批量操作分类:

  1. 批量创建

    • 数据格式:
      1
      2
      {"create":{"_index":"tt","_type":"ttt","_id":"100"}}
      {"name":"lisi"}
  2. 批量删除

    • 数据格式:
      1
      {"delete":{"_index":"lib","_type":"books","_id":"4"}}
  3. 批量更新

    • 数据格式:
      1
      2
      3
      4
      {"update":{"_index":"lib","_type":"books","_id":"4"}} //更新动作不能缺失_id,文档不存在更新将会失败
      {"doc":{"price":58}}
      {"update": {"_index": "cdr_20191108", "_type": "cdr", "_id": "1100036-f773d51bdac2461eb6b4e66579b33e6c-1573179699.5"}}
      {"script": {"source": "def hitDeathItem = false;ctx._source.next_cdr_delayed = params.nextCdrDelayed;if (params.nextCdrDelayed == 1) {ctx._source.auto_items.add(params.autoItem);for (result in ctx._source.qc_result_score) {if (result.hit !== null && result.hit == 1) {hitDeathItem = true;}if (result.item == params.autoItem.item) {result.score = result.score + params.autoItem.score;}}ctx._source.qc_score = hitDeathItem ? 0 : ctx._source.qc_score + params.autoItem.score;}", "params": {"nextCdrDelayed": 1, "autoItem": {"item": "过程语", "message": "评语:话单延迟追加", "record_type": 0, "revise_type": 3, "score": -1, "type": 8, "weight": 27}, "status": 32}}}
  4. 混合操作

    • 示例:
      1
      2
      3
      4
      5
      6
      7
      {"delete":{"_index":"lib","_type":"books","_id":"4"}} //删除的批量操作不需要请求体
      {"create":{"_index":"tt","_type":"ttt","_id":"100"}}
      {"name":"lisi"} //请求体
      {"index":{"_index":"tt","_type":"ttt"}} //没有指定_id,elasticsearch将会自动生成_id
      {"name":"zhaosi"} //请求体
      {"update":{"_index":"lib","_type":"books","_id":"4"}} //更新动作不能缺失_id,文档不存在更新将会失败
      {"doc":{"price":58}} //请求体