Merge Documents based on field value?

I have multiple Documents within an Index, each have the following fields:

id serviceName Type
Now, stupidly, id is not unique and I want to change that. I want to use Kibana/Elasticsearch to query the data so that I have id unique and the behaviour I want is that if I have the following Docs:

id    serviceName    Type
1     A              T1
1     B              T2
1     D              T2

I use a query so that I get this result

1 A,B,D T1,T2,T2
Is there a way for this?

@noah you can't get exactly that response, but you can do an aggregation with top_hits to group all the results together where id==1.

The following will display the top 100 results for each id "group", but only in instances where there are at least 2 in the group (min_doc_count: 2)

GET my_index/_search?size=0
{
  "size": 0,  
  "aggs": {
    "by_id": {
      "terms": {
        "field": "id",
        "min_doc_count": 2
      },
      "aggs": {
        "same_ids": {
          "top_hits": {
            "size": 100
          }
        }
      }
    }
  }
}

Result would be something like:

"aggregations": {
    "by_id": {
      "buckets": [
        {
          "doc_count": 3,
          "same_ids": {
            "hits": {
              "total": 3,
              "hits": [
               {
                 "_source": {
                   "serviceName": "A",
                   "type": "T1"
                 }
               },
               {
                 "_source": {
                   "serviceName": "B",
                   "type": "T2"
                 }
               },
               {
                 "_source": {
                   "serviceName": "C",
                   "type": "T3"
                 }
               }
             }
...
}
1 Like

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