ElasticSearch汇总按月分组的信息

时间:2016-07-27 20:08:49

标签: elasticsearch elasticsearch-aggregation

我正在开发一个ElasticSearch查询,该查询应该会返回所有日期字段现在为1年前的文档,然后按月分组(给我每个月的总计数),但是我没有写这个查询。

这就是我所拥有的:

{
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "account_id": [
              1
            ]
          }
        }
      ]
    }
  },
  "aggs": {
    "growth": {
      "date_range": {
        "field": "member_since",
        "format": "YYYY-MM-DD",
        "ranges": [
          {
            "to": "now-1Y/M"
          },
          {
            "from": "now-1Y/M"
          }
        ]
      }
    }
  },
  "size": 100
}

我正在运行查询:

POST https://my-es-cluster-url.com,但我一直收到此错误:

{
  "error": {
    "root_cause": [
      {
        "type": "parse_exception",
        "reason": "unit [Y] not supported for date math [-1Y/M]"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query_fetch",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "index0",
        "node": "MkypXlGdQamAplca1JIgZQ",
        "reason": {
          "type": "parse_exception",
          "reason": "unit [Y] not supported for date math [-1Y/M]"
        }
      }
    ]
  },
  "status": 400
}

1 个答案:

答案 0 :(得分:0)

我通过简单查询数据来重现您的问题。

"query": {
    "range": {
       "recent_buy_transaction": {
          "from": "now-1Y"
       }
    }
}

我得到了同样的错误。

  

ElasticSearchParseException [日期数学[-1Y]不支持unit [Y]]

但是,使用小的 y 可以解决问题。因此,您应该尝试使用:

  

现在-1Y / M

来自documentation

  

支持的时间单位为:y(年),M(月),w(星期),d(天),h(小时),m(分钟)和s(秒)。