更新时间:2023-02-08 20:19:55
根据嵌套文档中的字段进行排序的支持被添加到弹性搜索0.90:
Support for sorting based on fields inside of nested documents was added to elasticsearch in 0.90:
https:// github.com/elasticsearch/elasticsearch/issues/2662
嵌套字段支持的排序具有以下功能已经存在的排序选项的
顶部的g参数:
The sorting by nested field support has the following parameters on top of the already existing sort options:
nested_path
- 定义要排序的嵌套对象。实际的 nested_filter
过滤 nested_filter
nested_path
- Defines the on what nested object to sort. The actual
sort field must be a direct field inside this nested object. The
default is to use the most immediate inherited nested object from the
sort field.nested_filter
- A filter the inner objects inside the
nested path should match with in order for its field values to be
taken into account by sorting. Common case is to repeat the query /
filter inside the nested filter or query. By default no nested_filter
is active.鉴于您的示例数据,以下查询应该提供您以后的内容:
Given your example data, the following query should give you what you're after:
{
"query": {
"match_all": {}
},
"sort": [
{
"examples.source_score": {
"order": "desc",
"nested_path": "examples",
"nested_filter": {
"term": {
"examples.evidence_source": "friend"
}
}
}
}
]
}