Elasticsearch terms aggregation on a not analyzed field with filters

Elasticsearch terms aggregation on a not analyzed field with filters

I have a not analyzed field on my index:
“city”: { “type”: “string”, “index”: “not_analyzed” }

I have an aggregation like the following:
“aggs”: {
“city”: {
“terms”: {
“field”: “city”
}
}
}

that gives me an output like this:
“aggregations”: {
“city”: {
“doc_count_error_upper_bound”: 51,
“sum_other_doc_count”: 12478,
“buckets”: [
{
“key”: “New York”,
“doc_count”: 28420
},
{
“key”: “London”,
“doc_count”: 23456
},
{
“key”: “São Paulo”,
“doc_count”: 12727
}
]
}
}

I need to add a match_phrase_prefix query before processing the aggregation to filter my results based on a user text, like this:
{
“size”: 0,
“query”: {
“match_phrase_prefix”: {
“city”: “sao”
}
},
“aggs”: {
“city”: {
“terms”: {
“field”: “city”
}
}
}
}

and the result is… nothing!
“aggregations”: {
“city”: {
“doc_count_error_upper_bound”: 0,
“sum_other_doc_count”: 0,
“buckets”: []
}
}

I was expecting an aggregation result on São Paulo city. Obviously the problem is that my field should have lowercase and asciifolding filters to have a match (São/sao), but I can’t make my field analyzed because I don’t want to have aggregation results like São, Paulo, New, York (that’s what happens on analyzed fields).
What can I do? I tried a lot of combinations with mapping/query/aggs but I can’t get it to work.
Any help will be appreciated.

Solutions/Answers:

Solution 1:

Since it is not_analyzed the query terms are case-sensitive.
You could use multi-field mapping on city with analyzed and non-analyzed fields.

Example:

put <index>/<type>/_mapping
{
   "properties": {
      "city": {
         "type": "string",
         "fields": {
            "raw": {
               "type": "string",
               "index": "not_analyzed"
            }
         }
      }
   }
}

post <index>/<type>/_search
{
    "size": 0,
    "query": {
        "match_phrase_prefix": {
            "city": "Sao"
        }
    },
    "aggs": {
        "city": {
                "terms": {
                    "field": "city.raw"
                }
            }
    }
}

References

Related:  Full text search options for MongoDB setup