2

I need to filter a group of values based on date (added field here) and then group it by device_id. So I am using the following thing :

{
  "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"
          }
        }
      }
    }
  }
}

This is giving me an error "Failed to parse source" when executing the query. This is the right way of doing it ?

If I execute the date aggregation only it is showing values which are not in the specified date range

1 Answer 1

10

It is the other way around dates_between is a nested aggregation of 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"
                        }
                    }
                }
            }
        }
    }
}

You could also move the filter into the the query:

{
    "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"
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.