# Update document field by value of another document

**URL:** <https://discuss.elastic.co/t/update-document-field-by-value-of-another-document/126905>\
**Category:** Elasticsearch\
**Created:** [April 5, 2018, 11:24am UTC](https://discuss.elastic.co/t/update-document-field-by-value-of-another-document/126905 "2018-04-05T11:24:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arko](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@Arko](https://discuss.elastic.co/u/Arko)\
**Post date:** [April 5, 2018, 11:24am UTC](https://discuss.elastic.co/t/update-document-field-by-value-of-another-document/126905/1 "2018-04-05T11:24:35Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 3, 2018, 11:24am UTC](https://discuss.elastic.co/t/update-document-field-by-value-of-another-document/126905/2 "2018-05-03T11:24:41Z")

</div>

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