简体   繁体   English

ElasticSearch按嵌套字段值排序

[英]ElasticSearch sorting by nested field value

I need to sort result in next order: 我需要按下一个顺序对结果进行排序:

  • Users, that I following 用户,我关注
  • Users, that follows me 用户,跟着我
  • All other users 所有其他使用者

I have users which look like this: 我有一些看起来像这样的用户:

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

Is it possible? 可能吗?

Of course, I know current user id and username. 当然,我知道当前的用户ID和用户名。

I write this query, but it doesn't work (for example, user id is 1): 我编写了此查询,但是它不起作用(例如,用户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
}

I would do this by boosting; 我会通过加强来做到这一点; boost the hits that have the searchers id in their followers by an amount, then boost by a lower value the hits that have the searcher in their 'following' field: 将搜索者在其关注者中具有ID的匹配量提高一定数量,然后将“搜索”字段中具有搜索者的匹配量降低一个较低的值:

NOTE: the searcher's id is 55 in this example 注意:在此示例中,搜索者的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 }
           }
       ]           
   }

} }

If the searcher is in the hit's followers field, then the searcher is following that hit and so the boost is highest, etc... 如果搜索者在匹配项的关注者字段中,则搜索者正在跟踪该匹配项,因此提升幅度最高,依此类推...

You said you wanted all other users, hence the "match_all: {} query at the end. 您说您想要所有其他用户,因此在"match_all: {}使用"match_all: {}查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM