I have below mapping in my index:
{
"testIndex": {
"mappings": {
"type1": {
"properties": {
"text": {
"type": "string"
},
"time_views": {
"type": "nested",
"properties": {
"timestamp": {
"type": "long"
},
"views": {
"type": "integer"
}
}
}
}
}
}
}
}
actually "time_views" is an array, but inner attributes not array. this nested type holds history of view count of type1. "views" is an accumulative attribute.
I want to query on my document that retrieve sum of "views" of all documents that an arbitrary word in "text" field occurs in that documents.
I know I should use aggregation but I don't know how for this query.
{
"query": {
"term":{
"text": "anyword"
}
},
"size": 0,
"aggs":{
???
}
}
as I mentioned above "time_views" is an array for each document and I just want to use the maximum value of "views" of each array.
Sample data
{
"text": "red car",
"time_views": [
{
"timestamp": 1651116565,
"views": 100
},
{
"timestamp": 1651546456,
"views": 153
},
{
"timestamp": 165446456,
"views": 200
}
]
},
{
"text": "blue car",
"time_views": [
{
"timestamp": 1651116565,
"views": 20
},
{
"timestamp": 1651546456,
"views": 70
},
{
"timestamp": 165446456,
"views": 130
}
]
},
{
"text": "green car",
"time_views": [
{
"timestamp": 1651116565,
"views": 4
},
{
"timestamp": 1651546456,
"views": 86
},
{
"timestamp": 165446456,
"views": 100
}
]
}
I expect to get below result when I query for "car":
{
"text": "car"
"views": 430
}
where 430 = 200 (max value in first doc) + 130 (max value in second doc) + 100 (max value in third doc)
I don't care about Json structure of the result, I just need the information.
so what should I do? tnx :)