查询或筛选最小字段值? [英] Query or Filter for minimum field value?

查看:87
本文介绍了查询或筛选最小字段值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:存储在索引中的文档表示每个测试的测试分数和元数据。

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
}

然后我必须采取该查询的结果并进行过滤查询每个用户并筛选最低分值以获取其余的元数据。 Yuk。

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

我想知道是否有是一种返回一个响应的方法,每个测试者具有最低测试分数,并包含原始的_source文档。

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.

解决方案?

以下给出了每个用户的最低分数文档,并按整体最低分数排序。而且,它包括原始文件。

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
}

这篇关于查询或筛选最小字段值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆