Update document field by value of another document

I have problem with Elasticsearch update. It's trivial in sql engines but I can't do it in elasticsearch.

Let's say I have fellowing documents in ES:

PUT logs/_doc/1
{
 "commonId" : "111111",
 "user" : "abc",
 "phase" : "start"
}
PUT logs/_doc/2
{
 "commonId" : "111111",
 "device" : "kkk",
}
PUT logs/_doc/3
{
 "commonId" : "222222",
 "user" : "cde",
 "phase" : "start"
}
PUT logs/_doc/4
{
 "commonId" : "222222",
 "device" : "jjj",
}

Is there any way to create update_by_query to add any field to document based on field from another document (in my examlple with same commonId)? In example above I want to add "user" : "abc" field to _doc/2 and "user" : "cde" to _doc/4 ("commonId" is common for 1,2 and 3,4).
Generally I would work like:

  • insert field "user" to all documents where value of "user" should equals "user.value" from document where "phase" = "start" and "commonId" is the same.

I'm asking because I didn't find any example where value of field is a result of another query. Something like nested query in sql-like databases:

UPDATE logs SET user = t.user  
FROM  
(  
    SELECT user, commonId  
    FROM logs  
    WHERE phase = "start"
) t  
WHERE logs.commonId = t.commonId

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