2017-07-17 133 views
0

我想獲得子聚合的總和。例如,我通過智能手機進行分組,然後按運營商分組,然後按該運營商的平均價格進行分組。我想爲特定智能手機獲得所有運營商的所有價格的總和。所以基本上,我想是這樣的:獲取總和子聚合

{ 
    "aggs": { 
    "group_by_smartphones": { 
     "terms": { 
     "field": "smartphone", 
     "order": { 
      "_term": "asc" 
     }, 
     "size": 200 
     }, 
     "aggs": { 
     "group_by_sum": { 
      "sum": { 
      "field": "price" 
      }, 
      "aggs": { 
      "group_by_carrier": { 
       "terms": { 
       "field": "carrier", 
       "order": { 
        "group_by_avg": "desc" 
       } 
       }, 
       "aggs": { 
       "group_by_avg": { 
        "avg": { 
        "field": "price" 
        } 
       } 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 

除此之外,當我不喜歡這樣,我得到這個錯誤:

"type": "aggregation_initialization_exception", "reason": "Aggregator [group_by_sum] of type [sum] cannot accept sub-aggregations"

如何解決它,所以我可以得到所有價格的總和爲每個智能手機?

回答

1

就快,其實sumgroup_by_carrier子聚合都必須在同一水平:

{ 
    "aggs": { 
    "group_by_smartphones": { 
     "terms": { 
     "field": "smartphone", 
     "order": { 
      "_term": "asc" 
     }, 
     "size": 200 
     }, 
     "aggs": { 
     "sum_prices": { 
      "sum": { 
      "field": "price" 
      } 
     }, 
     "group_by_carrier": { 
      "terms": { 
      "field": "carrier", 
      "order": { 
       "group_by_avg": "desc" 
      } 
      }, 
      "aggs": { 
      "group_by_avg": { 
       "avg": { 
       "field": "price" 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
}