gpt4 book ai didi

elasticsearch - 获取带有排序嵌套对象的文档

转载 作者:行者123 更新时间:2023-12-03 01:04:43 26 4
gpt4 key购买 nike

我有以下映射:http://pastebin.com/FgtC5aaw(包括样本数据)

http://localhost:9200/databases/database/1


{
"columns": [
{
"name": "First column",
"position": 1
},
{
"name": "Second column",
"position": 2
},
{
"name": "Third column",
"position": 3
}
],
"name": "Database name"
}

我想得到的是“数据库”文档,但是每个带有列的列均按columns.position字段降序排列,因此:
{
"name": "Database name",
"columns": [
{
"name": "Third column",
"position": 3
},
{
"name": "Second column",
"position": 2
},
{
"name": "First column",
"position": 1
}
]
}

我尝试在“columns.position”上使用“sort”,但是我认为它只会对嵌套列上的所有“数据库”文档进行排序。

在Elasticsearch中这样的结果可能吗?

最佳答案

如果事先知道,您将始终需要按nested的降序检索columns.position对象列,您只需使用已经正确排序的columns列表对数据库文档进行索引即可。

如果不是这种情况,有一种方法可以使用 columns 对嵌套的inner_hits列表进行排序,如下所示:

curl -XPOST localhost:9200/databases/database/_search -d '{
"_source": false, <---- set this to true to get database fields too
"query": {
"nested": {
"path": "columns",
"query": {
"match_all": {}
},
"inner_hits": {
"sort": {
"columns.position": "desc"
}
}
}
}
}'

这将产生:
{
...
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [ {
"_index" : "databases",
"_type" : "database",
"_id" : "1",
"_score" : 1.0,
"inner_hits" : {
"columns" : {
"hits" : {
"total" : 3,
"max_score" : null,
"hits" : [ {
"_index" : "databases",
"_type" : "database",
"_id" : "1",
"_nested" : {
"field" : "columns",
"offset" : 2
},
"_score" : null,
"_source":{"name":"Third column","position":3},
"sort" : [ 3 ]
}, {
"_index" : "databases",
"_type" : "database",
"_id" : "1",
"_nested" : {
"field" : "columns",
"offset" : 1
},
"_score" : null,
"_source":{"name":"Second column","position":2},
"sort" : [ 2 ]
}, {
"_index" : "databases",
"_type" : "database",
"_id" : "1",
"_nested" : {
"field" : "columns",
"offset" : 0
},
"_score" : null,
"_source":{"name":"First column","position":1},
"sort" : [ 1 ]
} ]
}
}
}
} ]
}
}

关于elasticsearch - 获取带有排序嵌套对象的文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32342511/

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