# How to combine multiple fields values within single field

**URL:** <https://discuss.elastic.co/t/how-to-combine-multiple-fields-values-within-single-field/208826>\
**Category:** Elasticsearch\
**Created:** [November 21, 2019, 8:10am UTC](https://discuss.elastic.co/t/how-to-combine-multiple-fields-values-within-single-field/208826 "2019-11-21T08:10:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rameshkr1994](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rameshkr1994/32/59029_2.png) [@rameshkr1994](https://discuss.elastic.co/u/rameshkr1994)\
**Post date:** [November 21, 2019, 8:10am UTC](https://discuss.elastic.co/t/how-to-combine-multiple-fields-values-within-single-field/208826/1 "2019-11-21T08:10:51Z")

</div>

Dear All.

again i came here from long time with some issue.

`i have data inside elastic search like below:-`

id name cnt marks  
101 ram ind 80.32  
101 ram ...... null  
101 ram uk 78.3  
101 ram us null

`now when i search for "name":"ram" it give below output :- `  
id name cnt marks  
101 ram ind 80.32  
101 ram ....... null  
101 ram uk 78.3  
101 ram us null  
id name cnt marks  
`but i need output like below :`

id name cnt marks  
101 ram ind 80.32  
..... ....... ........ null  
...... .......... uk 78.3  
...... ......... us null

`final : we want to stop duplicate records like id,name`  
`please help us:-`  
...... is nothing here!

Thanks  
HadoopHelp

---

<div class="post-metadata">

**Author:** ![somnath.guthula](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@somnath.guthula](https://discuss.elastic.co/u/somnath.guthula)\
**Post date:** [November 21, 2019, 2:12pm UTC](https://discuss.elastic.co/t/how-to-combine-multiple-fields-values-within-single-field/208826/2 "2019-11-21T14:12:54Z")

</div>

Hi @rameshkr1994,

I believe what you are looking for is aggregations. I am not sure, but you should explore Terms Aggregation.

Link for reference :  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html)

An Example Query :

```
GET <index_name>/_search?_source=false
{
  "query": {
    "bool": {
      "filter": [{
          "term": {
            "name": "ram"
          }
        }]
    }
  },
  "aggs": {
    "group_by_id": {
      "terms": {
        "field": "id",
        "size": 1000
      },
      "aggs": {
        "top_hits": {
          "top_hits": {
            "size": 100,
            "_source": ["cnt", "marks"]
          }
        }
      }
    }
  }
}

```

The above is just a adhoc query. You may need to modify or tweak it a little.

Hope that helps. 🙂

Regards,  
Somnath

---

<div class="post-metadata">

**Author:** ![rameshkr1994](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rameshkr1994/32/59029_2.png) [@rameshkr1994](https://discuss.elastic.co/u/rameshkr1994)\
**Post date:** [November 21, 2019, 2:34pm UTC](https://discuss.elastic.co/t/how-to-combine-multiple-fields-values-within-single-field/208826/3 "2019-11-21T14:34:57Z")

</div>

Hi @somnath.guthula.

thanks for replying !!!

i written this query as per my requirement but not success-ed :

URL : [http://LOCALHOST:9200/healthecdemo/\_search?\_source=false](http://LOCALHOST:9200/healthecdemo/_search?_source=false)

Query 👎

> {  
> "query": {  
> "bool": {  
> "filter": [{  
> "term": {  
> "firstname": "Firstname1"  
> }  
> }]  
> }  
> },  
> "aggs": {  
> "group\_by\_id": {  
> "terms": {  
> "field": "healthrecordkey",  
> "size": 100  
> },  
> "aggs": {  
> "top\_hits": {  
> "top\_hits": {  
> "size": 100,  
> "\_source": ["sex", "drugname"]  
> }  
> }  
> }  
> }  
> }  
> }

response :-(ERROR)

> {  
> "took": 1,  
> "timed\_out": false,  
> "\_shards": {  
> "total": 1,  
> "successful": 1,  
> "skipped": 0,  
> "failed": 0  
> },  
> "hits": {  
> "total": {  
> "value": 0,  
> "relation": "eq"  
> },  
> "max\_score": null,  
> "hits":   
> },  
> "aggregations": {  
> "group\_by\_id": {  
> "doc\_count\_error\_upper\_bound": 0,  
> "sum\_other\_doc\_count": 0,  
> "buckets":   
> }  
> }  
> }

still not getting this solution!!

`as per your gouide i tried ?filter with term is not working here !! if change here filter and term with must and match then result is fetching but previous output is coming ?`

Thanks  
HadoopHelp

---

<div class="post-metadata">

**Author:** ![somnath.guthula](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@somnath.guthula](https://discuss.elastic.co/u/somnath.guthula)\
**Post date:** [November 21, 2019, 3:26pm UTC](https://discuss.elastic.co/t/how-to-combine-multiple-fields-values-within-single-field/208826/4 "2019-11-21T15:26:18Z")

</div>

As mentioned earlier, The query would require some tweaking before using it. I am assuming you are using the right keys.

Someone should help out in regards to this. My Apologies.

---

<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:** [December 19, 2019, 3:26pm UTC](https://discuss.elastic.co/t/how-to-combine-multiple-fields-values-within-single-field/208826/5 "2019-12-19T15:26:24Z")

</div>

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