# Nesting some other path in a subaggregation inside another nested path

**URL:** <https://discuss.elastic.co/t/nesting-some-other-path-in-a-subaggregation-inside-another-nested-path/276883>\
**Category:** Elasticsearch\
**Created:** [June 24, 2021, 6:54am UTC](https://discuss.elastic.co/t/nesting-some-other-path-in-a-subaggregation-inside-another-nested-path/276883 "2021-06-24T06:54:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![broy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/broy/32/90733_2.png) [@broy](https://discuss.elastic.co/u/broy)\
**Post date:** [June 24, 2021, 6:54am UTC](https://discuss.elastic.co/t/nesting-some-other-path-in-a-subaggregation-inside-another-nested-path/276883/1 "2021-06-24T06:54:14Z")

</div>

**Minimal schema**

```auto
{
  "mappings": {
    "_doc": {
      "dynamic": "false",
      "_source": {
        "includes": [
          
        ]
      },
      "properties": {
        "__collection": {
          "type": "keyword"
        },
        "relations": {
          "properties": {
            "persons": {
              "type": "nested",
              "properties": {
                "identifier": {
                  "type": "keyword"
                },
                "name": {
                  "type": "text",
                  "fields": {
                    "raw": {
                      "type": "keyword"
                    }
                  }
                }
              }
            },
            "tags": {
              "type": "nested",
              "properties": {
                "name": {
                  "type": "text",
                  "fields": {
                    "raw": {
                      "type": "keyword"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

```

**Query**  
GET feats/\_search

```auto
{
  "size": 0,
  "_source": false,
  "query": {
    "bool": { 
      "filter": [
        {"term": { "__collection": "projects" }}
      ]
    }
  },
  "aggs": {
    "projectCount" : {
      "nested": {
        "path": "relations.tags"
      },
      "aggs": {
        "tot_count": {
          "terms": {
            "field": "relations.tags.name.raw",
            "size": 15,
            "include": ["#Advertising", "#VirtualReality", "#BrandIdentity"],
            "order": {"_count":"desc"}
          },
          "aggs": {
            "totCount": {
            "reverse_nested": {
              "path": "relations.persons"
            },
            "aggs": {
              "peopleCount": {
                "cardinality": {
                  "field": "relations.persons.identifier"
                }
              }
            }
           }
          }
        }
      }
    }
  }
}

```

**Result:**

```auto
{
  "took" : 18,
  "timed_out" : false,
  ...
  "aggregations" : {
    "projectCount" : {
      "doc_count" : 11713,
      "tot_count" : {
        "doc_count_error_upper_bound" : 0,
        "sum_other_doc_count" : 0,
        "buckets" : [
          {
            "key" : "#Design",
            "doc_count" : 771,
            "totCount" : {
              "doc_count" : 280,
              "peopleCount" : {
                "value" : 175
              }
            }
          },
          {
            "key" : "#Advertising",
            "doc_count" : 656,
            "totCount" : {
              "doc_count" : 423,
              "peopleCount" : {
                "value" : 223
              }
            }
          }
        ]
      }
    }
  }
}

```

I think `peopleCount` sub aggregation isn't correct. What shall be the correct query. Also there could be optimizations

---

<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 22, 2021, 6:54am UTC](https://discuss.elastic.co/t/nesting-some-other-path-in-a-subaggregation-inside-another-nested-path/276883/2 "2021-07-22T06:54:40Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
