Amit's Garage

A garage where one can get tools to solve their problems

Updating data in Elasticsearch

November 28, 2017 by BlakDronzer

There are many ways in Elasticsearch for one to update their data.

SCENARIO 1:

Lets say, you want to update a value (like increment a certain fields value) or like set some value to specific fields (like for example update the value of flag), there is something called Partial Updates. Example for the same:


//This one indicates the option for user to set direct values in the partial update
POST /website/blog/1/_update
{
   "doc" : {
      "tags" : [ "testing" ],
      "views": 0
   }
}

OR


//This one indicates the option for user to update the value using script .. like incrementing the same and stuff
POST /website/blog/1/_update
{
   "script" : "ctx._source.views+=1"
}

The same is perfect solution if in case you looking to update for single row.

SCENARIO 2:

Lets consider you wanted to update multiple rows together following a certain criteria, then there is a feature for the same – Update By Query. Following example will share you an insight as how you can take advantage for the same.


POST blog_posts/_update_by_query
{
  "script": {
    "source": "ctx._source.likes++",
    "lang": "painless"
  },
  "query": {
    "term": {
      "user": "blakdronzer"
    }
  }
}

There is another good thing with partial updates, that is using ingest nodes. There are great possibilities with ingest nodes. Will  be covering ingest nodes later in another document but let me give in a scenario up here. Let’s say, there are a certain default values (static) you want to update together on a certain rows. Creating an ingest node can be handy in a situation like this.


PUT _ingest/pipeline/set-default-values
{
  "description" : "sets dynamic values",
  "processors" : [ {
      "set" : {
        "field": "topic_id",
        "value": 1
      }
  },
  {
      "set" : {
        "field": "topic_name",
        "value": "Defualt Topic"
      }
  },
  {
      "set" : {
        "field": "client_id",
        "value": 1
      }
  },
  {
      "set" : {
        "field": "client_name",
        "value": "Default Client"
      }
  },
 {
      "set" : {
        "field": "defaults_set",
        "value": 1
      }
  }
  ]
}

Now i can use use this ingest to set default values to multiple rows


POST articles/_update_by_query?pipeline=set-default-values
{
  "query": {
    "term": {
      "defaults_set": 0
    }
  }
}

Now this becomes very handy. There are lot other possibilities which are there with ingest nodes that can be taken in consideration / powered up with. This is just 1 simple example shared with you all.

Share :  

718   BlakDronzer    Elastic Search  
Total 0 Votes:
0
0

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Related Knowledge Base Posts -

  • Eased out solution for people using elasticsearch
  • Changing the field name in elasticsearch
  • Install and create a cluster of ES Servers
  • Search Knowledgebase