沙师弟,师父的充电器掉了

如何使用elasticsearch php 查詢當前index中,時間字段的最大值?

Elasticsearch | 作者 dave | 发布于2019年07月01日 | 阅读数:1734

我有一個gitlab_commit_index, 包含了30萬條document,其中每個document都有一個commit_date字段,我想使用elasticsearch php 查詢這個index中commit_date的最大值是多少,請教各位怎樣查,下面是我的錯誤寫法:
$params = [
'index' => 'gitlab_commit_index',
'type' => 'gitlab_commit_type',
'body' => [
'aggs' => [
'max_commit_date' => [
'max' => [
[
"field" => "commitDate"
],
],
],
]
],

];
報錯信息:
{"error":{"root_cause":[{"type":"parsing_exception","reason":"Expected [START_OBJECT] under [max], but got a [START_ARRAY] in [max_commit_date]","line":1,"col":35}],"type":"parsing_exception","reason":"Expected [START_OBJECT] under [max], but got a [START_ARRAY] in [max_commit_date]","line":1,"col":35},"status":400}
已邀请:

dave

赞同来自:

$params = [ 'index' => 'gitlab_commit_test_index', 'type' => 'gitlab_commit_test_type', 'body' => [ 'aggs' => [ 'max_commit_date' => [ 'max' => [ "field" => "committed_date" ], ], ] ], ];
找到答案

alvin - 90后IT男

赞同来自:

GET kibana_sample_data_ecommerce/_search
{
  "query": {
    "match_all": {}
  },
  "size": 0, 
  "aggs": {
    "max_time": {
      "max": {
        "field": "order_date"
      }
    }
  }
}
 
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4675,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "max_time" : {
      "value" : 1.563666336E12,
      "value_as_string" : "2019-07-20T23:45:36.000Z"
    }
  }
}

要回复问题请先登录注册