# Getting the last nested or most recent nested element

**URL:** <https://discuss.elastic.co/t/getting-the-last-nested-or-most-recent-nested-element/28334>\
**Category:** Elasticsearch\
**Created:** [August 31, 2015, 7:53am UTC](https://discuss.elastic.co/t/getting-the-last-nested-or-most-recent-nested-element/28334 "2015-08-31T07:53:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pentium10](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pentium10/32/1181_2.png) [@pentium10](https://discuss.elastic.co/u/pentium10)\
**Post date:** [August 31, 2015, 7:53am UTC](https://discuss.elastic.co/t/getting-the-last-nested-or-most-recent-nested-element/28334/1 "2015-08-31T07:53:27Z")

</div>

We have this mapping:

```
{
    "product_achievement": {
        "type": "nested",
        "properties": {
            "id": {
                "type": "long"
            },
            "last_purchase": {
                "type": "long"
            },
            "products": {
                "type": "long"
            }
        }
    }
}

```

As you see this is nested, and the `last_purchase` field is a `unixtimestamp` value. We would like to query from all nested elements the most recent entry defined by the `last_purchase` field, AND see if in the last entry there is some product id is in products.

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [September 1, 2015, 4:21pm UTC](https://discuss.elastic.co/t/getting-the-last-nested-or-most-recent-nested-element/28334/2 "2015-09-01T16:21:22Z")

</div>

I think it might be easier and faster to just index the last\_products field one more time as non-nested field of the root document. Then all you have to do is to perform a terms filter search on this field.

---

<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:** [July 5, 2017, 11:52pm UTC](https://discuss.elastic.co/t/getting-the-last-nested-or-most-recent-nested-element/28334/3 "2017-07-05T23:52:51Z")

</div>


