# How to make aggregations work for text fields

**URL:** https://discuss.elastic.co/t/how-to-make-aggregations-work-for-text-fields/191984
**Category:** Elasticsearch
**Created:** [July 24, 2019, 8:37am UTC](https://discuss.elastic.co/t/how-to-make-aggregations-work-for-text-fields/191984 "2019-07-24T08:37:13Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Malik\_Khan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/malik_khan/32/50795_2.png) [@Malik\_Khan](https://discuss.elastic.co/u/Malik_Khan)
#### Post date: [July 24, 2019, 8:37am UTC](https://discuss.elastic.co/t/how-to-make-aggregations-work-for-text-fields/191984/1 "2019-07-24T08:37:13Z")

</div>

am trying to write a elasticsearch query to get unique locality towns. my locality\_town is of text type, when I used aggregations I came to know my field need to be of keyword type, so I tried to add a keyword field to locality\_town field. when I try to search into locality\_town.keyword, I get search hits but nothing in "aggregations":"Buckets".

Followig is how my schema looks like...

```
"locality_town": {
	"type": "text"
},			
"properties": {
	"properties": {
		"locality_town": {
			"properties": {
				"fielddata": {
					"type": "boolean"
				},
				"fields": {
					"properties": {
						"keyword": {
							"properties": {
								"ignore_above": {
									"type": "long"
								},
								"type": {
									"type": "text",
									"fields": {
										"keyword": {
											"type": "keyword",
											"ignore_above": 256
										}
									}
								}
							}
						}
					}
				},
				"type": {
					"type": "text",
					"fields": {
						"keyword": {
							"type": "keyword",
							"ignore_above": 256
						}
					}
				}
			}
		}
	}
}

```

My Search query looks like following

```
{ 
       "query": 
	{
		"prefix" : { "locality_town" : "m" }
	},
	"size": "1",
	"_source": {
		"includes": [
			"locality_town"
		]
	},
	"aggs": {
		"loc": {
			"terms": {
				"field": "locality_town.keyoword",
			    "size": 5,
				"order": {
					"_count": "desc"
				}
			}
		}
	}
}

```

Here is the output it gives

```
{
	"took": 19,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": 341641,
		"max_score": 1,
		"hits": [
			{
				"_index": "myindex",
				"_type": "myschema",
				"_id": "334398",
				"_score": 1,
				"_source": {
					"locality_town": "West Midlands"
				}
			}
		]
	},
	"aggregations": {
		"loc": {
			"doc_count_error_upper_bound": 0,
			"sum_other_doc_count": 0,
			"buckets": []
		}
	}
}
```

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [July 25, 2019, 6:06am UTC](https://discuss.elastic.co/t/how-to-make-aggregations-work-for-text-fields/191984/2 "2019-07-25T06:06:56Z")

</div>

Did you update/reindex after you changed the mapping?

---

<div class="post-metadata">

### Author: ![Malik\_Khan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/malik_khan/32/50795_2.png) [@Malik\_Khan](https://discuss.elastic.co/u/Malik_Khan)
#### Post date: [July 25, 2019, 9:54am UTC](https://discuss.elastic.co/t/how-to-make-aggregations-work-for-text-fields/191984/3 "2019-07-25T09:54:54Z")

</div>

Yes.

---

<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: [August 22, 2019, 10:03am UTC](https://discuss.elastic.co/t/how-to-make-aggregations-work-for-text-fields/191984/4 "2019-08-22T10:03:09Z")

</div>

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