# Help with sum and max query

**URL:** <https://discuss.elastic.co/t/help-with-sum-and-max-query/102941>\
**Category:** Elasticsearch\
**Created:** [October 6, 2017, 4:59am UTC](https://discuss.elastic.co/t/help-with-sum-and-max-query/102941 "2017-10-06T04:59:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![truongdqse03303](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/truongdqse03303/32/22605_2.png) [@truongdqse03303](https://discuss.elastic.co/u/truongdqse03303)\
**Post date:** [October 6, 2017, 4:59am UTC](https://discuss.elastic.co/t/help-with-sum-and-max-query/102941/1 "2017-10-06T04:59:03Z")

</div>

Hi experts. I having problem when i want to sum the max value field.  
My query to take the max value field

```
{
  "size":0,
  "query": {
   "bool":
         {
             "must": [ 
              {"term": {"zone_id" : "1"}}
             ]
         }
},
  
   "aggs": {
  "content_level": {
     "terms": {
        "field": "content_level"
     },
     "aggs":{
     "news_id":{
       "terms": {
       "field": "news_id"
       },
       "aggs": {
       "viewed_count_max": {
        "max": {
         "field":"viewed_count"
        }
       }
       }
     }
     }
  }
   }
}

```

and the result

```
   {
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 4,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "content_level": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "A",
                    "doc_count": 3,
                    "news_id": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                            {
                                "key": "1111",
                                "doc_count": 2,
                                "viewed_count_max": {
                                    "value": 1100
                                }
                            },
                            {
                                "key": "3333",
                                "doc_count": 1,
                                "viewed_count_max": {
                                    "value": 2000
                                }
                            }
                        ]
                    }
                },
                {
                    "key": "B",
                    "doc_count": 1,
                    "news_id": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                            {
                                "key": "2222",
                                "doc_count": 1,
                                "viewed_count_max": {
                                    "value": 666
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

```

anybody can help me sum this viewed\_count\_max.  
Thanks

---

<div class="post-metadata">

**Author:** ![truongdqse03303](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/truongdqse03303/32/22605_2.png) [@truongdqse03303](https://discuss.elastic.co/u/truongdqse03303)\
**Post date:** [October 6, 2017, 7:45am UTC](https://discuss.elastic.co/t/help-with-sum-and-max-query/102941/2 "2017-10-06T07:45:43Z")

</div>

Anyone can help me with this problem is:  
Each new\_id have 1 view, and with the same new\_id i take the view at max and return the sum of all view by new\_id not duplicate.  
I tried buget aggs but not working right for now.  
Thanks

---

<div class="post-metadata">

**Author:** ![truongdqse03303](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/truongdqse03303/32/22605_2.png) [@truongdqse03303](https://discuss.elastic.co/u/truongdqse03303)\
**Post date:** [October 6, 2017, 2:46pm UTC](https://discuss.elastic.co/t/help-with-sum-and-max-query/102941/3 "2017-10-06T14:46:44Z")

</div>

Im on urgent now, Any advise would be nice. I'm just new on Elasticsearch query  
Thanks

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 6, 2017, 4:17pm UTC](https://discuss.elastic.co/t/help-with-sum-and-max-query/102941/4 "2017-10-06T16:17:45Z")

</div>

Please read

> [@About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21):
>
> The heart of the free and open Elastic Stack Elasticsearch is a distributed, RESTful search and analytics engine capable of addressing a growing number of use cases. As the heart of the Elastic Stack, it centrally stores your data for lightning fast search, fine‑tuned relevancy, and powerful analytics that scale with ease. warning PLEASE READ THIS SECTION IF IT'S YOUR FIRST POST Some useful links: [elasticsearch reference guide](http://www.elastic.co/guide/en/elasticsearch/reference/current/index.html)[elasticsearch user guide](http://www.elastic.co/guide/en/elasticsearch/guide/current/index.html)[elasticsearch plugins](https://www.elastic.co/guide/en/elasticsearch/plugins/current/index.html)[elasticsearch cl…](https://www.elastic.co/guide/en/elasticsearch/client/index.html)

Specifically the "be patient" part.

---

<div class="post-metadata">

**Author:** ![truongdqse03303](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/truongdqse03303/32/22605_2.png) [@truongdqse03303](https://discuss.elastic.co/u/truongdqse03303)\
**Post date:** [October 7, 2017, 1:49am UTC](https://discuss.elastic.co/t/help-with-sum-and-max-query/102941/5 "2017-10-07T01:49:45Z")

</div>

Hi @dadoonet  
Thanks for reply  
I do try read the doc, bucket aggs or script field in max and sum aggs but always throw err or not working right.

```
{
    "size": 0,
    "query": {
        "bool": {
            "must": [{
                "term": {
                    "zone_id": "1"
                }
            }]
        }
    },

    "aggs": {
        "content_level": {
            "terms": {
                "field": "content_level"
            },
            "aggs": {
                "news_id": {
                    "terms": {
                        "field": "news_id"
                    },
                    "aggs" : {
                        "viewed_count_max" : {
                            "max" : {
                                "script" : {
                                    "source" : "doc.viewed_count.value"
                                }
                            }
                        }
                    }
                }
            }
        }
    }
} 

```

Here is one of my tried query.  
What am i doing wrong here, pls tell me  
Thanks

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 10, 2017, 9:54am UTC](https://discuss.elastic.co/t/help-with-sum-and-max-query/102941/6 "2017-10-10T09:54:34Z")

</div>

Could you provide a full recreation script as described in

> [@About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21):
>
> The heart of the free and open Elastic Stack Elasticsearch is a distributed, RESTful search and analytics engine capable of addressing a growing number of use cases. As the heart of the Elastic Stack, it centrally stores your data for lightning fast search, fine‑tuned relevancy, and powerful analytics that scale with ease. warning PLEASE READ THIS SECTION IF IT'S YOUR FIRST POST Some useful links: [elasticsearch reference guide](http://www.elastic.co/guide/en/elasticsearch/reference/current/index.html)[elasticsearch user guide](http://www.elastic.co/guide/en/elasticsearch/guide/current/index.html)[elasticsearch plugins](https://www.elastic.co/guide/en/elasticsearch/plugins/current/index.html)[elasticsearch cl…](https://www.elastic.co/guide/en/elasticsearch/client/index.html)

It will help to better understand what you are doing.  
Please, try to keep the example as simple as possible.

Also please paste the error message you have.

---

<div class="post-metadata">

**Author:** ![truongdqse03303](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/truongdqse03303/32/22605_2.png) [@truongdqse03303](https://discuss.elastic.co/u/truongdqse03303)\
**Post date:** [October 11, 2017, 4:03am UTC](https://discuss.elastic.co/t/help-with-sum-and-max-query/102941/7 "2017-10-11T04:03:09Z")

</div>

hi @dadoonet  
here is my script

```
{
    "query": {
        "match_all": {}
    },
    "aggs": {
        "content_level": {
            "terms": {"field": "content_level" },
            "aggs": {
                "news_id": {
                    "terms": {"field": "news_id"},
                    "aggs": {
                        "viewed_max": {"max": {"field": "viewed_count"} },
                        "the_sum": { "sum_bucket": {"buckets_path": "viewed_max" } }
                    }
                }
            }
        }
    }
} 

```

and the error

> {  
> "error": {  
> "root\_cause": ,  
> "type": "reduce\_search\_phase\_exception",  
> "reason": "[reduce] ",  
> "phase": "fetch",  
> "grouped": true,  
> "failed\_shards": ,  
> "caused\_by": {  
> "type": "class\_cast\_exception",  
> "reason": "org.elasticsearch.search.aggregations.metrics.max.InternalMax cannot be cast to org.elasticsearch.search.aggregations.InternalMultiBucketAggregation"  
> }  
> },  
> "status": 503  
> }

Thanks for answer me, it's would be great if i can have the right query. I'm trying the other way to solve my problems but it's slow.  
Best regards

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 8, 2017, 4:03am UTC](https://discuss.elastic.co/t/help-with-sum-and-max-query/102941/8 "2017-11-08T04:03:10Z")

</div>

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