1

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

  • 用户,我关注
  • 跟随我的用户
  • 所有其他用户

我的用户看起来像这样:

{
    "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
}
4

1 回答 1

1

我会通过提升来做到这一点;将搜索者在其追随者中的点击量提高一定数量,然后将搜索者在其“关注”字段中的点击量提高一个较低的值:

注意:在这个例子中搜索者的 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 }
           }
       ]           
   }

}

如果搜索者在命中的追随者字段中,则搜索者正在关注该命中,因此提升最高,等等......

你说你想要所有其他用户,因此最后的"match_all: {}查询。

于 2013-06-05T07:25:37.057 回答