Hi Tamara, I got another trouble here, could u please help me with this?
Say I'm having a mapping like
{
"transaction_id": {"type": "keyword"},
"transaction_pattern": {"type": "keyword"},
"transaction_cost": {"type": "integer"},
"created_at": {"type": "date", "format": "......"}
}
There are like 500 distinct transaction_pattern in my index, and the number of these patterns can be viewed as fixed.
Now I am asked to extract the documents which are created in the last 10 minutes (because I have to run this query every 10 minutes), and the transaction_cost is 50% higher than the AVG cost of the same transaction_pattern in the last 7 days.
The following is what I've done so far, but I am still far from my goal.
{
"size": 0,
"query": {
"bool": {
"filter": {
"range": {
"created_at": {
"gte": "now-7d/d",
"lte": "now/d"
}
}
}
}
},
"aggs": {
"groupby": {
"terms": {
"field": "transaction_pattern",
"size": 1000
},
"aggs": {
"last_ten_min": {
"filter": {
"range": {
"created_at": {
"gte": "now-10m/m",
"lte": "now-1m/m"
}
}
}
},
"last_ten_min_selector": {
"bucket_selector": {
"buckets_path": {
"count": "last_ten_min._count"
},
"script": {
"inline": "params.count > 0"
}
}
},
"last_seven_day": {
"filter": {
"range": {
"created_at": {
"gte": "now-7d/d",
"lte": "now-1d/d"
}
}
},
"aggs": {
"avg_cost": {
"avg": {
"field": "transaction_cost"
}
}
}
},
"last_seven_day_selector": {
"bucket_selector": {
"buckets_path": {
"count": "last_seven_day._count"
},
"script": {
"inline": "params.count > 0"
}
}
}
}
}
}
}
Obviously, I can get the docs created in the last 10 mins in last_ten_min aggs, and also I can calculate the AVG cost of the same pattern in the last 7 days in last_seven_day aggs, but HOW CAN I MAKE USE OF the avg cost in filtering out the docs in last_ten_min aggs?
For example, say there is a transaction_pattern like "hello, world", which occurred in the last 7 days for many times, and the AVG cost of this specific pattern is 100. And in the last 10 minutes, "hello, world" occurred 5 times, the costs are 180, 120, 90, 100, 170 respectively. So my goal is to find out the docs with cost 180 and 170, because both 180 & 170 are 50% higher than the 7-day-average-cost, which is 100.