我有一个索引,其类型可以简化为:
{
'date': DATE_STRING,
'owner': INT,
'color: 'red' | 'purple' | 'blue'
}
,并希望查询以显示以下数据,其中所有者的值等于他们拥有的“蓝色”项目的总数减去所请求的“红色”项目的数量时间(不要问为什么):
答案 0 :(得分:0)
设置索引:
PUT colorful
{
"mappings": {
"properties": {
"date": {
"type": "date"
},
"owner": {
"type": "integer"
},
"color": {
"type": "keyword"
}
}
}
}
插入一些文档
POST colorful/_doc
{"date":"2020-05-28T19:56:12.237Z","owner":131351351,"color":"red"}
POST colorful/_doc
{"date":"2020-04-28T19:58:02.110Z","owner":35135125,"color":"purple"}
POST colorful/_doc
{"date":"2020-05-15T19:58:15.966Z","owner":997654341,"color":"blue"}
POST colorful/_doc
{"date":"2020-05-21T19:58:35.766Z","owner":366449,"color":"red"}
按日期范围和总计过滤。最小值,最大值,平均(=平均值)可以使用stats
来计算,对于median
,有percentiles[50]
。不确定a particular owner's value
是什么意思,但是可以使用top_hits
获取实际的范围过滤文档,还可以为特定文档添加过滤器。
GET colorful/_search
{
"size": 0,
"query": {
"range": {
"date": {
"gte": "now-3M",
"lte": "now-1h"
}
}
},
"aggs": {
"1)general_stats": {
"stats": {
"field": "owner"
}
},
"2)median": {
"percentiles": {
"field": "owner",
"percents": [
50
]
}
},
"3)top_hits": {
"top_hits": {
"size": 10
}
}
}
}