Nesting some other path in a subaggregation inside another nested path

Minimal schema

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

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

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

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