How to execute aggregation?


(yevsh) #1

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."


(Mark Harwood) #2

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.


(yevsh) #3

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


(system) #4

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