- 假设文档如下,首先在nested子文档里增加一个
weight
字段
[{
"title": "Investment secrets",
"body": "What they don't tell you ...",
"tags": [ "shares", "equities" ],
"comments": [
{
"id": 12313,
"city": "Pune",
"name": "Mary Brown 1",
"comment": "Lies, lies, lies ",
"date": "2018-10-18",
"weight": 1
},
{
"id": 12314,
"city": "Pune",
"name": "Mary Brown 1",
"comment": "Lies, lies, lies ",
"date": "2018-10-20"
"weight": 1
}
]
},
{
"title": "Investment secrets",
"body": "What they don't tell you ...",
"tags": [ "shares", "equities" ],
"comments": [
{
"id": 12315,
"city": "Pune",
"name": "Mary Brown ",
"comment": "Lies, lies, lies ",
"date": "2018-10-18",
"weight": 1
},
{
"id": 12316,
"city": "Bangalore",
"name": "Mary Brown ",
"comment": "Lies, lies, lies ",
"date": "2018-10-20",
"weight": 1
}
]
}]
- 然通过对
comment.weight
进行sum
排序即可:
POST blogs/_search
{
"sort": [
{
"comments.weight": {
"mode": "sum",
"order": "desc",
"missing": "0",
"nested": {
"path": "comments",
"filter": {
"bool": {
"must": [
{
"match": {
"comments.city": "Pune"
}
}
]
}
}
}
}
}
]
}
问 elasticsearch 如何根据nested的数量对父文档进行排序?