Filtered Terms aggregations in Array data types


(Kiran Sriharsha) #1

Hi

I am having a cities field in the index, which is of type array. Each document will have a city field with 1 or more values in the field.

Mapping
{
"mapping": {
"document": {
"properties": {
"multi_location": {
"properties": {
"city": {
"analyzer": "keyword_char_analyzer",
"type": "string",
"fields": {
"multi_location.city_sort": {
"index": "not_analyzed",
"type": "string"
}
}
}
}
}
}
}
}
}

Document Response:
{
"_index": "mydocuments",
"_type": "document",
"_score": 2.1978815,
"fields": {
"multi_location.city": [
"Denver",
"Austin",
"Dallas",
"Houston"
]
}
}

Aggregation Query:
{
"aggregations": {
"city": {
"terms": {
"field": "multi_location.city",
"size": 0,
"include": "n.*"
}
},
"city_sort": {
"terms": {
"field": "multi_location.city.multi_location.city_sort",
"size": 0
}
}
}
}

Requirement
Get documents count for the query prefix match "N". (Documents in city New York, Nashville, etc). But this is not straight forward in above query as city aggregation is on non_analyzed field which gives the response "New": and then this should be validated in city_sort aggregation to get actual city name "new york". I need guidance on applying include field in terms aggregation of non analysed field.

Thanks
Sri Harsha


(system) #2