【发布时间】:2016-04-24 05:12:20
【问题描述】:
我的索引中有两种类型(Event 和 City),我正在尝试按日期对它们进行排序。但是,每种类型的日期字段名称不同:
对于Event,值在updated_at 字段中,对于City,日期在其city_events 嵌套对象数组的嵌套对象之一的update_at 字段中(注意region_id 的过滤)。
我试过像这样指定排序数组中的每个字段:
"sort": [
{
"city_events.updated_at": {
"order": "desc",
"nested_path": "city_events",
"nested_filter": {
"term": {
"city_events.region_id": 1
}
}
}
},
{
"updated_at": "desc"
}
]
但不幸的是,这并没有将这两种类型混合在一起。相反,它首先按嵌套的city_events.updated_at 字段对所有Cities 进行排序,然后将所有Events 附加到按其updated_at 字段排序的底部。如何将两者混合和排序?
作为替代解决方案,我尝试仅按嵌套的 city_events.updated_at 字段进行排序并指定 "missing": "updated_at",但是尽管两个字段的格式相同,但仍引发了 "number_format_exception" 错误:
{
"error": {
"root_cause": [
{
"type": "number_format_exception",
"reason": "For input string: \"updated_at\""
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query_fetch",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "events_1461095196252",
"node": "sYQstSw_SN62ojmXgGjPlg",
"reason": {
"type": "number_format_exception",
"reason": "For input string: \"updated_at\""
}
}
]
},
"status": 400
}
更新 1:基于下面的the answer by Andrei Stefan,我尝试开发一个 groovy 脚本,该脚本循环遍历每个City 文档的 city_events,选择具有匹配 region_id 的文档,然后返回 city_event 的 updated_at 值进行评分,但在访问脚本中的嵌套字段时遇到问题:https://stackoverflow.com/questions/36781476/elasticsearch-access-fields-inside-array-of-nested-objects-in-a-groovy-script
【问题讨论】:
标签: elasticsearch