I have some containers like those
"_source": {
"testId": "test001",
"containers": [
{
"name": "containers1",
"start": "1658308670434",
"end": "1658308670687"
},
{
"name": "containers2",
"start": "1658308670688",
"end": "1658308670939"
},
{
"name": "containers3",
"start": "1658308670940",
"end": "1658308671191"
},
{
"name": "containers4",
"start": "1658308680940"
}
]
}
What I want is to:
- Filtered out all the containers that don't have names, start or end.
- Aggregate those with the same testId, and the terms is
containers.name
and the value isend
-start
.
my attempt is that.
{
"query": {
"bool": {
"must": [
{
"match": {
"testId": "1234"
}
}
]
}
},
"_source": [
"containers"
],
"aggregations": {
"containersWithData": {
"filter": {
"bool": {
"must": [
{
"exists": {
"field": "containers.start"
}
},
{
"exists": {
"field": "containers.end"
}
}
]
}
},
"aggregations": {
"avgContainersDuration": {
"terms": {
"field": "containers.name"
},
"aggregations": {
"start": {
"avg": {
"field": "containers.start"
}
},
"end": {
"avg": {
"field": "containers.end"
}
},
"diff": {
"bucket_script": {
"buckets_path": {
"start": "start",
"end": "end"
},
"script": "params.start - params.begin"
}
}
}
}
}
}
}
}
the answer is wrong.
Mapping part
"containers": {
"properties": {
"name": {
"type": "keyword"
},
"begin": {
"type": "long"
},
"end": {
"type": "long"
}
}
},