我们公司拥有超过50万家公司10年来的财务数据,包括国内货币和年末美元、欧元汇率。我们的客户需要查找本币或外币(美元、欧元)的公司,例如,查找收入范围从从到到、从、到以本币、美元或欧元的所有公司。
我的解决方案是,将汇率存储到所有财务记录中。Elasticsearch映射如下:
{
"my_index" : {
"mappings" : {
"properties" : {
"organCode" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"profitsAndLosses" : {
"type" : "nested",
"properties" : {
"revenue" : {
"type" : "double"
},
"usd_rate": {
"type": "double"
},
"eur_rate": {
"type": "double"
},
"yearReport" : {
"type" : "integer"
}
}
}
}
}
}
}在客户搜索时,我将其分为两种情况:国内货币和外币,两种类型的查询如下:情况1:国内:
GET my_index/_search
{
"query": {
"nested": {
"path": "profitsAndLosses",
"query": {
"range": {
"profitsAndLosses.revenue": {
"gte": 100000,
"lte": 200000
}
}
}
}
}
}case2:国外(例如以美元为单位)
GET my_index/_search
{
"query": {
"nested": {
"path": "profitsAndLosses",
"query": {
"script": {
"script": {
"lang": "painless",
"source": "def converted_revenue = doc[profitsAndLosses.revenue].value / doc[profitsAndLosses.usd_rate].value; converted_revenue >= params.from && converted_revenue <= params.to",
"params": {
"from": 1000,
"to": 2000
}
}
}
}
}
}
}但是,如果重复地将汇率存储到所有记录中,将消耗更多的磁盘存储空间,并且如果需要更新某一年的汇率,我需要在elasticsearch中重新索引数百万条记录。我希望将汇率存储为参考数据,我的查询可能如下所示(伪):
"source": "def converted_revenue = get_exchangeRate('usd', doc[profitsAndLosses.yearReport].value); converted_revenue >= params.from && converted_revenue <= params.to"有没有像这样创建查询的方法?或者有更好的解决方案?
发布于 2019-12-31 06:42:23
我强烈建议您进行转换,并将转换后的汇率保存在每个记录中。这正是索引的正确用例。我知道存储一个操作的结果可能感觉很奇怪或重复,但如果您可以使用常规的Range查询,那么它将使您的查询变得容易得多,并且将占用更少的计算资源进行计算。
索引用CPU、RAM和磁盘来换取速度,而在这里,您有了一个很好的机会,可以用一点磁盘来换取搜索的简单性和速度。
500k条记录*10年相当于500万条记录。对于这种大小的数据集,几个额外的双精度字段应该是相对较小的磁盘空间。当然,您最清楚自己的需求,但我非常非常确定,在花费时间编写脚本查询之前,您无法获得额外的磁盘空间。
https://stackoverflow.com/questions/59526248
复制相似问题