T. Cem Yılmaz
T. Cem Yılmaz

Reputation: 510

Elasticsearch Term Aggregations Natural Ordering

I am making a product category page with Elasticsearch used for aggregations. As far as I know, ES can only do "_count", "_term" and by sub-aggregation for ordering the aggregation results with DSL.

My aggregation field is a string field and not analyzed but contains documents like "23 EU, 24 EU, 23 Toddler, etc.."

Here is my code snippet using elastic-builder package

     "aggs":{  
        "ProductFilters.Size":{  
           "terms":{  
              "field":"ProductFilters.Size",
              "size":52,
              "order":{  
                 "_term":"asc"
              }
           }
        },
        "ProductFilters.Size_count":{  
           "cardinality":{  
              "field":"ProductFilters.Size"
           }
        }
     }

however this code generates a result like this. I want this results generated like in the way a human being would like not 10 after 1 but 2.

Is natural sorting on aggregations are possible with ElasticSearch?

Upvotes: 0

Views: 512

Answers (1)

briarheart
briarheart

Reputation: 2006

To have a correct natural sorting you would need to pad numbers with zeros (provided you know the max value). For example assuming that the max value is 99 string "1 Youth" should become "01 Youth".

It is not possible to do it automatically without third-party plugins. Quick search in Google by words "elasticsearch natural sort" leads to this plugin but it does not seem very actively maintained.

Upvotes: 1

Related Questions