ElasticSearch按嵌套字段值排序


ElasticSearch sorting by nested field value

我需要按下一个顺序排序结果:

  • 用户,我遵循
  • 用户,跟随我
  • 所有其他用户

我有这样的用户:

{
    "username": "admin"
    "followers": [
        {
            "id": 2,
            "username": "kiehn.nicola2"
        },
        {
            "id": 3,
            "username": "adaline253"
        },
        {
            "id": 4,
            "username": "skuhic4"
        }
    ],
    "following": [
        {
            "id": 2,
            "username": "kiehn.nicola2"
        },
        {
            "id": 3,
            "username": "adaline253"
        },
        {
            "id": 4,
            "username": "skuhic4"
        },
        {
            "id": 5,
            "username": "heaney.garth5"
         }
    ]
}

有可能吗?

当然,我知道当前用户id和用户名。

我写了这个查询,但它不起作用(例如,用户id是1):

{
    "query": {
        "bool": {
            "must": [
                {
                    "wildcard": {
                        "username": {
                            "value": "*a*",
                            "boost": 1
                        }
                    }
                }
            ]
        }
    },
    "sort": [
        {
            "following.username": {
                "order": "asc",
                "nested_path": "following",
                "nested_filter": {
                    "term": {
                        "following.id": 1
                    }
                }
            },
            "followers.username": {
                "order": "asc",
                "nested_path": "followers",
                "nested_filter": {
                    "term": {
                        "followers.id": 1
                    }
                }
            }
        }
    ],
    "size": 40
}

我将通过增强;将搜索者id在其追随者中的点击量增加一定数量,然后将搜索者id在其"following"字段中的点击量增加较低的值:

注意:在这个例子中搜索者的id是55

"query": {
   "bool": {
       "should": [
           {
               "nested": {
                   "path": "followers",
                   "query": {
                       "term" : { "followers.id": { "value": 55, "boost": 3.0 } }
                   }
               }
           },
           {
               "nested": {
                   "path": "following",
                   "query": {
                       "term" : { "following.id": { "value": 55, "boost": 2.0 } }
                   }
               }
           },
           {
               "match_all": { "boost": 1.0 }
           }
       ]           
   }

}

如果搜索者在命中的追随者字段中,那么搜索者就会跟随命中,所以boost是最高的,等等…

您说您需要所有其他用户,因此在最后使用"match_all: {}查询。