简体   繁体   English

查询或过滤最小字段值?

[英]Query or Filter for minimum field value?

Example: a document stored in an index represents test scores and meta data about each test. 示例:存储在索引中的文档表示测试分数和有关每个测试的元数据。

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

I need to be able to filter out all but the lowest test score and return the associated metadata with that test for each test taker. 我需要能够过滤掉除最低分数以外的所有分数,并为每个应试者返回与该测试相关的元数据。 So my expected result set would be: 因此,我的预期结果将是:

{ "test": 2, "user":2, "score":65, "meta":"other data" },
{ "test": 4, "user":1, "score":23, "meta":"other data" }

The only way I see to do this now is by first doing a terms aggregation by user with a nested min aggregation to get their lowest score. 我现在看到的唯一方法是,首先通过用户使用嵌套的最小聚合来进行术语聚合,以获得最低的分数。

POST user/tests/_search
{
  "aggs" : {
    "users" : {
      "terms" : {
          "field" : "user",
          "order" : { "lowest_score" : "asc" }
      },
      "aggs" : {
        "lowest_score" : { "min" : { "field" : "score" } }
      }
    }
  },"size":0
}

Then I'd have to take the results of that query and do a filtered query for EACH user and filter on the lowest score value to grab the rest of the metadata. 然后,我必须获取该查询的结果,并对每个用户进行过滤查询,然后根据最低得分值进行过滤,以获取其余的元数据。 Yuk. 育。

POST user/tests/_search
{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {"term": { "user": {"value": "1" }}},
            {"term": { "score": {"value": "22" }}}
          ]
        }
      }
    }
  }
}

I'd like to know if there is a way to return return one response that has the lowest test score for each test taker and includes the original _source document. 我想知道是否有一种方法可以返回一个对每个应试者的考试成绩都最低并且包含原始_source文档的响应。

Solutions? 解决方案?

UPDATE - SOLVED 更新-解决

The following gives me the lowest score document for each user and is ordered by the overall lowest score. 以下是我给每个用户的最低分数文档,并按整体最低分数排序。 And, it includes the original document. 并且,它包括原始文档。

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

Maybe you could try this with top hits aggregation: 也许您可以结合热门匹配来尝试:

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM