Update by query for each field with value by sql query


following task:

  • get each document one by one from elasticserach server
  • add field to document with value stored in a database (MariaDB)

The _id of the document is the reference to its record in the database:

I think the update_by_query API is the right one.

Fantasy code:

  "script": {
    "source": "ctx._source.count++",
    "field.any_ref": // HowTo: my.query.getValueById(ctx._id)

But there is no HowTo add the value by query using the document._id. Is that posible?

In case, that is not posible, how to realize this task?

It does not sound like update by query is the right tool. You probably need to write an external script that does this.

Any other or maybe extended suggestion?

At the moment i can add dynamically a value by a method, but i can't pass a specific value of the current document as parameter:

// PHP Script
    "script": {
        "inline": "ctx._source.myField=' 
          . $this->getRef( 'ctx._source.id' /*how to pass the parameter? */) . '",
        "lang": "painless"
private function getRef($id) 
    var_dump($id); // => return 'ctx._source.id' instaead the field value

Come on guys... any hints?

Please be patient in waiting for responses to your question and refrain from pinging multiple times asking for a response or opening multiple topics for the same question. This is a community forum, it may take time for someone to reply to your question. For more information please refer to the Community Code of Conduct specifically the section "Be patient". Also, please refrain from pinging folks directly, this is a forum and anyone that participates might be able to assist you.

If you are in need of a service with an SLA that covers response times for questions then you may want to consider talking to us about a subscription.

It's fine to answer on your own thread after 2 or 3 days (not including weekends) if you don't have an answer.

Ok, finally i found a solution:

$client = ClientBuilder::create()->build();
$params = [
    'scroll' => '30s',  
    'size'   => 1,      
    'index'  => 'my_index',
    'body'   => [
        'query' => [
            'match_all' => new \stdClass()

$response = $client->search($params);

while (isset($response['hits']['hits']) && count($response['hits']['hits']) > 0) {

    // **
    // here i use the update-Api to add a new field with the value from the database
    // **

    $scroll_id = $response['_scroll_id'];

    $response = $client->scroll([
        'body' => [
            'scroll_id' => $scroll_id,
            'scroll'    => '30s' 

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.