dongshao5573
2013-06-04 23:08
浏览 122
已采纳

ElasticSearch按嵌套字段值排序

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.

I write this query, but it doesn't work (for example, user id is 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
}
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doulanli6146 2013-06-05 07:25
    已采纳

    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:

    NOTE: the searcher's id is 55 in this example

    "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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题