# Max aggregation on .keyword field with both string and number values

**URL:** <https://discuss.elastic.co/t/max-aggregation-on-keyword-field-with-both-string-and-number-values/281168>\
**Category:** Elasticsearch\
**Created:** [August 12, 2021, 8:16am UTC](https://discuss.elastic.co/t/max-aggregation-on-keyword-field-with-both-string-and-number-values/281168 "2021-08-12T08:16:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniele\_Renda](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/daniele_renda/32/39939_2.png) [@Daniele\_Renda](https://discuss.elastic.co/u/Daniele_Renda)\
**Post date:** [August 12, 2021, 8:16am UTC](https://discuss.elastic.co/t/max-aggregation-on-keyword-field-with-both-string-and-number-values/281168/1 "2021-08-12T08:16:27Z")

</div>

Hi,  
I've a quite common situation: I've an index for products with a barcode field:

```auto
 "barcode": {
               "type": "text",
                "fields": {
                  "ngram": {
                    "type": "text",
                    "analyzer": "ngram",
                    "search_analyzer": "standard"
                  },
                  "keyword": {
                    "type": "keyword"
                  }
                }
              },

```

I need to get the MAX value for the barcode when it's a number: in fact in my index I could have either string values (ie. "MYBARCODE001245") or numeric values (i.e. "1234567890123"). I need to get the MAX value of numeric values.  
In that even possible? I guess I need to change a little bit my schema index because I see the query can't run on my field like it is.

Thanks for your hints.

---

<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:** [September 9, 2021, 8:17am UTC](https://discuss.elastic.co/t/max-aggregation-on-keyword-field-with-both-string-and-number-values/281168/2 "2021-09-09T08:17:09Z")

</div>

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