【发布时间】:2020-09-11 02:00:22
【问题描述】:
我有一个名为 account 的类型,其映射如下:
"country" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"followingClientIds" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
},
"fielddata" : true
},
followingClientIds 是我关注的其他帐户的字符串 ID 数组。
我想构建一个查询,从一个国家/地区获取每个帐户,并按照我们都关注的共同帐户数对它们进行排序。
以下是我到目前为止所做的一些查询:
GET account/_search
{
"size": 20,
"query": {
"bool": {
"filter": {
"term": {
"country.keyword": "AT"
}
}
}
},
"sort": [
{
"followingClientIds.keyword": {
"order": "asc",
"nested_filter": {
"terms": {
"followingClientIds.keyword": [
"dFbEW23hVZ3w8jhH9LeCw3QG33UjuF5C"
]
}
}
}
}
]
}
例如,我有这 3 个帐户类型的文档:
{
"username": "user2",
"country": "AT",
"followingClientIds": ["abc"]
},
{
"username": "user3",
"country": "AT",
"followingClientIds": ["abc", "bcd", "cde"]
},
{
"username": "user4",
"country": "AT",
"followingClientIds": ["abc"]
}
假设我将向查询发送 country 和 followingClientIds 以进行排序:
{
"country": "AT",
"followingClientIds": ["abc", "bcd", "cde"]
}
我希望结果是这样的:
{
"username": "user3",
"country": "AT",
"followingClientIds": ["abc", "bcd", "cde"],
"fields": [ // dont really need this custom field, but would be cool
"mutual_following_count": 3
]
},
{
"username": "user2",
"country": "AT",
"followingClientIds": ["abc"],
"fields": [
"mutual_following_count": 1
]
},
{
"username": "user4",
"country": "AT",
"followingClientIds": ["abc"],
"fields": [
"mutual_following_count": 1
]
}
【问题讨论】: