# Merge Documents based on field value?

**URL:** <https://discuss.elastic.co/t/merge-documents-based-on-field-value/98579>\
**Category:** Elasticsearch\
**Created:** [August 28, 2017, 3:30pm UTC](https://discuss.elastic.co/t/merge-documents-based-on-field-value/98579 "2017-08-28T15:30:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![noah](https://avatars.discourse-cdn.com/v4/letter/n/a87d85/32.png) [@noah](https://discuss.elastic.co/u/noah)\
**Post date:** [August 28, 2017, 3:30pm UTC](https://discuss.elastic.co/t/merge-documents-based-on-field-value/98579/1 "2017-08-28T15:30:31Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Mike.Barretta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mike.barretta/32/16688_2.png) [@Mike.Barretta](https://discuss.elastic.co/u/Mike.Barretta)\
**Post date:** [August 28, 2017, 11:24pm UTC](https://discuss.elastic.co/t/merge-documents-based-on-field-value/98579/2 "2017-08-28T23:24:42Z")

</div>

@noah you can't get exactly that response, but you can do an aggregation with [top\_hits](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html) 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`)

```auto
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:

```auto
"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"
                 }
               }
             }
...
}

```

---

<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:** [September 25, 2017, 11:24pm UTC](https://discuss.elastic.co/t/merge-documents-based-on-field-value/98579/3 "2017-09-25T23:24:47Z")

</div>

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