'Elastic search collapse with nested object

I have an elastic search index

like

{
  "title": "A",
  "comments": [ 
    {
      "id":    "1"
    },
    {
       "id":    "2"
    }
  ]
},
{
  "title": "B",
  "comments": [ 
    {
      "id":    "1"
    },
    {
       "id":    "3"
    }
  ]
},
{
  "title": "C",
  "comments": [ 
    {
      "id":    "7"
    },
    {
       "id":    "3"
    }
  ]
}

I want to collapse is the group by the nested object. In the above JSON, I want to group it by Id. So the output will be like

hits:[{
  "title": "A",
  "comments": [ 
    {
      "id":    "1"
    },
    {
       "id":    "2"
    }
  ]
},
  inner_hits {[
    {
      "title": "A",
      "comments": [ 
        {
          "id":    "1"
        },
        {
          "id":    "2"
        }
      ]
    },
    {
      "title": "B",
      "comments": [ 
        {
          "id":    "1"
        },
        {
          "id":    "3"
        }
      ]
    }
  ]}
}]

Baiscally I need collapse bases on the nested object property.

Tried this

/_search?track_total_hits=true
{
    "collapse": {
        "field": "comments.id",
        "inner_hits": {
            "name": "id",
            "size": 10
        },
        "max_concurrent_group_searches": 3
    }
}

But its always returing first object only in the inner hits



Solution 1:[1]

Within the mapping of the object comments , you should remove the nested type.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jorge R