问题
I'm trying to get a max and min value of a date.
From the documentation I don't see a max option in composite: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html#_value_sources
I'd like something like this:
{
"size":0,
"aggs":{
"intervals":{
"composite":{
"size":10000,
"sources":[
{
"id":{
"terms":{
"field":"id"
}
}
},
{
"minTime": {
"min": {
"script": "doc['createdAt'].value"
}
}
},
{
"maxTime": {
"max": {
"script": "doc['createdAt'].value"
}
}
}
]
}
}
}
}
Is it possible to add to this query or will I need a separate query for this?
回答1:
The composite aggregation allows you to paginate over buckets. min and max are metric aggregations that apply on each bucket (i.e. metric aggregations cannot be sources of composite aggregations) and must added as sub-aggregations to the composite aggregation, so what you need to do is the following instead:
{
"size": 0,
"aggs": {
"intervals": {
"composite": {
"size": 10000,
"sources": [
{
"id": {
"terms": {
"field": "id"
}
}
}
]
},
"aggs": {
"minTime": {
"min": {
"script": "doc['createdAt'].value"
}
},
"maxTime": {
"max": {
"script": "doc['createdAt'].value"
}
}
}
}
}
}
来源:https://stackoverflow.com/questions/64895363/elasticsearch-composite-aggregation-max-value