# Aggregations: combining children, nested and filter - filter not working properly

**URL:** <https://discuss.elastic.co/t/aggregations-combining-children-nested-and-filter-filter-not-working-properly/1383>\
**Category:** Elasticsearch\
**Created:** [May 27, 2015, 11:57am UTC](https://discuss.elastic.co/t/aggregations-combining-children-nested-and-filter-filter-not-working-properly/1383 "2015-05-27T11:57:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![crutch](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/crutch/32/414_2.png) [@crutch](https://discuss.elastic.co/u/crutch)\
**Post date:** [May 27, 2015, 11:57am UTC](https://discuss.elastic.co/t/aggregations-combining-children-nested-and-filter-filter-not-working-properly/1383/1 "2015-05-27T11:57:45Z")

</div>

Hello,

I am experiencing strange behavior when using aggregations combining `children`, `nested`, and `filter` aggs (with a `range` filter).

It looks like `children` aggregation is somehow affecting the `range` filter - it either returns count of all nested docs of a child document or none.

```
PUT /nestingtest/
{
  "mappings": {
    "teams": {
      properties: {
        name: {
          type: "string",
          index: "not_analyzed",
          doc_values: true
        }
      }
    },
    "tasks": {
        _parent: { type: "teams" },
      properties: {
        task_id: {
          type: "long",
          doc_values: true
        },
        started_at: {
          type: "object",
          properties: {
            raw: {
              type: "date",
              doc_values: true
            },
            year: {
              type: "short",
              doc_values: true
            },
            month: {
              type: "short",
              doc_values: true
            },
            day: {
              type: "short",
              doc_values: true
            },
            hour: {
              type: "short",
              doc_values: true
            },
            minute: {
              type: "short",
              doc_values: true
            },
            wday: {
              type: "short",
              doc_values: true
            }
          }
        },
        events: {
          type: "nested",
          properties: {
            id: {
              type: "long",
              doc_values: true
            },
            happened_at: {
              type: "object",
              properties: {
                raw: {
                  type: "date",
                  doc_values: true
                },
                year: {
                  type: "short",
                  doc_values: true
                },
                month: {
                  type: "short",
                  doc_values: true
                },
                day: {
                  type: "short",
                  doc_values: true
                },
                hour: {
                  type: "short",
                  doc_values: true
                },
                minute: {
                  type: "short",
                  doc_values: true
                },
                wday: {
                  type: "short",
                  doc_values: true
                }
              }
            }
          }
        }
      }
    }
  }
}

```

Data:

```
POST /nestingtest/teams/1
{ name: "team-A"}

POST /nestingtest/tasks/1?parent=1
{
  _parent: 1,
  task_id: 1,
  started_at: {
    raw: "2015-05-27T13:05",
    year: 2015,
    month: 5,
    day: 27,
    hour: 13,
    minute: 5,
    dow: 3
  },
  events: [
    {
      id: 1,
      happened_at: {
        raw: "2015-05-27T13:10",
        year: 2015,
        month: 5,
        day: 27,
        hour: 13,
        minute: 10,
        dow: 3
      }
    }
  ]
}

POST /nestingtest/tasks/2?parent=1
{
  _parent: 1,
  task_id: 2,
  started_at: {
    raw: "2015-05-27T16:05",
    year: 2015,
    month: 5,
    day: 27,
    hour: 16,
    minute: 5,
    dow: 3
  },
  events: [
    {
      id: 21,
      happened_at: {
        raw: "2015-05-27T16:10",
        year: 2015,
        month: 5,
        day: 27,
        hour: 16,
        minute: 10,
        dow: 3
      }
    },
    {
      id: 22,
      happened_at: {
        raw: "2015-05-27T17:10",
        year: 2015,
        month: 5,
        day: 27,
        hour: 17,
        minute: 10,
        dow: 3
      }
    }
  ]
}

```

Queries:  
This one, querying for parent documents is the one which behaves strangely:

```
POST /nestingtest/teams/_search?pretty
{
  aggs: {
    "to-tasks": {
      children: {
        type: "tasks"
      },
      aggs: {
        "to-events": {
          nested: {
            path: "tasks.events"
          },
          aggs: {
            filtered: {
              filter: {
                range: {
                  "events.happened_at.hour": {
                    gt: 15
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

```

This one queries directly children type (i.e., not using `children` aggregation) and is working as expected. However, I would prefer to query for parent type, as I want to combine other criteria into query.

```
 POST /nestingtest/tasks/_search?pretty
 {
  aggs: {
    "to-events": {
      nested: {
        path: "events"
      },
      aggs: {
        filtered: {
          filter: {
            range: {
              "events.happened_at.hour": {
                gt: 15
              }
            }
          }
        }
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 12:11am UTC](https://discuss.elastic.co/t/aggregations-combining-children-nested-and-filter-filter-not-working-properly/1383/2 "2017-07-06T00:11:40Z")

</div>


