Help with sum and max query


(Truongdqse03303) #1

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


(Truongdqse03303) #2

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


(Truongdqse03303) #3

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


(David Pilato) #4

Please read

Specifically the "be patient" part.


(Truongdqse03303) #5

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


(David Pilato) #6

Could you provide a full recreation script as described in

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.


(Truongdqse03303) #7

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


(system) #8

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