【发布时间】:2021-04-19 07:04:55
【问题描述】:
我有一个索引“candidate_ranking”,其中包含具有以下文档结构的文档。索引的每个文档都有 1 个候选 ID,并且可以有多个具有相同候选 ID 的文档,但是它们的 created_at 字段会有所不同。这是我的候选排名索引中的文档样本-
"hits" : [
{
"_index" : "candidate_ranking",
"_type" : "_doc",
"_id" : "SCa26HgB0zUr7edEvDul",
"_score" : 1.0,
"_source" : {
"id" : 118558,
"candidate_id" : 29492,
"created_at" : "2021-03-27T01:34:29.628550+00:00",
"badge" : "2"
}
},
{
"_index" : "candidate_ranking",
"_type" : "_doc",
"_id" : "SSa26HgB0zUr7edEvDul",
"_score" : 1.0,
"_source" : {
"id" : 133354,
"candidate_id" : 29492,
"created_at" : "2021-03-27T02:11:35.811420+00:00",
"badge" : "2"
}
},
{
"_index" : "candidate_ranking",
"_type" : "_doc",
"_id" : "Sia26HgB0zUr7edEvDul",
"_score" : 1.0,
"_source" : {
"id" : 148136,
"candidate_id" : 29492,
"created_at" : "2021-03-29T20:20:36.482066+00:00",
"badge" : "2"
}
},
{
"_index" : "candidate_ranking",
"_type" : "_doc",
"_id" : "Sya26HgB0zUr7edEvDul",
"_score" : 1.0,
"_source" : {
"id" : 162916,
"candidate_id" : 29492,
"created_at" : "2021-03-29T21:05:03.985032+00:00",
"badge" : null
}
},
{
"_index" : "candidate_ranking",
"_type" : "_doc",
"_id" : "TCa26HgB0zUr7edEvDul",
"_score" : 1.0,
"_source" : {
"id" : 177712,
"candidate_id" : 29492,
"created_at" : "2021-03-29T21:33:32.596613+00:00",
"badge" : null
}
},
{
"_index" : "candidate_ranking",
"_type" : "_doc",
"_id" : "TSa26HgB0zUr7edEvDul",
"_score" : 1.0,
"_source" : {
"id" : 192999,
"candidate_id" : 29492,
"created_at" : "2021-03-29T22:20:24.942116+00:00",
"badge" : null
}
},
{
"_index" : "candidate_ranking",
"_type" : "_doc",
"_id" : "Tia26HgB0zUr7edEvDul",
"_score" : 1.0,
"_source" : {
"id" : 225434,
"candidate_id" : 29492,
"created_at" : "2021-03-29T23:13:59.266074+00:00",
"badge" : null
}
},
{
"_index" : "candidate_ranking",
"_type" : "_doc",
"_id" : "Tya26HgB0zUr7edEvDul",
"_score" : 1.0,
"_source" : {
"id" : 247169,
"candidate_id" : 29492,
"created_at" : "2021-03-30T00:16:04.077245+00:00",
"badge" : null
}
},
{
"_index" : "candidate_ranking",
"_type" : "_doc",
"_id" : "UCa26HgB0zUr7edEvDul",
"_score" : 1.0,
"_source" : {
"id" : 271179,
"candidate_id" : 29492,
"created_at" : "2021-03-30T01:19:59.803999+00:00",
"badge" : null
}
},
{
"_index" : "candidate_ranking",
"_type" : "_doc",
"_id" : "USa26HgB0zUr7edEvDul",
"_score" : 1.0,
"_source" : {
"id" : 295537,
"candidate_id" : 29492,
"created_at" : "2021-03-30T02:23:42.077149+00:00",
"badge" : null
}
}
]
}
此徽章值可以是空字符串或“1”或“2”。
我目前正在使用此聚合来获取具有徽章值 1 和 2 的所有用户的计数
GET /candidate_ranking/_search
{
"aggs": {
"mega_mogul": {
"terms": {
"field": "badge.keyword",
"exclude": ["", "2"],
"size": 500000
}
},
"rising_mogul": {
"terms": {
"field": "badge.keyword",
"exclude": ["", "1"],
"size": 500000
}
}
}
}
我的索引包含每个“candidate_id”的多个文档。 我只想为每个candidate_id 的最新文档进行徽章聚合。 类似于按 created_at 字段降序排序或排序,然后只为每个候选 ID 取最高值。因此将所有拥有最新徽章的候选人计算为 1 或 2。
我试过这样做,但没有用
GET /candidate_ranking/_search
{
"aggs": {
"mega_mogul": {
"terms": {
"field": "badge.keyword",
"exclude": ["", "2"],
"size": 500000,
"order": {"created_at": "desc"},
"top_hits": {"size":1}
}
},
"rising_mogul": {
"terms": {
"field": "badge.keyword",
"exclude": ["", "1"],
"size": 500000
}
}
}
}
【问题讨论】:
-
每个文档是否有唯一的“candidate_id”?您能否分享更多示例索引数据和预期的搜索结果?
-
badge的值应该是 1 或 2? -
可能有多个文档具有相同的candidate_id。徽章值应该是 1。我想分别计算 1 和 2,而不是组合计数
标签: elasticsearch elasticsearch-aggregation