Elastic search fill missing values in aggregation

I'm doing a sum aggregation in elasticsearch and in the query part I'm using a range filter. Is there anyway to fill missing data when the results come back? To make this more clear I'm going to share the code which is currently written in PHP.

{
    "index" => $indexName,
    "size" => 0,
    "body" => [
        "query" => [
            "bool" => [
                "filter" => [
                    "range" => [
                        "day_of_year" => [
                            "gte" => $from->dayOfYear,
                            "lte" => $to->dayOfYear
                        ]
                    ]
                ]
            ]
        ],
        "aggs" => [
            "spend_by_objective" => [
                "terms" => [
                    "field" => "objective",
                    "size" => 1000,
                ],
                "aggs" => [
                    "total_spend" => [
                        "sum" => ["field" => "spend"]
                    ],
                ]
            ]
        ]
    ]
}

And the result I'm receiving back is as following:

    {
            "spend_by_objective": {
                "doc_count_error_upper_bound": 0,
                "sum_other_doc_count": 0,
                "buckets": []

            }
    },

Is there any option where I can get the results to contain all the objectives possible in the documents but the aggregation to be shown as 0 if there are no hits.

I am still new to Elasticsearch so to begin with I'm not sure if this is doable, nor if this is ES's "job". The query above works as intended. I just wanted to know if I can achieve this on ES side or I need to manually map them in the logic part of my application.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.