gpt4 book ai didi

Elasticsearch : Sorting on inner hits

转载 作者:行者123 更新时间:2023-12-02 23:14:09 28 4
gpt4 key购买 nike

我有这样的有效负载,所有文档中都会出现相同的有效负载,但每个文档中每个标签的权重都不同。

{
"tags": [
{
"tag": "tag1",
"weight": 5
},
{
"tag": "tag2",
"weight": 10
},
{
"tag": "tag3",
"weight": 7
}
]
}

我在映射中使用了嵌套字段,因为我想匹配数组的一个元素而不是整个数组。

{
"mappings": {
"doc": {
"properties": {
"tags": {
"type": "nested",
"properties": {
"tag": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"weight": {
"type": "double"
}
}
}
}
}
}
}

我的要求是搜索标签并根据文档中的权重对该特定标签进行排序。我试过下面的查询,但它没有按预期排序。

{
"from": 0,
"size": 50,
"sort": [
{
"tags.weight": {
"order": "asc",
"nested_filter": {
"term": {
"tags.tag": "tag1"
}
}
}
}
]
}

编辑 添加示例

例如:假设有 3 个文档

{     
"uid":1,
"tags": [
{
"tag": "tag1",
"weight": 5
},
{
"tag": "tag2",
"weight": 10
},
{
"tag": "tag3",
"weight": 7
}
]
},
{
"uid":2,
"tags": [
{
"tag": "tag1",
"weight": 9
},
{
"tag": "tag2",
"weight": 10
},
{
"tag": "tag3",
"weight": 7
}
]
},
{
"uid":3,
"tags": [
{
"tag": "tag1",
"weight": 7
},
{
"tag": "tag2",
"weight": 10
},
{
"tag": "tag3",
"weight": 7
}
]
}

当搜索“tag1”并按升序排序时,我应该得到以下输出即文档应该根据“tag1”的权重进行排序。

{
"uid": 1,
"tags": [
{
"tag": "tag1",
"weight": 5
},
{
"tag": "tag2",
"weight": 10
},
{
"tag": "tag3",
"weight": 7
}
]
},
{
"uid": 3,
"tags": [
{
"tag": "tag1",
"weight": 7
},
{
"tag": "tag2",
"weight": 10
},
{
"tag": "tag3",
"weight": 7
}
]
},
{
"uid": 2,
"tags": [
{
"tag": "tag1",
"weight": 9
},
{
"tag": "tag2",
"weight": 10
},
{
"tag": "tag3",
"weight": 7
}
]
}

提前致谢!!

最佳答案

inner_hits属性中,可以设置sort

例子:

"inner_hits":{
"sort": [
{
"tags.tag": {
"order": "asc"
}
}
]
}

然后,在 result 上,在 hits 中,您将拥有排序对象的 inner_hits 属性。

关于 Elasticsearch : Sorting on inner hits,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57320052/

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