gpt4 book ai didi

elasticsearch - 在 Elasticsearch 上按多个值嵌套排序

转载 作者:行者123 更新时间:2023-11-29 02:47:44 24 4
gpt4 key购买 nike

我想使用多个值对嵌套的 Elasticsearch 进行排序。这是一个例子:

我有一些Events,它们有一个嵌套的topics,像这样

    "_source": {
"topics": [
{
"type": "Tools",
"name": "Data Science",
"id": 19
},
{
"type": "Challenges",
"name": "Disaster Resilience",
"id": 1
},
{
"type": "Tools",
"name": "Entrepreneurship",
"id": 21
},
{
"type": "Challenges",
"name": "Prosperity",
"id": 8
}
]
...
}

此外,成员 具有相同的嵌套主题,使用相同的结构。

我想做的是根据成员(member)主题排序事件。例如,如果一个成员有三个与事件匹配的主题,以及两个与另一个匹配的主题,我想首先显示最匹配的事件。

我试过这样的:

"sort":[
{
"topics.id":{
"nested_path":"topics",
"mode":"sum",
"order":"asc",
"nested_filter":{
"match": {
"topics.id": 13
}
}
}
}
]

适用于特定主题。但我想在下面做类似的事情,在 sort 中使用多个值,首先返回最匹配的事件。在这种情况下,包含主题 13 和 14 的事件将比仅包含主题 13 的事件首先返回,所有其他不匹配的事件将在之后显示。

"sort":[
{
"topics.id":{
"nested_path":"topics",
"mode":"sum",
"order":"asc",
"nested_filter":[
{
"match": {
"topics.id": 13
}
},
{
"match": {
"topics.id": 14
}
}
]
}
}
]

已编辑:这是我在使用最后一个片段时遇到的错误:

{
"error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "[field_sort] nested_filter doesn't support values of type: START_ARRAY"
}
],
"type": "illegal_argument_exception",
"reason": "[field_sort] nested_filter doesn't support values of type: START_ARRAY"
},
"status": 400
}

但不幸的是,这不起作用。有没有办法做到这一点?我在这里错过了一些很棒的功能吗?

谢谢!

最佳答案

我能够使用 Painless 脚本解决这个问题。如果这是性能方面的,我不知道,但它正在工作。如果我找到更好的解决方案,也会发布这个。

{
"query":{
"bool":{
"must":[
{
"nested":{
"path":"topics",
"query":{
"function_score":{
"script_score":{
"script":{
"lang":"painless",
"params":{
"ids":[
1,
14,
19,
13,
19,
21,
8
]
},
"inline":"int s = 0; for (int i = 0; i < params.ids.length; i++){ for (int j = 0; j < doc['topics.id'].value; j++) { if (params.param1[i] == doc['topics.id'][j]) { s =+ 1 }}} return s"
}
}
}
}
}
}
]
}
}
}

关于elasticsearch - 在 Elasticsearch 上按多个值嵌套排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43477813/

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