我有这样的文件(这里有两个例子):
{
"id": 1234,
"title": "the title",
"body": "the body",
"examples": [
{
"evidence_source": "friend",
"source_score": 15
},
{
"evidence_source": "parent",
"source_score": 12
}
]
}
Run Code Online (Sandbox Code Playgroud)
和
{
"id": 6346,
"title": "new title",
"body": "lots of content",
"examples": [
{
"evidence_source": "friend",
"source_score": 10
},
{
"evidence_source": "parent",
"source_score": 27
},
{
"evidence_source": "child",
"source_score": 4
}
]
}
Run Code Online (Sandbox Code Playgroud)
examples数组中子文档的格式将始终具有a evidence_source和a,source_score但是这些子文档的数量可变,每个子文档具有不同的evidence_source值.
我想知道是否可以根据source_score与特定值匹配的值之一对此格式的文档进行排序evidence_source.我真的希望能够做到这一点:
source_score下降,其中相关evidence_source的friend.由此产生的文件订购顺序为id1234,6346.source_score下降,其中相关evidence_source的parent.由此产生的文档订购顺序为id6346,1234.我做出这样的事情的最接近的结果是1和2,但我不相信他们完全符合我的想法.
关于我如何解决这个问题的任何想法?我已经考虑了一些基于分别索引这些examples子文档的想法,但我对弹性搜索还不熟悉,所以我正在寻找一些关于如何以最直接的方式实现我的目标的建议(这可能是一个梦想...)
更新:elasticsearch邮件列表上的帖子似乎表明这是不可能的,但我想知道这里的其他人是否有任何不同的想法!
Dan*_*e B 18
支持基于嵌套文档内的字段进行排序已添加到0.90中的elasticsearch:
https://github.com/elasticsearch/elasticsearch/issues/2662
嵌套字段支持的排序在现有排序选项之上具有以下参数:
nested_path- 定义要排序的嵌套对象.实际的排序字段必须是此嵌套对象中的直接字段.默认设置是使用排序字段中最直接的继承嵌套对象.nested_filter- 嵌套路径内的内部对象应匹配的过滤器,以便通过排序考虑其字段值.常见的情况是在嵌套过滤器或查询中重复查询/过滤.默认情况下,没有nested_filter活动.
根据您的示例数据,以下查询应该为您提供所需的内容:
{
"query": {
"match_all": {}
},
"sort": [
{
"examples.source_score": {
"order": "desc",
"nested_path": "examples",
"nested_filter": {
"term": {
"examples.evidence_source": "friend"
}
}
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8448 次 |
| 最近记录: |