简体   繁体   English

ElasticSearch:字词查询中的相交计数

[英]ElasticSearch: Count of intersection in terms query

I'm using elasticsearch to implement full user search. 我正在使用elasticsearch来实现完整的用户搜索。 I want to return the number of mutual friends shared between the searching user and each of the results. 我想返回搜索用户与每个结果之间共享的共同朋友的数量。 I'm currently using the "terms" query to boost the score of results for users who share mutual friends with the searching user. 我目前正在使用“条款”查询来提高与搜索用户共享共同朋友的用户的结果得分。

For context, the relevant chunk of my query is pasted below: 对于上下文,我的查询的相关块粘贴在下面:

"should": [ { "terms": { "friend_ids": [ "100", "2", "8", "9", "15", "25" ] } } ]

Ideally the response would include for each doc a field called "num_mutual_friends".... 理想情况下,响应将为每个文档包含一个名为“ num_mutual_friends”的字段。

One way of achieving this is to use a script_field: 实现此目的的一种方法是使用script_field:

{
  "query": {...},
  "script_fields": {
    "num_mutual_friends": {
      "script": {
        "inline": "doc['friend_ids'].values.intersect(input_friend_ids).length",
        "params": {
          "input_friend_ids": [
            "101", "9", "8"
          ]
        }
      }
    }
  }
}

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

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