使用 Elasticsearch 的字段最大值 Python API

Max value of feild using Elastic Search Python API

您好,我正在尝试使用 Elasticsearch Python API 从 Elasticsearch 中获取字段 "UID" 的最大值。当我使用下面的代码尝试它时,出现错误。

res = es.search(index="some_index", body={"query": {"size": 0,
"aggregations": {
"maxuid": {
  "max": {
    "field": "UID"
  }}}}})
print(res) 

elasticsearch.exceptions.RequestError: TransportError(400, u'parsing_exception', u'no [query] registered for [aggregations]')

我有一个用于相同请求的 curl 命令,但当我在正文中使用 Python API 的查询时,它会出现上述错误。

这是有效的 curl 请求,它给我预期的结果。如何在 Elasticsearch Python API 中使用它是我面临的问题。

GET some_index/some_doc/_search{
"size": 0,
"aggregations": {
"maxuid": {
"max": {
"field": "UID"
}}}}

非常感谢对此的任何帮助。谢谢。

像这样的东西会起作用:

res = es.search(
      index="some_index", 
       body={
        "aggs": {
          "maxuid": {
            "max": { "field": "UID"}
          }
       }
    }
)
print(res)

您不能在查询子句中指定聚合,聚合和查询是两个不同的子句,这就是您获得该异常的原因。

粗略的结构如下所示:

res = es.search(
       index="some_index", 
       body={
        "query": {},
        "size": 0,
        "aggs": {
          "maxuid": {
            "max": { "field": "UID"}
          }
        }
    }
)

我没有 运行 请求,因此您可能需要稍微调整一下。

PS:我不是 python 人 :)