Rank beginner at ElasticSearch here.
I have a list of customers, and their orders as a nested field. Assuming a document structure like:
[
{ customerId: 123,
birthday: 1980-01-01,
orders: [
{
orderValue: 1500,
orderDate: 2018-12-18T12:18:12Z
},
[...]
]
},
[...]
}
What I'd like to query is: The list of users who ordered for a certain amount from between two dates. And I'd like to be able to combine that with a range query for, for example, birthday.
I've gotten to the point where I can get the sum ordered between two dates per subscriber using aggregations:
{
"size": 0,
"aggs": {
"foo": {
"nested": {
"path": "orders"
},
"aggs": {
"grouped_by_customerId": {
"terms": {
"field": "orders.customerId.keyword"
},
"aggs": {
"filtered_by_date": {
"filter": {
"range": {
"orders.orderDate": {
"from": "2018-01-28",
"to": null,
"include_lower": false,
"include_upper": true,
"format": "yyyy-MM-dd",
"boost": 1
}
}
},
"aggs": {
"sum": {
"sum": {
"field": "orders.orderValue"
}
}
}
}
}
}
}
}
}
}
However, I'd like to limit the results I get back in the Query part, to mix better with all our other filters.
My first thought was to have a script filter and pass the bounding dates and minimum value in as parameters, but then I'd have to iterate over a doc's nested documents, and that doesn't seem to work.
Is that last idea possible, and if so, how?
Thanks!
orders.orderValue, right? and now you just need to compare that sum with some parameter?