ElasticSearch未能按顺序对嵌套对象进行排序



ElasticSearch 6.5.2给定映射和查询,将"desc"更改为"asc"不会影响文档顺序,反之亦然。没有看到任何错误,结果中只有sort: [Infinity]

映射:

{
"mappings": {
"_doc": {
"properties": {
"tags": {
"type": "keyword"
},
"metrics": {
"type": "nested",
"dynamic": true
}
}
}
}
}

查询

{
"query": {
"match_all": {
}
},
"sort": [
{
"metrics.http.test.value": {
"order": "desc"
}
}
]
}

文件结构:

{
"tags": ["My Tag"],
"metrics": {
"http.test": {
"updated_at": "2018-12-08T23:22:07.056Z",
"value": 0.034
}
}
}

按嵌套字段排序时,需要使用nested参数告知嵌套字段的路径。查询中还缺少一个要排序的字段。假设您想在updated_at上排序,则查询将为:

{
"query": {
"match_all": {}
},
"sort": [
{
"metrics.http.test.updated_at": {
"order": "desc",
"nested": {
"path": "metrics"
}
}
}
]
}

在使用嵌套字段进行排序时,还有一件事需要记住,那就是排序中的filter子句。

显然将映射更改为:

"metrics": {
"dynamic": true,
"properties": {}
}

修复了它,并允许按正确的顺序进行排序。

最新更新