I'm trying to create a query that returns documents based on a calculation that is best compared to a SumIf in Excel.
A very simplified version of my mapping is:
{
"item": {
"properties": {
"name": {
"type": "text"
},
"data": {
"properties": {
"inventory": {
"properties": {
"locations": {
"type": "nested",
"properties": {
"state": {
"type": "keyword"
},
"amount": {
"type": "double"
}
}
},
"total": {
"type": "double"
}
}
}
}
}
}
}
}
(This data wouldn't make much sense in a real system, but I can't post the actual mapping.)
Two example documents could be:
{
"name": "guitar",
"data": {
"inventory": {
"locations": [
{
"state": "CA",
"amount": 200
},
{
"state": "AZ",
"amount": 100
},
{
"state": "NY",
"amount": 300
}
],
"total": 600
}
}
}
{
"name": "piano",
"data": {
"inventory": {
"locations": [
{
"state": "CA",
"amount": 200
},
{
"state": "AZ",
"amount": 100
},
{
"state": "KY",
"amount": 50
}
],
"total": 350
}
}
}
I'd like to be able to return a query that returns documents where the sum of the amount of the 'NY' and 'CA' nested documents is larger than 150. In the case with the example data it should return both documents, even though NY is not in the first document, since CA by itself is already at 200.
I've read most of the aggregations documentation that Elastic provides, but I've been unable to find a way to do this.
I'd love to get some pointers on how to solve this issue.