Reputation: 2977
Shown below is the structure of type (kind of a table) of my data.
Aircraft | Duration
A320 | 0.95
A320 | 0.55
A321 | 16.50
A321 | 3.9
In this data, I want to perform a ceil() on duration, followed by a groupBy operation to get following output:
Aircraft | Duration | Count
A320 | 1 | 2
A321 | 17 | 1
A321 | 4 | 1
Upvotes: 0
Views: 443
Reputation: 217254
I've based myself on the following mapping type (edited Duration
as string):
curl -XPUT localhost:9200/tests -d '
{
"mappings": {
"test1": {
"properties": {
"Aircraft": {
"type": "string"
},
"Duration": {
"type": "string"
}
}
}
}
}'
And created four documents that match your data table above.
curl -XPOST localhost:9200/tests/_bulk -d '
{"index": {"_type": "test1", "_id": 1}}
{"Aircraft": "A320", "Duration": "0.95"}
{"index": {"_type": "test1", "_id": 2}}
{"Aircraft": "A320", "Duration": "0.55"}
{"index": {"_type": "test1", "_id": 3}}
{"Aircraft": "A321", "Duration": "16.50"}
{"index": {"_type": "test1", "_id": 4}}
{"Aircraft": "A321", "Duration": "3.9"}
'
The aggregation query that will return the results you expect would look like this:
curl -XPOST localhost:9200/tests/_search -d '
{
"size": 0,
"query": {
"filtered": {
"filter": {
"terms": {
"Aircraft": [
"a320",
"b737"
]
}
}
}
},
"aggs": {
"aircrafts": {
"terms": {
"field": "Aircraft"
},
"aggs": {
"duration": {
"terms": {
"script": "Math.ceil(doc['Duration'].value as double)"
}
}
}
}
}
}'
The output of that query looks like this:
Note: Make sure to enable scripting in your elasticsearch.yml
file by adding
script.disable_dynamic: false
Upvotes: 1