Creating a bucket with whole field not just terms

I am doing some aggregations and I am using a field named "city" to get buckets or groups, the problem was that for cities with 2 or more words i was getting a bucket per word. For example Los Angeles and Mexico City, I got 4 buckets. So as I did some research and I found this


I added that custom analyzer and it worked,
But the problem is that if I search now for Mexico i wont get any results, I have to have "mexico city". I can think of solution and it is 2 duplicate the field and have different analyzers I dont know if this is the correct approach or if there is another way.

Take a look at the fields parameter in the mapping: https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-fields.html

This will allow you to index one field in your json document in multiple ways so you can have it use the standard analyzer for searching and you custom aggregation analyzer for aggregations

Hope that helps

Thanks, exactly what I was looking for