I am trying to sort the buckets of a terms aggregation in elasticsearch case-insensitive. Here is the field mapping:
'brandName' => [
'type' => 'string',
'analyzer' => 'english',
'index' => 'analyzed',
'fields' => [
'raw' => [
'type' => 'string',
'index' => 'not_analyzed'
]
]
]
Note that this data structure here is for PHP.
And the aggregation looks like this:
aggregations => [
'brands' => [
'terms' => [
'field' => 'brandName.raw',
'size' => 0,
'order' => ['_term' => 'asc']
]
]
]
This works, but the resulting buckets are in lexicographical order.
I found some interesting docs here that explained how to do this, but it is in the context of sorting the hits, not the aggregations buckets.
I tried it anyway. Here is the analyzer I created:
'analysis' => [
'analyzer' => [
'case_insensitive_sort' => [
'tokenizer' => 'keyword',
'filter' => [ 'lowercase' ]
]
]
]
And here is the updated field mapping, with a new sub-field called "sort" using the analyzer.
'brandName' => [
'type' => 'string',
'analyzer' => 'english',
'index' => 'analyzed',
'fields' => [
'raw' => [
'type' => 'string',
'index' => 'not_analyzed'
],
'sort' => [
'type' => 'string',
'index' => 'not_analyzed',
'analyzer' => 'case_insensitive_sort'
]
]
]
And here's the updated aggregation portion of my query:
aggregations => [
'brands' => [
'terms' => [
'field' => 'brandName.raw',
'size' => 0,
'order' => ['brandName.sort' => 'asc']
]
]
]
This generates the following error: Invalid term-aggregator order path [brandName.sort]. Unknown aggregation [brandName].
Am I close? Can this kind of aggregation bucket sorting be done?