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个回答



我会通过提升来做到这一点; 将搜索者ID在其关注者中的点击量增加一定数量,然后通过较低值将搜索者点击其“跟随”字段中的点击次数提升:</ p>

注意:搜索者的ID 在这个例子中是55 </ p>

 “query”:{
“bool”:{
“should”:[
{
“nested”:{
“路径”:“粉丝”,
“查询”:{
“term”:{“followers.id”:{“value”:55,“boost”:3.0}}
}
}
},
{
“nested”:{
“path”:“follow”,
“query”:{
“term”:{“following.id”:{“value”:55,“ 提升“:2.0}}
}
}
},
{

“match_all”:{“boost”:1.0}
}

}
</ code> </ pre >

} </ p>

如果搜索者在点击关注者字段中,那么搜索者正在关注 那个命中,所以提升最高等等... </ p>

你说你想要所有其他用户,因此最后的“match_all:{} </ code>查询 。</ p>
</ div>

展开原文

原文

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.

duanfan9859
duanfan9859 ramseykhalaf,谢谢! 这是问题的解决方案。
大约 7 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问