# How to get max value with document data

**URL:** <https://discuss.elastic.co/t/how-to-get-max-value-with-document-data/39614>\
**Category:** Elasticsearch\
**Created:** [January 20, 2016, 3:44am UTC](https://discuss.elastic.co/t/how-to-get-max-value-with-document-data/39614 "2016-01-20T03:44:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gpong](https://avatars.discourse-cdn.com/v4/letter/g/d6d6ee/32.png) [@gpong](https://discuss.elastic.co/u/gpong)\
**Post date:** [January 20, 2016, 3:44am UTC](https://discuss.elastic.co/t/how-to-get-max-value-with-document-data/39614/1 "2016-01-20T03:44:06Z")

</div>

Hi Guys,

I have three document  
{  
"plant": "F130",  
"partno": "aa.123.abcds",  
"date": "20150903",  
"curr": "NTD",  
"cost": "1000",  
}

{  
"plant": "F130",  
"partno": "aa.123.abcds",  
"date": "20151203",  
"curr": "NTD",  
"cost": "1200",  
}

{  
"plant": "F130",  
"partno": "aa.123.abcds",  
"date": "20151215",  
"curr": "NTD",  
"cost": "1500",  
}

and now I need to get document with max date value. so I use this query to get result

```
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "plant": "f130"
          }
        },
        {
          "term": {
            "partno": "aa.123.abcds"
          }
        },
        {
          "term": {
            "curr": "ntd"
          }
        }
      ]
    }
  },
  "aggregations": {
    "by_date": {
      "max": {
        "field": "date"
      }
    }
  }
}

```

But aggregation the result is

"aggregations": {  
"by\_date": {  
"value": 1450137600000,  
"value\_as\_string": "20151215"  
}

what I expected is this document

{  
"plant": "F130",  
"partno": "aa.123.abcds",  
"date": "20151215",  
"curr": "NTD",  
"cost": "1500",  
}

Can anyone help me how to do it?

Thanks

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 20, 2016, 7:03am UTC](https://discuss.elastic.co/t/how-to-get-max-value-with-document-data/39614/2 "2016-01-20T07:03:46Z")

</div>

It looks good but may be your mapping is incorrect?

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [January 20, 2016, 8:14am UTC](https://discuss.elastic.co/t/how-to-get-max-value-with-document-data/39614/3 "2016-01-20T08:14:49Z")

</div>

I see no need to use aggregations. Simply sort the results on the date  
value (descending) and retrieve only one result by setting the size to 1.

{  
"query": {  
"bool": {  
"must": [  
{  
"term": {  
"plant": "f130"  
}  
},  
{  
"term": {  
"partno": "aa.123.abcds"  
}  
},  
{  
"term": {  
"curr": "ntd"  
}  
}  
]  
}  
},  
"sort": [  
{  
"date": {  
"order": "desc"  
}  
}  
],  
"size": 1  
}

---

<div class="post-metadata">

**Author:** ![gpong](https://avatars.discourse-cdn.com/v4/letter/g/d6d6ee/32.png) [@gpong](https://discuss.elastic.co/u/gpong)\
**Post date:** [January 20, 2016, 8:39am UTC](https://discuss.elastic.co/t/how-to-get-max-value-with-document-data/39614/4 "2016-01-20T08:39:27Z")

</div>

this works perfectly  
Thanks Ivan

---

<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:23pm UTC](https://discuss.elastic.co/t/how-to-get-max-value-with-document-data/39614/5 "2017-07-05T23:23:07Z")

</div>


