可以在ElasticSearch查询中完成:
SELECT COUNT(DISTINCT users_contacts.value)
FROM users
INNER JOIN users_contacts ON users_contacts.user_id = users.id
WHERE users_contacts.type = "email"
如果没有users_contacts.type = "email"
条件,我可以使用ValueCount聚合。
答案 0 :(得分:0)
是的,您可以使用top_hits聚合,如下面的文档链接
所示修改强>
我尽力展示如何做到这一点。首先,我希望您将联系人保存在自己的文档中
<强> 1。创建映射
{
"dynamic": true,
"_all": {
"enabled": false
},
"properties":{
"user_id" : {
"type": "integer"
},
"contact_type" : {
"type":"string",
"index": "not_analyzed"
}
}
}
<强> 2。插入文件
使用此模板创建多个文档(更改文本,数字和日期)
{
"user_id": 5,
"contact_type": "email"
}
第3。查询本身
{
"query": {
"term": {
"contact_type":{
"value":"email"
}
}
},
"aggs": {
"contact-counts": {
"terms": {
"field": "user_id"
}
}
}
}
4.查看结果
在其中搜索聚合键和联系人计数
它应该看起来像这样
"aggregations": {
"contact-counts": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 4, // user_id
"doc_count": 1 // document count
}
]
}
}
我希望它有所帮助! :)
修改强>
最后你不需要top_hits聚合,只需要普通聚合本身:)