gpt4 book ai didi

sorting - 如何在Elasticsearch中按计算值排序

转载 作者:行者123 更新时间:2023-12-02 22:31:31 25 4
gpt4 key购买 nike

我想将elasticsearch用作搜索引擎。我正在将记录从mysql复制到elasticsearch,当我查询elasticsearch时,我想使用Elastic中的数据计算一个值并使用它对结果进行排序

我的索引如下:

  {
"busquedas" : {
"aliases" : { },
"mappings" : {
"coche" : {
"properties" : {
"coeff_e" : {
"type" : "double"
},
"coeff_r" : {
"type" : "double"
},
"desc" : {
"type" : "string"
}
}
}
},
"settings" : {
"index" : {
"creation_date" : "1460116924258",
"number_of_shards" : "5",
"number_of_replicas" : "1",
"uuid" : "N6jhy_ilQmSb6og16suZ4g",
"version" : {
"created" : "2030199"
}
}
},
"warmers" : { }
}

}

我想计算每个记录的值
myCustomOrder = (coeff_e + coeff_r) * timestamp

并使用它对结果进行排序
{
"sort" : [
{ "myCustomOrder" : {"order" : "asc"}},
"_score"
],
"query" : {
"term" : { ... }
}
}

我知道我可以使用groovy来计算值,但是我只能使用它来过滤,如示例中所示
{
"from": 10,
"size": 3,
"filter": {
"script": {
"script": "doc['coeff_e'].value < 0.5"
}
}
}

预先谢谢您,我完全是Elasticsearch的新手:D

最佳答案

与过滤相同。看看这个section of the documentation。一旦您已经了解脚本:-),它应该是不言自明的。

为了完整起见:

{
"query" : {
....
},
"sort" : {
"_script" : {
"type" : "number",
"script" : {
"inline": "doc['field_name'].value * factor",
"params" : {
"factor" : 1.1
}
},
"order" : "asc"
}
}
}

关于sorting - 如何在Elasticsearch中按计算值排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36500737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com