2

我需要根据日期(在此处添加字段)过滤一组值,然后按device_id. 所以我正在使用以下东西:

{
  "aggs":{
    "dates_between":{
      "filter": {
        "range" : {
          "added" : {
            "gte": "2014-07-01 00:00:00",
            "lte" :"2014-08-01 00:00:00"
          }
        }
      },
      "aggs": {
        "group_by_device_id": {
          "terms": {
            "field": "device_id"
          }
        }
      }
    }
  }
}

"Failed to parse source"这在执行查询时给了我一个错误。这是正确的做法吗?

如果我只执行日期聚合,它会显示不在指定日期范围内的值

4

1 回答 1

9

相反,它dates_between是一个嵌套聚合group_by_device_id

"aggs": {
    "group_by_device_id": {
        "terms": {
            "field": "device_id"
        },
        "aggs": {
            "dates_between": {
                "filter": {
                    "range": {
                        "added": {
                            "gte": "2014-07-01 00:00:00",
                            "lte": "2014-08-01 00:00:00"
                        }
                    }
                }
            }
        }
    }
}

您还可以将过滤器移动到查询中:

{
    "query": {
        "filtered": {
            "query": {
                "match_all": {}
            },
            "filter": {
                "range": {
                    "added": {
                        "gte": "2014-07-01 00:00:00",
                        "lte": "2014-08-01 00:00:00"
                    }
                }
            }
        }
    },
    "aggs": {
        "group_by_device_id": {
            "terms": {
                "field": "device_id"
            }
        }
    }
}
于 2014-09-17T12:09:46.113 回答