我正在尝试构建一些 ElasticSearch 查询。
我的文件是来自市场的订单。它们来自视频游戏的 API。它们看起来像:
{
"issued" : "2021-03-14T23:50:20Z",
"location_id" : 1028858195912,
"order_id" : 5936180221,
"type_id" : 55326,
"price" : 75100.0,
"min_volume" : 1,
"volume_remain" : 47,
"volume_total" : 50,
"order_type" : "buy"
}
order_type
可以是“买入”或“卖出”。
type_id
是项目的类型。
我的目标是比较“买入”订单和“卖出”订单,以找到划算的交易。
在结果中我希望有类似的东西:
{
"type_id" : 314543
"buy_price" : 250,
"buy_location_id" : 3213541354,
"buy_quantity" : 15,
"sell_price" : 500,
"sell_location_id" : 213544254,
"estimated_profit": 3750,
},
// etc
表示所有划算的交易,按估计利润排序。
如果我能用一个非常棒的请求做到这一点,我不确定这是否可行。
因此,我正在考虑使用一个请求来获取每种商品类型的最高“销售”订单,然后针对他们中的每个人发出请求以查找与相同 type_id 相关的最佳“购买”订单。>
然后按“estimated_profit”对结果进行排序。
我尝试了一些方法,例如:
GET /orders/_search
{
"size": 0,
"query": {
"bool": {
"must": [
{"match":{"order_type": "buy"}}
]
}
},
"aggs": {
"group_by_type": {
"terms":{
"field": "type_id"
},
"aggs": {
"top_three":{
"top_hits":{
"size": 3,
"sort": [{
"price": {
"order": "desc"
}
}]
}
}
}
}
}
}
这有点奏效,但我觉得有一个我不知道的更好的解决方案。
我如何比较所有这些订单以提取最优惠的价格?