Es: principle of partial update, based on groovy use, built-in optimistic lock concurrency control

Posted by jbloom on Wed, 25 Dec 2019 17:35:01 +0100

1. Basic syntax of partial update

POST /index/type/id/_update
{
    "doc" : {
        "A few to be revised field That is, no full amount of data is needed"
    }
}

Only a few modified field s can be passed at a time, without sending the full amount of document data to the past

2. Implementation principle and advantages of partial update

3. Example:

POST /test_index/test_type/10/_update
{  
  "doc": {
    "test_field2":"update test2"
    }
}

4. How to execute partial update based on groovy script

PUT /test_index/test_type/11
{
  "num": 0,
  "tags": []
}

(1) Built in script

POST /test_index/test_type/11/_update
{
   "script" : "ctx._source.num+=1"
}
{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "11",
  "_version": 2,
  "found": true,
  "_source": {
    "num": 1,
    "tags": []
  }
}

(2) External script
ctx._source.tags+=new_tag

POST /test_index/test_type/11/_update
{
  "script": {
    "lang": "groovy", 
    "file": "test-add-tags",
    "params": {
      "new_tag": "tag1"
    }
  }
}

(3) Deleting documents with scripts
ctx.op = ctx._source.num == count ? 'delete' : 'none'

POST /test_index/test_type/11/_update
{
  "script": {
    "lang": "groovy",
    "file": "test-delete-document",
    "params": {
      "count": 1
    }
  }
}

(4) upsert operation

POST /test_index/test_type/11/_update
{
  "doc": {
    "num": 1
  }
}
{
  "error": {
    "root_cause": [
      {
        "type": "document_missing_exception",
        "reason": "[test_type][11]: document missing",
        "index_uuid": "6m0G7yx7R1KECWWGnfH1sw",
        "shard": "4",
        "index": "test_index"
      }
    ],
    "type": "document_missing_exception",
    "reason": "[test_type][11]: document missing",
    "index_uuid": "6m0G7yx7R1KECWWGnfH1sw",
    "shard": "4",
    "index": "test_index"
  },
  "status": 404
}

If the specified document does not exist, perform the initialization operation in upsert; if the specified document exists, perform the partial update operation specified by doc or script

POST /test_index/test_type/11/_update
{
   "script" : "ctx._source.num+=1",
   "upsert": {
       "num": 0,
       "tags": []
   }
}

5. Concurrency control principle of partial update optimistic lock

retry strategy

POST /index/type/id/_update?retry_on_conflict=5&version=6