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

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题