{
	"script": {
		"lang": "mustache",
		"source": """
		{
			"_source": {
				"includes": [
					"{{aggComponentName}}",
					"usage"
				]
			},
			"query": {
				"bool": {
					"must": [
						{
							"match_phrase": {
								"network_id": {
									"query": "{{networkId}}"
								}
							}
						},
						{
							"match_phrase": {
								"network_group_id": {
									"query": "{{networkGroupId}}"
								}
							}
						},
						{
							"bool": {
								"should": [
									{
										"match_phrase": {
											"usage_type.keyword": "usage.ingress.tx"
										}
									},
									{
										"match_phrase": {
											"usage_type.keyword": "usage.egress.tx"
										}
									}
								],
								"minimum_should_match": 1
							}
						},
						
						{
							"range": {
								"@timestamp": {
									"gte": "{{gte}}",
									"lte": "{{lte}}",
									"format": "epoch_millis"
								}
							}
						}
						
						{{#componentName}},
						 
						  {
							"match_phrase": {
								"{{componentName}}": {
									"query": "{{componentValue}}"
								}
							}
					  	
						}
						{{/componentName}}
						
					],
					"filter": {
						"bool": {
							"must": [
								{
									"match_all": {}
								}
								{{#attributes}},
								{
									"terms": {
									"attributes.keyword": {{#toJson}}attributes{{/toJson}}
										
									}
								}
							 {{/attributes}}
								
							]
						}
					}
				}
			},
			"size": "{{size}}",
			"aggs": {
				"items": {
					"terms": {
						"field": "{{aggComponentName}}",
						"size": "{{aggTermSize}}",
						"order": {
							"usage_bytes": "desc"
						}
					},
					"aggs": {
						"usage_bytes": {
							"sum": {
								"field": "usage"
							}
						},
						"timebuckets": {
							"date_histogram": {
								"field": "timestamp",
								"fixed_interval": "{{interval}}",
								"time_zone": "{{timeZone}}",
								"min_doc_count": 0
							},
							"aggs": {
								"usage_bytes": {
									"sum": {
										"field": "usage"
									}
								}
							}
						}
					}
				}
			}
		}
		"""
	}
}