# Placing sorting on a document based on a particular nested document

**URL:** <https://discuss.elastic.co/t/placing-sorting-on-a-document-based-on-a-particular-nested-document/70338>\
**Category:** Elasticsearch\
**Created:** [January 2, 2017, 7:57am UTC](https://discuss.elastic.co/t/placing-sorting-on-a-document-based-on-a-particular-nested-document/70338 "2017-01-02T07:57:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ashish\_Goel](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@Ashish\_Goel](https://discuss.elastic.co/u/Ashish_Goel)\
**Post date:** [January 2, 2017, 7:57am UTC](https://discuss.elastic.co/t/placing-sorting-on-a-document-based-on-a-particular-nested-document/70338/1 "2017-01-02T07:57:46Z")

</div>

Hi there,

I have documents of type "entity" containing nested document "users" of the format:

```
"users": {
  "type": "nested",
     "properties": {
       "id": {
         "type": "string",
         "index": "not_analyzed"
       },
       "timestamp": {
         "type": "long"
      },
      "status": {
         "type": "string",
         "index": "not_analyzed"
      }
   }
}

```

I want to sort my "entity" document based on the descending order of "timestamp" inside "users". I also need to filter out "users" based on status, only consider COMPLETED users. So, overall, my query is "Give me the latest 5 entity documents where latest is to be decided based on timestamp in the COMPLETED users".

I tried something like:

```
{
  "sort": {
    "users.timestamp": {
      "order": "desc",
      "nested_filter": {
        "term": {
          "users.status": "COMPLETED"
        }
      }
    }
  }
}

```

This works well when there is only one entry in users inside my entity document.  
How do I ensure that while taking timestamp to decide on the sort order, I only take the user that has the latest timestamp?

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [January 2, 2017, 10:02am UTC](https://discuss.elastic.co/t/placing-sorting-on-a-document-based-on-a-particular-nested-document/70338/2 "2017-01-02T10:02:59Z")

</div>

Hi,

I think you also need to specify the `nested_path` and the sort `mode` parameter that decides which of the multi-valued "timestamps" in all the nested user documents you want to sort by. I tried this on a quick example (maybe not covering all the details you have in your docs and your main query):

```auto
"sort": {
    "users.timestamp": {
      "order": "desc",
      "mode": "max",
      "nested_path" : "users",
      "nested_filter": {
        "term": {
          "users.status": "COMPLETE"
        }
      }
    }
  }

```

---

<div class="post-metadata">

**Author:** ![Ashish\_Goel](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@Ashish\_Goel](https://discuss.elastic.co/u/Ashish_Goel)\
**Post date:** [January 2, 2017, 11:05am UTC](https://discuss.elastic.co/t/placing-sorting-on-a-document-based-on-a-particular-nested-document/70338/3 "2017-01-02T11:05:24Z")

</div>

Thanks Christoph.

That worked like a charm.

---

<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:** [January 30, 2017, 11:05am UTC](https://discuss.elastic.co/t/placing-sorting-on-a-document-based-on-a-particular-nested-document/70338/4 "2017-01-30T11:05:48Z")

</div>

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