---
"top_hits aggregation with nested documents":
  - skip:
          version: "5.99.99 - "
          reason: "5.x nodes don't include index or id in nested top hits"
  - do:
      indices.create:
        index: my-index
        body:
          settings:
            number_of_shards: 1
            number_of_replicas: 0
          mappings:
            doc:
              properties:
                users:
                  type: nested

  - do:
      index:
        index:  my-index
        type:   doc
        id:     1
        refresh: true
        body: |
          {
            "group" : "fans",
            "users" : [
              {
                "first" : "John",
                "last" :  "Smith"
              },
              {
                "first" : "Alice",
                "last" :  "White"
              }
            ]
          }

  - do:
      index:
        index:  my-index
        type:   doc
        id:     2
        refresh: true
        body: |
          {
            "group" : "fans",
            "users" : [
              {
                "first" : "Mark",
                "last" :  "Doe"
              }
            ]
          }

  - do:
      search:
        body:
          aggs:
            to-users:
              nested:
                path: users
              aggs:
                users:
                  top_hits:
                    sort: "users.last.keyword"

  - match: { hits.total: 2 }
  - length: { aggregations.to-users.users.hits.hits: 3 }
  - match: { aggregations.to-users.users.hits.hits.0._id: "2" }
  - match: { aggregations.to-users.users.hits.hits.0._index: my-index }
  - match: { aggregations.to-users.users.hits.hits.0._nested.field: users }
  - match: { aggregations.to-users.users.hits.hits.0._nested.offset: 0 }
  - match: { aggregations.to-users.users.hits.hits.1._id: "1" }
  - match: { aggregations.to-users.users.hits.hits.1._index: my-index }
  - match: { aggregations.to-users.users.hits.hits.1._nested.field: users }
  - match: { aggregations.to-users.users.hits.hits.1._nested.offset: 0 }
  - match: { aggregations.to-users.users.hits.hits.2._id: "1" }
  - match: { aggregations.to-users.users.hits.hits.2._index: my-index }
  - match: { aggregations.to-users.users.hits.hits.2._nested.field: users }
  - match: { aggregations.to-users.users.hits.hits.2._nested.offset: 1 }
