Elasticsearch Update document Data _elasticsearch

Source: Internet
Author: User
Text mode batch update multiple fields

The simplest use of an update request is to add new data. The new data is merged into the existing data, and if the same field exists, it is replaced by the new data. For example, we can add tags and views fields for our blog:

Post/website/blog/1/_update
{
  "doc": {"
      tags": ["Testing"],
      "views": 0
   }
}

If the request succeeds, we receive an index-like return content

Using scripts to update the default scripting language is called Mvel, but Elasticsearch also supports JavaScript, Groovy, and Python. Mvel is a simple and efficient Java basic Dynamic scripting language with syntax similar to JavaScript.

The scripting language can be used in the update API to modify content in the _source, which is called Ctx._source in the script. For example, we can use a script to increase the number of views in a blog:

Post/website/blog/1/_update
{
   "script": "Ctx._source.views+=1"
}

We can also use the script to add a new tag to the tags array. In this example, we declare the new tag as a variable instead of writing him to death in the script. This allows Elasticsearch to reuse the script for tag additions without having to rewrite the script again:

Post/website/blog/1/_update
{
   "script": "Ctx._source.tags+=new_tag",
   "params": {
      "New_tag": " Search "
   }
}

To get the document, the latter two have changed:

{"
   _index": "    website", "
   _type":     "blog",
   "_id":       "1",
   "_version":  5,
   "found" :     True,
   "_source": {
      "title":  "my A-blog entry",
      "text":   "starting to get" hang of This ... ""
      tags ":  [" Testing "," search "], <1>
      " views ":  1 <2>
   }
}
tags in the array appears search. The Views field is incremented.

We can even use Ctx.op to choose whether or not to delete a document based on content:

Post/website/blog/1/_update
{
   "script": "Ctx.op = Ctx._source.views = = count?" Delete ': ' None ', '
    params ': {
        ' count ': 1
    }
}
Update a document that may not exist

Imagine that we might need to store a page counter in Elasticsearch. Each time the user visits this page, we add the current page counter. But if this is a new page, we can't make sure that the counter already exists. If we try to update a document that does not exist, the update operation will fail.

To prevent this from happening, we can use the Upsert parameter to set the document to be created when it does not exist:

Post/website/pageviews/1/_update
{
   "script": "Ctx._source.views+=1",
  "Upsert": {
       "views": 1
   }
}

When this request is first run, the content of the Upsert is indexed to the new document, which initializes the views field to 1. When requested later, the document already exists, so the script update is executed and the views counter is incremented.

To avoid losing data, the update API obtains the _version in the current document in the fetch step and passes it to the index request in the indexing step. If other processes modify the document between these two departments, then the _version will be different, and the update will fail.

For many local updates, it is not really important that the document is changed. For example, two processes will have to increase the number of page browsing counters, who first after the fact is not important--in the event of a conflict, only need to come back.

You can set the number of times to automatically complete this request by setting the Retry_on_conflict parameter, which defaults to 0.

Post/website/pageviews/1/_update?retry_on_conflict=5 <1>
{
   "script": "Ctx._source.views+=1",
   " Upsert ': {views
       ': 0
   }
}
Try again 5 times before failure This parameter is very useful for requests that are similar to adding counters in unrelated order


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.