# Elasticsearch update by query - update ONLY the last document

**URL:** <https://discuss.elastic.co/t/elasticsearch-update-by-query-update-only-the-last-document/278506>\
**Category:** Elasticsearch\
**Tags:** eql-elastic-query-language\
**Created:** [July 13, 2021, 8:45am UTC](https://discuss.elastic.co/t/elasticsearch-update-by-query-update-only-the-last-document/278506 "2021-07-13T08:45:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Savas12.01](https://avatars.discourse-cdn.com/v4/letter/s/439d5e/32.png) [@Savas12.01](https://discuss.elastic.co/u/Savas12.01)\
**Post date:** [July 13, 2021, 8:45am UTC](https://discuss.elastic.co/t/elasticsearch-update-by-query-update-only-the-last-document/278506/1 "2021-07-13T08:45:35Z")

</div>

Hello community,

I am new to the elasticsearch and I am trying to get the last document from my index and update its list of object.

I tried several queries e.g:

```auto
POST localhost:9200/test-index/_update_by_query

{
 "size":1,
 "sort": [{"timestamp":"asc"}],
  "script": {
      "inline": "ctx._source.addresses.add(params.address)",
        "params" : {
           "address" :{
                  "street": "Yemen Road",
                  "number": 15,
                  "county": "Yemen"
              }
        }
    }
}

```

But this updates all my documents. I guess that the script is applied before the size. I am stuck at this point and I would really like to do this in one API call.  
What is the fastest way to do this?

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [July 13, 2021, 10:19am UTC](https://discuss.elastic.co/t/elasticsearch-update-by-query-update-only-the-last-document/278506/2 "2021-07-13T10:19:51Z")

</div>

Update by query does not support a size. You would need to run a query first using size and sorting, and then use the update API on that single document.

---

<div class="post-metadata">

**Author:** ![Savas12.01](https://avatars.discourse-cdn.com/v4/letter/s/439d5e/32.png) [@Savas12.01](https://discuss.elastic.co/u/Savas12.01)\
**Post date:** [July 13, 2021, 11:08am UTC](https://discuss.elastic.co/t/elasticsearch-update-by-query-update-only-the-last-document/278506/3 "2021-07-13T11:08:06Z")

</div>

@spinscale thank you for the answer. I knew how to do this with two api calls, was wondering is it possible to do it only with one. Thanks for confirming I need to use 2 calls.

---

<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:** [August 10, 2021, 11:08am UTC](https://discuss.elastic.co/t/elasticsearch-update-by-query-update-only-the-last-document/278506/4 "2021-08-10T11:08:52Z")

</div>

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