# Max ES Query

**URL:** <https://discuss.elastic.co/t/max-es-query/229785>\
**Category:** Elasticsearch\
**Created:** [April 25, 2020, 7:33pm UTC](https://discuss.elastic.co/t/max-es-query/229785 "2020-04-25T19:33:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stefano\_Bossi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stefano_bossi/32/41743_2.png) [@Stefano\_Bossi](https://discuss.elastic.co/u/Stefano_Bossi)\
**Post date:** [April 25, 2020, 7:33pm UTC](https://discuss.elastic.co/t/max-es-query/229785/1 "2020-04-25T19:33:27Z")

</div>

Dear Forum,

I need some help to design a query I have some difficulties.  
I need the maximum value of two fields (i.e. numerical ones). I have figured out by myself the max of one value with this query:

```auto
    {
      "size": "0",
      "aggs": {
        "max_myField": {
          "max": {
            "field": "myFieldOne"
          }
        }
      }
    }

```

but how to have the max value among myFieldOne and myFiledTwo ?

Regards,  
S.

---

<div class="post-metadata">

**Author:** ![Luca\_Belluccini](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luca_belluccini/32/33239_2.png) [@Luca\_Belluccini](https://discuss.elastic.co/u/Luca_Belluccini)\
**Post date:** [April 26, 2020, 2:16am UTC](https://discuss.elastic.co/t/max-es-query/229785/2 "2020-04-26T02:16:13Z")

</div>

If both fields always exist:

```auto
POST /_search
{
  "size": 0,
  "aggs": {
    "max_price": {
      "max": {
        "script": {
          "source": "Math.max(doc.myFieldOne.value, doc.myFieldTwo.value)"
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Stefano\_Bossi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stefano_bossi/32/41743_2.png) [@Stefano\_Bossi](https://discuss.elastic.co/u/Stefano_Bossi)\
**Post date:** [April 26, 2020, 10:22pm UTC](https://discuss.elastic.co/t/max-es-query/229785/3 "2020-04-26T22:22:45Z")

</div>

Thanks!

S.

---

<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:** [May 24, 2020, 10:22pm UTC](https://discuss.elastic.co/t/max-es-query/229785/4 "2020-05-24T22:22:52Z")

</div>

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