简体   繁体   English

弹性搜索中聚集的排序结果

[英]sorting results of aggregation in elasticsearch

I have written a query to get the latest records per Id from my ES. 我编写了一个查询,以从我的ES中获取每个ID的最新记录。 But the result of this query only does the inner sorting and chooses the latest records. 但是此查询的结果仅进行内部排序,并选择最新记录。 Now I need the results that I got to be sorted by date. 现在,我需要按日期对结果进行排序。

This is my query: 这是我的查询:

{
   "size":0,
   "query":{
      "bool":{
         "must":[
            {
               "match":{
                  "base":"XYZ"
               }
            },
            {
               "match":{
                  "Type":"low"
               }
            }
         ]
      }
   },
   "aggs":{
      "sources":{
         "terms":{
            "field":"Id"
         },
         "aggs":{
            "latest":{
               "top_hits":{
                  "size":1,

                  "_source":{
                     "includes":[
                        "base",
                        "Type"
                     ]
                  },
                  "sort":{
                     "orderDate":"desc"
                  }
               }
            }
         }
      }
   }
}

What you are trying to do is sorting the buckets by another another bucket. 您要尝试的是将各个存储桶按另一个存储桶进行排序。 You can achieve this in two ways: 您可以通过两种方式实现:

(a) By bucket_sort aggregation (a)通过bucket_sort聚合

(b) by using order param for terms aggregation referencing to another bucket. (b)通过使用order参数来引用其他存储桶的术语聚合。

(a) bucket_sort aggregation (a)bucket_sort聚合

This aggregation sorts the buckets of its parent multi-bucket aggregation. 此聚合对其父级多存储桶聚合的存储桶进行排序。 You can specify the field(s) based on which the buckets will be sorted. 您可以指定将对存储桶进行排序的字段。 Using this, the query for your case will be: 使用此查询,您的案例查询将是:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "base": "XYZ"
          }
        },
        {
          "match": {
            "Type": "low"
          }
        }
      ]
    }
  },
  "aggs": {
    "source": {
      "terms": {
        "field": "id"
      },
      "aggs": {
        "latest": {
          "top_hits": {
            "size": 1,
            "_source": {
              "includes": [
                "base",
                "Type"
              ]
            },
            "sort": {
              "orderDate": "desc"
            }
          }
        },
        "latestOrder": {
          "max": {
            "field": "orderDate"
          }
        },
        "bucket_sort_order": {
          "bucket_sort": {
            "sort": {
              "latestOrder": {
                "order": "desc"
              }
            }
          }
        }
      }
    }
  },
  "post_filter": {
    "term": {
      "status": "yes"
    }
  }
}

In the query above I have used a max aggregation named as latestOrder . 在上面的查询中,我使用了名为latestOrder的最大聚合。 This aggregation gives us the value for latest orderDate . 这种聚合为我们提供了最新orderDate的值。 If we look as the top hit aggregation the document returned by it will have the same orderDate as returned by the max aggeration ie latestOrder . 如果我们将orderDate视为热门latestOrder ,则其返回的文档将具有与最大缩放量(即latestOrder返回的orderDate相同的orderDate The reason being we have ordered top hit by orderDate in desc and limited the size to one, which is equivalent to max orderDate . 原因是我们在desc中按orderDate排序了热门orderDate ,并将大小限制为一个,这等于max orderDate

latestOrder works as a sorting field for us which is then used in the bucket_sort aggregation to sort the parent buckets which are the buckets returned by terms aggregation. latestOrder对我们来说是一个排序字段,然后在bucket_sort聚合中用于对父存储桶进行排序,这些父存储桶是术语聚合返回的存储桶。

(b) order param in terms agg (b) order参数agg

We use the similar approach as above. 我们使用与上述类似的方法。 We use max aggregation latestOrder and reference it in order param of terms aggregation. 我们使用最大聚合的latestOrder ,并在术语聚合的order参数中引用它。 So the query will be: 因此查询将是:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "base": "XYZ"
          }
        },
        {
          "match": {
            "Type": "low"
          }
        }
      ]
    }
  },
  "aggs": {
    "source": {
      "terms": {
        "field": "id",
        "order": {
          "latestOrder": "desc"
        }
      },
      "aggs": {
        "latest": {
          "top_hits": {
            "size": 1,
            "_source": {
              "includes": [
                "base",
                "Type",
                "orderDate"
              ]
            },
            "sort": {
              "orderDate": "desc"
            }
          }
        },
        "latestOrder": {
          "max": {
            "field":"orderDate"
          }
        }
      }
    }
  },
  "post_filter": {
    "term": {
      "status": "yes"
    }
  }
}

UPDATE in queries : Based on discussion in comments added post_filter. 查询中的UPDATE :基于注释中的讨论添加了post_filter。

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

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