我自己基於對以下映射類型(編輯Duration
作爲字符串):
curl -XPUT localhost:9200/tests -d '
{
"mappings": {
"test1": {
"properties": {
"Aircraft": {
"type": "string"
},
"Duration": {
"type": "string"
}
}
}
}
}'
並創建了四個與上面的數據表匹配的文檔。
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"}
'
聚集查詢將返回你所期望應該是這樣的結果:
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)"
}
}
}
}
}
}'
該查詢的輸出是這樣的:
![enter image description here](https://i.stack.imgur.com/zcXol.png)
注意:請確保在您的elasticsearch.yml
文件中啓用腳本,方法是加入
script.disable_dynamic: false
來源
2015-05-20 05:48:55
Val
感謝您提供解決方案。我的'持續時間'實際上是字符串。我試過「script」:「Math.ceil(Double.parseDouble(doc ['Duration'] .value))」但沒有奏效。有什麼建議麼。 – Mohitt
我編輯了我的答案並重新測試了OK。 – Val
非常感謝。只是一個側面的問題。我有一個過濾條款來過濾一些特定的飛機。示例 - {\t 「條款」:{\t 「aircraft」:[「a320」,「b737」] }。在上面的查詢中應該插入哪裏? – Mohitt