dingezzz
dingezzz

Reputation: 66

Order elasticsearch aggregation

I have multiple aggregations in my "query" see example. I use more aggregations sometimes. and I like to order the aggregations itself so "brand" is always on top of the aggregations list (in the result) and then "energy" for example?

If tried to figure it out myself but cant find anything that works.

{
    "size": 1000,
    "_source": ["id"],
    "query": { 
        "bool": { 
            "must": [
                { "match": { "category_default": "2" }}
            ],
            "must_not": [
                { "match": { "category_default": "2196" }},
                { "match": { "leverbaarheid_id": "3" }}
            ]
        }
    },  
    "aggs": {
        "brand": {
            "terms": {
                "field": "brand.keyword",
                "order": { "_term": "asc" }
            }
        },
        "min_price": { "min": { "field" : "selling_price" } },
        "max_price": { "max": { "field" : "selling_price" } },
        "energy": {
            "terms": {
                "field": "energy.keyword",
                "order": { "_term": "asc" }
            }
        }
    }
}

Upvotes: 1

Views: 96

Answers (1)

Nikolay Vasiliev
Nikolay Vasiliev

Reputation: 6066

I don't think this is directly possible.

I like to order the aggregations itself so "brand" is always on top of the aggregations list (in the result) and then "energy" for example?

Here you use name it "aggregations list" although it is actually a JSON object, which is the output format of Elasticsearch aggregations:

Each aggregation is associated with a logical name that the user defines ... . These logical names will also be used to uniquely identify the aggregations in the response.

Moreover, in a JSON object keys can be in any order, see for instance this question.

You would have to create a list from your aggregations with proper ordering on the client side.

Upvotes: 1

Related Questions