简体   繁体   English

Elasticsearch按动态价格排序

[英]Elasticsearch sort by dynamic price

This is the scenario: 这是方案:

Imagine that a register user creates a new entity with the price: 1 | currency: EUR 想象一下,注册用户使用以下price: 1 | currency: EUR创建了一个新实体price: 1 | currency: EUR price: 1 | currency: EUR and other registered user creates a new entity with price: 1 | currency: USD price: 1 | currency: EUR和其他注册用户创建price: 1 | currency: USD的新实体price: 1 | currency: USD price: 1 | currency: USD and so on... price: 1 | currency: USD ,依此类推...

What I'm trying to do is sort by price the records depending on the currency selected by the frontend user (EUR or USD). 我想要做的是根据记录价格对记录进行排序,具体取决于前端用户选择的货币(欧元或美元)。

I can not index, for example price_eur or price_usd , because the rates change every hour, but if is possible, what is the best way to update the price for 10000 records?. 我无法建立索引,例如price_eurprice_usd ,因为费率每小时都在变化,但是如果可能的话,更新10000条记录的价格的最佳方法是什么?

1 - Below are my suggestions, it works, but I think could be a better approach, any suggestion? 1-以下是我的建议,它可行,但是我认为这可能是更好的方法,有什么建议吗?

2 - If there is not, what will be the better performance approach? 2-如果没有,哪种性能更好?

3 - ES has some magic filed to play and used, for expample price_in_eur ? 3-ES拥有一些可玩和可使用的魔力,例如price_in_eur

Function score 功能评分

{
    "query": {
        "function_score": {
            "functions": [
                {
                    "script_score": {
                        // 1 EUR = 0.74452 USD
                        "script": "('EUR' == _source.currency ? doc['boat.price'].value : doc['boat.price'].value * 0.744520)"
                     }
                }
            ],
            "query": {
                "filtered": {
                    "query": { "match_all":{} }
                }
            }
        } 
    },
    "sort": { "_score": { "order": "desc" }}
}

Sort script 排序脚本

{
    "fields": ["_source"],
    "query": { "match_all": {} },  
    "sort": {
        "_script": {
            // 1 EUR = 0.74452 USD
            "script": "('EUR' == _source.currency ? doc['boat.price'].value : doc['boat.price'].value * 0.744520)",
            "type" : "number",
            "order": "asc"

        }
    }
}

Thanks for your time. 谢谢你的时间。

There is no direct way by which ES will do the price conversion for you automatically. ES没有直接方法可以自动为您进行价格转换。 The way you are doing is the correct way of accomplishing the task. 您所做的方式是完成任务的正确方法。 You should go for a function score query since it'll be better compared to a single script based sorting. 您应该进行功能评分查询,因为与基于单个脚本的排序相比,它会更好。 As the docs states: 文档所述:

Note, it is recommended, for single custom based script based sorting, to use function_score query instead as sorting based on score is faster. 请注意,对于基于单个自定义脚本的排序,建议使用function_score查询代替,因为基于分数的排序更快。

Using a function score query will be better since sorting on a score value is faster. 使用功能分数查询会更好,因为对分数值进行排序会更快。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM