How to make aggregations work for text fields

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": []
		}
	}
}

Did you update/reindex after you changed the mapping?

1 Like

Yes.

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