# UpdateByQuery script update - when does ES update document?

**URL:** <https://discuss.elastic.co/t/updatebyquery-script-update-when-does-es-update-document/220642>\
**Category:** Elasticsearch\
**Created:** [February 24, 2020, 12:08pm UTC](https://discuss.elastic.co/t/updatebyquery-script-update-when-does-es-update-document/220642 "2020-02-24T12:08:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![MattJones](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mattjones/32/55706_2.png) [@MattJones](https://discuss.elastic.co/u/MattJones)\
**Post date:** [February 24, 2020, 12:08pm UTC](https://discuss.elastic.co/t/updatebyquery-script-update-when-does-es-update-document/220642/1 "2020-02-24T12:08:38Z")

</div>

For an index with the following mapping:

```auto
{
  "item_code": {
    "type": "keyword",
    "index": true,
  },
  "prices": {
    "type": "nested",
    "dynamic": false,
    "properties": {
      "pricing_hash": {
        "type": "keyword",
        "index": true,
      },
      "price": {
        "type": "float",
        "index": true,
      },
      "best_item": {
        "type": "boolean",
        "index": true,
      }
    }
  }
}

```

Consider the following update query:

```auto
POST /items/_update_by_query
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "item_code": "ABC123"
          }
        }
      ]
    }
  },
  "script": {
    "source": "
int i;
boolean found;
for (new_price in params.new_prices) {
    found = false;
    if (ctx._source.prices == null) {
        ctx._source.prices = [];
    }
    for (i=0; i<ctx._source.prices.length; i++) {
        if (ctx._source.prices[i].pricing_hash == new_price.pricing_hash) {
            new_price.best_item = ctx._source.prices[i].best_item;
            ctx._source.prices[i] = new_price;
            found = true;
            break;
        }
    }
    if (found == false) {
        ctx._source.prices.add(new_price);
    }
}
",
    "lang": "painless",
    "params": {
      "new_prices": [
        {
          "pricing_hash": "abc12345",
          "price": 1.99,
          "best_item": true
        },
        {
          "pricing_hash": "def12345",
          "price": 2.99,
          "best_item": true
        }
      ]
    }
  }
}

```

(script indented for clarity).

If I'm indexing multiple prices into the same nested field in the same document, would ES internally save the document once at the end? Or every time a new price is changed or appended?

---

<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:** [March 23, 2020, 12:08pm UTC](https://discuss.elastic.co/t/updatebyquery-script-update-when-does-es-update-document/220642/2 "2020-03-23T12:08:44Z")

</div>

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