弹性搜索按动态价格排序 [英] Elasticsearch sort by dynamic price

查看:103
本文介绍了弹性搜索按动态价格排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是场景:



想象一下注册用户使用价格创建一个新实体:1 |货币:EUR 和其他注册用户创建一个新实体,具有 price:1 |货币:USD 等等...



我要做的是按价格排序,根据由前端用户(欧元或美元)。



我无法索引,例如 price_eur price_usd ,因为费率每小时都在变化,但如果可能,最新的方法是更新10000条记录的价格。



1 - 以下是我的建议,它有效,但我认为可以是一个更好的方法,任何建议?



2 - 如果没有,将是什么更好的表现方法?



3 - ES有一些魔术场合要玩,用于示例 price_in_eur

功能得分

  {
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}}
}

排序脚本

  {
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

}
}
}

感谢你的时间。

解决方案

ES将自动为您进行价格转换。你正在做的是完成任务的正确方法。你应该去做功能分数查询,因为与单个基于脚本的排序相比会更好。由于文档指出:使用function_score查询,而不是根据分数进行排序是
更快。


使用功能得分查询将会更好,因为对得分值的排序更快。



谢谢


This is the scenario:

Imagine that a register user creates a new entity with the price: 1 | currency: EUR and other registered user creates a new entity with price: 1 | currency: USD and so on...

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?.

1 - Below are my suggestions, it works, but I think could be a better approach, any suggestion?

2 - If there is not, what will be the better performance approach?

3 - ES has some magic filed to play and used, for expample 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. 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.

Using a function score query will be better since sorting on a score value is faster.

Thanks

这篇关于弹性搜索按动态价格排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆