gpt4 book ai didi

elasticsearch - Elasticsearch DSL:聚合

转载 作者:行者123 更新时间:2023-12-03 02:02:47 24 4
gpt4 key购买 nike

下面显示的是我的数据类型(表的类型)的结构。

 Aircraft | Duration 
A320 | 0.95
A320 | 0.55
A321 | 16.50
A321 | 3.9

在此数据中,我想对持续时间执行ceil(),然后执行groupBy操作以获取以下输出:
 Aircraft | Duration | Count
A320 | 1 | 2
A321 | 17 | 1
A321 | 4 | 1

最佳答案

我已经基于以下映射类型(将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)"
}
}
}
}
}
}'

该查询的输出如下所示:

注意:确保通过添加启用 elasticsearch.yml文件中的脚本
script.disable_dynamic: false

关于elasticsearch - Elasticsearch DSL:聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30340830/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com