elasticsearch 如何根据nested的数量对父文档进行排序?

发布于 2021-08-11 18:09:54

如题,谢谢!

查看更多

关注者
0
被浏览
692
莫小豆
莫小豆 2021-08-11
这家伙很懒,什么也没写!
  1. 假设文档如下,首先在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
    }
  ]
}]
  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"
                  }
                }
              ]
            }
          }
        }
      }
    }
  ]
}
1 个回答

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览