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 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler