# How to execute aggregation?

**URL:** <https://discuss.elastic.co/t/how-to-execute-aggregation/96404>\
**Category:** Elasticsearch\
**Created:** [August 9, 2017, 9:01am UTC](https://discuss.elastic.co/t/how-to-execute-aggregation/96404 "2017-08-09T09:01:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yevsh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yevsh/32/20938_2.png) [@yevsh](https://discuss.elastic.co/u/yevsh)\
**Post date:** [August 9, 2017, 9:01am UTC](https://discuss.elastic.co/t/how-to-execute-aggregation/96404/1 "2017-08-09T09:01:41Z")

</div>

Not able to run aggregation query.  
This is what I am doing:

1)Mapping creation:

```
curl -XPUT 'localhost:9200/items?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "item": {
      "properties": {
        "number": { 
          "type": "text",
          "fields": {
            "keyword": { 
              "type": "keyword"
            }
          }
        }
      }
    }
  }
}
'

```

## 2)Inserting index:

`curl -XPUT 'http://localhost:9200/items/item/i1' -d '{ "number" : 1001}'`

3)Aggregation query:

```
curl -XPOST 'localhost:9200/items/_search?size=0&pretty' -H 'Content-Type: application/json' -d'
{
    "aggs" : {
        "number" : { "max" : { "field" : "number" } }
    }
}
'

```

Error:  
Fielddata is disabled on text fields by default. Set fielddata=true on [number] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [August 9, 2017, 9:34am UTC](https://discuss.elastic.co/t/how-to-execute-aggregation/96404/2 "2017-08-09T09:34:44Z")

</div>

In your mapping the field called `number` in your source JSON will be stored in 2 indexed fields. One is called `number` and is (perhaps oddly) treated as a "text" type field meaning its contents will be treated as a string and chopped into individual words in the index (where words are expected to be separated by punctuation like whitespace). The original string will also be indexed as-is in a field called `number.keyword` which is the field name you should use in your aggregation.

However- given your field is called `number` I expect you'd be better served mapping this as type `long` or some other suitable numeric type to ensure operations like `max` work correctly.

---

<div class="post-metadata">

**Author:** ![yevsh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yevsh/32/20938_2.png) [@yevsh](https://discuss.elastic.co/u/yevsh)\
**Post date:** [August 9, 2017, 10:24am UTC](https://discuss.elastic.co/t/how-to-execute-aggregation/96404/3 "2017-08-09T10:24:33Z")

</div>

Thanks, changing to "type": "long", solved the issue, and now I am getting:

```
 "aggregations" : {
    "number" : {
      "value" : 1001.0
    }

```

Is there a built-in option to return the value without floating point? e.g. 1001

---

<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 6, 2017, 10:24am UTC](https://discuss.elastic.co/t/how-to-execute-aggregation/96404/4 "2017-09-06T10:24:57Z")

</div>

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