Update/Delete by query & Batch processing

이상민·2021년 4월 28일
0
post-thumbnail

1. Update and Delete by query

update multiple docs with single query

--> similar to UPDATE WHERE in SQL

1-1. Update by query

  • use update by query API
POST /{index name}/_update_by_query
{
    "script" : {
        "source" : "ctx._source.{field name}--"
    },
    "query" : {
        "match_all" : {}
    }
}

1-2. Delete by query

delete multiple docs with single query

--> similar to DELETE WHERE in SQL

  • use delete by query API
DELETE /{index name}/_delete_by_query
{
    "query" : {
        "match_all" : {}
    }
}

2. Batch processing

how to perform CRUD on many docs with a single query

  • use Bulk API

2-1. Index, Create

POST /_bulk
<!-- action and meta data -->
{ "index" : { "_index" : [index name], "_id" : [doc id] }}
<!-- data -->
{ [field name] : [value], ... }
{ "create" : {"_index" : [index name], "_id" : [doc id] }}
{ [field name] : [value], ... }
  • index vs create
    • index : idempotent
    • create : fails if doc exists

2-2. Update, Delete

POST /_bulk
{ "update" : { "_index" : [index name], "_id" : [doc id] }}
{ "doc" : { [field name] : [value] }}
{ "delete" : {"_index" : [index name], "_id" : [doc id] }}
  • bath processing can be used on specific index using endpoint
POST /[index name]/_bulk
{"delete" : { "_id" : [doc id] }}

2-3. Things for batch processing

  • Content-Type header should be application/x-ndjson

  • Each line must end with newline character

  • failed action will not affect other actions

2-4 When to use Bulk API?

  • When performing lots of write op
  • network round trips are avoided
profile
편하게 읽기 좋은 단위의 포스트를 추구하는 개발자입니다

0개의 댓글