示例:存储在索引中的文档表示有关每个测试的测试分数和元数据。
{ "test": 1, "user":1, "score":100, "meta":"other data" },
{ "test": 2, "user":2, "score":65, "meta":"other data" },
{ "test": 3, "user":2, "score":88, "meta":"other data" },
{ "test": 4, "user":1, "score":23, "meta":"other data" }
我需要能够过滤除最低测试分数以外的所有测试分数,并为每个测试者返回该测试的相关元数据。所以我的预期结果集是:
{ "test": 2, "user":2, "score":65, "meta":"other data" },
{ "test": 4, "user":1, "score":23, "meta":"other data" }
我现在看到这样做的唯一方法是首先通过用户使用嵌套的最小聚合进行术语聚合以获得最低分数。
POST user/tests/_search
{
"aggs" : {
"users" : {
"terms" : {
"field" : "user",
"order" : { "lowest_score" : "asc" }
},
"aggs" : {
"lowest_score" : { "min" : { "field" : "score" } }
}
}
},"size":0
}
然后,我必须获取该查询的结果并对EACH用户执行过滤查询,并对最低分数值进行过滤以获取其余元数据。育。
POST user/tests/_search
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{"term": { "user": {"value": "1" }}},
{"term": { "score": {"value": "22" }}}
]
}
}
}
}
}
我想知道是否有办法返回返回一个每个考试者的考试成绩最低的答案,并包含原始的_source文档。
解决方案?
以下为我提供了每个用户的最低分数文档,并按总体最低分数排序。并且,它包括原始文件。
GET user/tests/_search?search_type=count
{
"aggs": {
"users": {
"terms": {
"field": "user",
"order" : { "lowest_score" : "asc" }
},
"aggs": {
"lowest_score": { "min": { "field": "score" }},
"lowest_score_top_hits": {
"top_hits": {
"size":1,
"sort": [{"score": {"order": "asc"}}]
}
}
}
}
}
}
答案 0 :(得分:2)
也许你可以尝试使用热门命中聚合:
GET user/tests/_search?search_type=count
{
"aggs": {
"users": {
"terms": {
"field": "user",
"order": {
"_term": "asc"
}
},
"aggs": {
"lowest_score": {
"min": {
"field": "score"
}
},
"agg_top": {
"top_hits": {"size":1}
}
}
}
},
"size": 20
}