Nested aggregations with multiple nested fields

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

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.

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

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