2014-12-11 57 views
0

示例:存儲在索引中的文檔表示關於每個測試的測試分數和元數據。查詢或篩選最小字段值?

{ "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 
} 

然後,我不得不採取查詢的結果,併爲每個用戶做一個過濾查詢和最低得分值過濾搶到元數據的其餘部分。育。

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"}}] 
      } 
     } 
     } 
    } 
    } 
} 

回答

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 
} 
+0

完美。我錯過了top_hits的價值!我發現這在尋找最終修復方面很有用。我將用解決方案更新上面的帖子。謝謝! – hubbardr 2014-12-11 21:35:20