示例:存储在索引中的文档表示测试分数和有关每个测试的元数据。
{ "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 }
然后,我必须获取该查询的结果,并对每个用户进行过滤查询,然后根据最低得分值进行过滤,以获取其余的元数据。Yu
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"}}] } } } } } }
也许您可以结合热门匹配来尝试:
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 }