【问题标题】:Elasticsearch aggregation by max date giving wrong results最大日期的 Elasticsearch 聚合给出错误的结果
【发布时间】:2021-02-05 07:45:05
【问题描述】:

我想按sysCode 和最大date 分组,即获取每个sysCode 的最新文档,然后在employeeId 上聚合并键入字段。 下面的查询没有返回我想要的结果,对于GER,它返回employeeId=1,对于IND,它返回我不想要的employeeId=3

示例 json 文档

{
  "sysCode": "GER",
  "employeeId": 1,
  "date": "2014-06-14",
  "categories": {
    "pb": [
      {
        "metric": "OVERDUE",
        "type": "LATE"
      }
    ]
  }
}
{
  "sysCode": "GER",
  "employeeId": 2,
  "date": "2014-06-15",
  "categories": {
    "pb": [
      {
        "metric": "OVERDUE",
        "type": "LATE"
      }
    ]
  }
}
{
  "sysCode": "IND",
  "employeeId": 3,
  "date": "2014-06-16",
  "categories": {
    "pb": [
      {
        "metric": "OVERDUE",
        "type": "LATE"
      }
    ]
  }
}
{
  "sysCode": "IND",
  "employeeId": 3,
  "date": "2014-06-16",
  "categories": {
    "pb": [
      {
        "metric": "OVERDUE",
        "type": "MISSED"
      }
    ]
  }
}

聚合查询

{
  "aggs": {
    "result_by_sys_code": {
      "terms": {
        "field": "sysCode"
      },
      "aggs": {
        "max_as_of_date": {
          "max": {
            "field": "date"
          }
        },
        "employees": {
          "terms": {
            "field": "employeeId"
          },
          "aggs": {
            "nested": {
              "nested": {
                "path": "categories.pb"
              },
              "aggs": {
                "metrics": {
                  "terms": {
                    "field": "categories.pb.type.keyword"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

映射

{
  "mappings": {
    "properties": {
      "date": {
        "type": "date"
      },
      "categories": {
        "properties": {
          "pb": {
            "type": "nested",
            "properties": {
              "metric": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "type": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              }
            }
          }
        }
      },
      "controlCode": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  }
}

【问题讨论】:

  • 你能在这里添加你的映射吗
  • 添加了字段映射
  • 由于您的日期格式为yyyy-mm-dd,如果多个文档具有相同的日期,您打算怎么做(恕我直言,这种情况经常发生)?

标签: elasticsearch elasticsearch-aggregation


【解决方案1】:

我认为使用top_hits,您可以满足您的要求。 来自官方文档:

此聚合器旨在用作子聚合器,以便可以按桶聚合最匹配的文档。

curl -X POST "localhost:9200/sales/_search?size=0&pretty" -H 'Content-Type: application/json' -d'
{
  "aggs": {
    "top_tags": {
      "terms": {
        "field": "type",
        "size": 3
      },
      "aggs": {
        "top_sales_hits": {
          "top_hits": {
            "sort": [
              {
                "date": {
                  "order": "desc"
                }
              }
            ],
            "_source": {
              "includes": [ "date", "price" ]
            },
            "size": 1
          }
        }
      }
    }
  }
}
'

我们按类型对销售进行分组,并按类型显示最后一次销售。对于每次销售,源中仅包含日期和价格字段。

另外,Sampler Aggregations 做类似的事情,但以不同的方式。

一种过滤聚合,用于将任何子聚合的处理限制为得分最高的文档样本。

可以参考Limit ElasticSearch aggregation to top n query results 获取 TL;Sampler 上的 DR。

【讨论】:

  • 在我的情况下,我需要在最热门的地方进行子聚合,这可能吗?
  • 原谅?我不明白
猜你喜欢
  • 1970-01-01
  • 2015-02-11
  • 2016-07-16
  • 2014-08-04
  • 2022-01-17
  • 2017-12-28
  • 1970-01-01
  • 1970-01-01
  • 2021-01-15
相关资源
最近更新 更多