# Nested aggregations with multiple nested fields

**URL:** <https://discuss.elastic.co/t/nested-aggregations-with-multiple-nested-fields/169244>\
**Category:** Elasticsearch\
**Created:** [February 20, 2019, 3:32pm UTC](https://discuss.elastic.co/t/nested-aggregations-with-multiple-nested-fields/169244 "2019-02-20T15:32:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sesha](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@sesha](https://discuss.elastic.co/u/sesha)\
**Post date:** [February 20, 2019, 3:32pm UTC](https://discuss.elastic.co/t/nested-aggregations-with-multiple-nested-fields/169244/1 "2019-02-20T15:32:44Z")

</div>

I have example data as below, But I need to aggregate teams and players with multiple nested fields ("foot-ball-teams", "cricket-teams").

I have to get output unique teams from "foot-ball-teams", "cricket-teams" and unique team's players of each team

```
PUT /sports/
{
  "mappings": {
    "_doc": {
      "properties": {
        "foot-ball-teams": {
          "type": "nested",
          "properties": {
            "name": {
              "type": "keyword"
            },
            "players": {
              "type": "keyword"
            }
          }
        },
        "cricket-teams": {
          "type": "nested",
          "properties": {
            "name": {
              "type": "keyword"
            },
            "players": {
              "type": "keyword"
            }
          }
        }
      }
    }
  }
}

POST /sports/_doc
{

    "foot-ball-teams": [
        { "name": "team1",
          "players": [
              "player1",
              "player2"
          ]  
        },
        { "name": "team2",
            "players": [
                "player1",
                "player3"
            ]
          }
    ],

    "cricket-teams": [
        { "name": "team1",
          "players": [
              "player1",
              "player2"
          ]  
        },
        { "name": "team2",
            "players": [
                "player1",
                "player4"
            ]
          }
    ]
}
```

---

<div class="post-metadata">

**Author:** ![RahulD](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@RahulD](https://discuss.elastic.co/u/RahulD)\
**Post date:** [February 21, 2019, 7:49pm UTC](https://discuss.elastic.co/t/nested-aggregations-with-multiple-nested-fields/169244/2 "2019-02-21T19:49:53Z")

</div>

Are you sure the mapping you provided is correct? The players field is defined as a keyword but you are indexing an array of values.

---

<div class="post-metadata">

**Author:** ![sesha](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@sesha](https://discuss.elastic.co/u/sesha)\
**Post date:** [February 24, 2019, 6:49am UTC](https://discuss.elastic.co/t/nested-aggregations-with-multiple-nested-fields/169244/3 "2019-02-24T06:49:19Z")

</div>

Yes its working fine, Array of words will be treated as keywords.

---

<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:** [March 24, 2019, 6:51am UTC](https://discuss.elastic.co/t/nested-aggregations-with-multiple-nested-fields/169244/4 "2019-03-24T06:51:11Z")

</div>

Closed on March 24, 2019, 6:51am UTC

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