# Can't set different values to the same field in different documents using updateByQuery

**URL:** <https://discuss.elastic.co/t/cant-set-different-values-to-the-same-field-in-different-documents-using-updatebyquery/232407>\
**Category:** Elasticsearch\
**Created:** [May 13, 2020, 9:51am UTC](https://discuss.elastic.co/t/cant-set-different-values-to-the-same-field-in-different-documents-using-updatebyquery/232407 "2020-05-13T09:51:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shahak\_Gabay](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shahak_gabay/32/68174_2.png) [@Shahak\_Gabay](https://discuss.elastic.co/u/Shahak_Gabay)\
**Post date:** [May 13, 2020, 9:51am UTC](https://discuss.elastic.co/t/cant-set-different-values-to-the-same-field-in-different-documents-using-updatebyquery/232407/1 "2020-05-13T09:51:40Z")

</div>

Hi,

Using elasticsearch-7.5.2 with NEST client I'm trying to iterate through all documents in an update\_by\_query request and set different values to the same field (called wcf) in each doc.

My code looks like this:

```auto
var task = ElasticClient.UpdateByQuery<LogLine>(u => u.Index("test")
                .Query(q => q
                    .MatchAll()
                )
                .Script(d => d
                    .Source("for (int i = 0; i < 7; i++) {ctx._source.wcf=i;}").Lang("painless")));

```

This code just updates all documents 7 times and I end up with all documents having the same value for the 'wcf' field while what I am actually looking for is to have the first document's 'wcf' field with value '0', the second with value '1' etc'.

Hope someone could assist me with this.

Thanks in advance!

---

<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:** [June 10, 2020, 9:51am UTC](https://discuss.elastic.co/t/cant-set-different-values-to-the-same-field-in-different-documents-using-updatebyquery/232407/2 "2020-06-10T09:51:43Z")

</div>

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