# Values are not showing correctly when used aggs sort

**URL:** https://discuss.elastic.co/t/values-are-not-showing-correctly-when-used-aggs-sort/221807
**Category:** Elasticsearch
**Created:** [March 3, 2020, 5:12am UTC](https://discuss.elastic.co/t/values-are-not-showing-correctly-when-used-aggs-sort/221807 "2020-03-03T05:12:52Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![pyerunka](https://avatars.discourse-cdn.com/v4/letter/p/d6d6ee/32.png) [@pyerunka](https://discuss.elastic.co/u/pyerunka)
#### Post date: [March 3, 2020, 5:12am UTC](https://discuss.elastic.co/t/values-are-not-showing-correctly-when-used-aggs-sort/221807/1 "2020-03-03T05:12:53Z")

</div>

Hello,

I have one index named master\_db, in that i have fetched one column from db named content\_type.  
I am using aggs to sort values of content \_type as below:

```
GET /master_db/_search?size=0
{
	"aggs":{
		"group_by":{
			"terms":{
				"field":"content_type",
				"order":{
					"_count":"asc"
				},
				"size":25
			}
		}
	}
} 

```

And i am getting results like:

> "aggregations" : {  
> "group\_by" : {  
> "doc\_count\_error\_upper\_bound" : 0,  
> "sum\_other\_doc\_count" : 0,  
> "buckets" : [  
> {  
> "key" : "ticket",  
> "doc\_count" : 860764  
> },  
> {  
> "key" : "documentation",  
> "doc\_count" : 62753  
> },  
> {  
> "key" : "solution",  
> "doc\_count" : 27574  
> },  
> {  
> "key" : "best",  
> "doc\_count" : 23401  
> },  
> {  
> "key" : "practice",  
> "doc\_count" : 23401  
> },  
> {  
> "key" : "and",  
> "doc\_count" : 15306  
> },  
> {  
> "key" : "case",  
> "doc\_count" : 15306  
> },  
> {  
> "key" : "history",  
> "doc\_count" : 15306  
> },  
> {  
> "key" : "learned",  
> "doc\_count" : 15306  
> },  
> {  
> "key" : "lessons",  
> "doc\_count" : 15306  
> },  
> {  
> "key" : "report",  
> "doc\_count" : 9452  
> },  
> {  
> "key" : "training",  
> "doc\_count" : 6915  
> },  
> {  
> "key" : "announcement",  
> "doc\_count" : 4925  
> },  
> {  
> "key" : "page",  
> "doc\_count" : 4401  
> },  
> {  
> "key" : "reference",  
> "doc\_count" : 4401  
> },  
> {  
> "key" : "link",  
> "doc\_count" : 2182  
> },  
> {  
> "key" : "alert",  
> "doc\_count" : 1801  
> },  
> {  
> "key" : "technical",  
> "doc\_count" : 1801  
> }  
> ]  
> }  
> }  
> }

In above result, we can see reference and page are two different words but it should be one single word with space like reference page. similar to this there are other cases also in the above result.  
I want this to print as 1 word and show count as well for the same.  
In index also this value is stored as a one word only.  
How i can achieve this using aggs sort?

Regards,  
Priyanka

---

<div class="post-metadata">

### Author: ![pyerunka](https://avatars.discourse-cdn.com/v4/letter/p/d6d6ee/32.png) [@pyerunka](https://discuss.elastic.co/u/pyerunka)
#### Post date: [March 12, 2020, 3:31am UTC](https://discuss.elastic.co/t/values-are-not-showing-correctly-when-used-aggs-sort/221807/2 "2020-03-12T03:31:08Z")

</div>

Hello,

Can anyone help me in this?

Regards,  
Priyanka

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [March 12, 2020, 10:33am UTC](https://discuss.elastic.co/t/values-are-not-showing-correctly-when-used-aggs-sort/221807/3 "2020-03-12T10:33:03Z")

</div>

Do not aggregate on fields that are searchable. Only aggregate on keyword fields, which will not show a two term value in two different buckets, but treat it as one.

---

<div class="post-metadata">

### Author: ![pyerunka](https://avatars.discourse-cdn.com/v4/letter/p/d6d6ee/32.png) [@pyerunka](https://discuss.elastic.co/u/pyerunka)
#### Post date: [March 13, 2020, 3:35am UTC](https://discuss.elastic.co/t/values-are-not-showing-correctly-when-used-aggs-sort/221807/4 "2020-03-13T03:35:02Z")

</div>

Hello @spinscale,

Thanks for your reply!!  
I have tried with above solution. My code is like:

```
GET /master_db/_search
{
"size": 50,
"aggs": {
"content_type": {
"terms": {
"field": "content_type.keyword"
},
"aggs": {
"content_subtype": {
"terms": {
"field": "content_subtype.keyword"
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
}
} 

```

In above code content type and content\_subtype are two different fields.  
I am getting output like:

```
"buckets" : [
        {
          "key" : "Ticket",
          "doc_count" : 860764,
          "content_subtype" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : []
          }
        },
        {
          "key" : "Documentation",
          "doc_count" : 62753,
          "content_subtype" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 8542,
            "buckets" : [
              {
                "key" : "Procedure",
                "doc_count" : 20184,
                "average_balance" : {
                  "value" : null
                }
              }

```

under content type, I am getting values for content sub\_type.  
I do not want my output like this.  
I want first content type values should be displayed and after that all the values for content sub\_type.

Regards,  
Priyanka

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [March 13, 2020, 8:36am UTC](https://discuss.elastic.co/t/values-are-not-showing-correctly-when-used-aggs-sort/221807/5 "2020-03-13T08:36:56Z")

</div>

Hey,

then have two separate aggregations, one for each field, without nesting the one into the other.

--Alex

---

<div class="post-metadata">

### Author: ![pyerunka](https://avatars.discourse-cdn.com/v4/letter/p/d6d6ee/32.png) [@pyerunka](https://discuss.elastic.co/u/pyerunka)
#### Post date: [March 16, 2020, 5:55am UTC](https://discuss.elastic.co/t/values-are-not-showing-correctly-when-used-aggs-sort/221807/6 "2020-03-16T05:55:58Z")

</div>

Hello @spinscale,

Thanks for your reply!!!  
I have created two separate aggregations, one for each field.  
Now i want to search in that using match all. I want to use query in aggs.  
My query should match in all the specified fields and depending on that aggregation count should be displayed.  
How i can achieve that?

Regards,  
Priyanka

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [March 16, 2020, 1:44pm UTC](https://discuss.elastic.co/t/values-are-not-showing-correctly-when-used-aggs-sort/221807/7 "2020-03-16T13:44:18Z")

</div>

hey,

you cannot search in an aggregation result, you can however use a [filter aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/7.6/search-aggregations-bucket-filter-aggregation.html) to reduce the number of results that get aggregated on. The `terms` aggregation also allows for filtering values, but that is specific to this aggregation. See [https://www.elastic.co/guide/en/elasticsearch/reference/7.6/search-aggregations-bucket-terms-aggregation.html#\_filtering\_values\_4](https://www.elastic.co/guide/en/elasticsearch/reference/7.6/search-aggregations-bucket-terms-aggregation.html#_filtering_values_4)

--Alex

---

<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: [April 13, 2020, 1:44pm UTC](https://discuss.elastic.co/t/values-are-not-showing-correctly-when-used-aggs-sort/221807/8 "2020-04-13T13:44:20Z")

</div>

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