{
	"script": {
		"lang": "mustache",
		"source": """
		 {
			"_source": {
				"includes": [
					"usage_type.keyword",
					"usage",
					"timestamp"
				]
			},
			"query": {
				"bool": {
					"must": [
						{
							"match_phrase": {
								"network_id": {
									"query": "{{network_id_query_param}}"
								}
							}
						},
						{
							"match_phrase": {
								"network_group_id": {
									"query": "{{network_group_id_query_param}}"
								}
							}
						},
						{
							"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": "{{timestamp_greater_than_equal_query_param}}",
									"lte": "{{timestamp_lesser_than_equal_query_param}}",
									"format": "epoch_millis"
								}
							}
						}
						
						{
							{#match_phrase_condition_query_param
							}
						},
						{
							"match_phrase": {
								"{{phrase_name_query_param}}": {
									"query": "{{phrase_name_value_query_param}}"
								}
							}
						}
						{
							{/match_phrase_condition_query_param
							}
						}
					],
					"filter": {
						"bool": {
							"must": [
								{
									"match_all": {}
								}
								 {
									{#term_condition_query_param
									}
								},
								{
									"terms": {
										"attributes.keyword": [
											"{{#term_name_values_query_param}}",
											"{{.}}",
											"{{/term_name_values_query_param}}"
										]
									}
								}
							 {
									{/term_condition_query_param
									}
								}
							]
						}
					}
				}
			},
			"size": "{{size_query_param}}",
			"aggs": {
				"timebuckets": {
					"date_histogram": {
						"field": "timestamp",
						"interval": "{{timestamp_interval_query_param}}",
						"time_zone": "{{timezone_query_param}}",
						"min_doc_count": 1
					},
					"aggs": {
						"usage_sums": {
							"terms": {
								"field": "usage_type.keyword",
								"size": 2,
								"order": {
									"usage_bytes": "desc"
								}
							},
							"aggs": {
								"usage_bytes": {
									"sum": {
										"field": "usage"
									}
								}
							}
						}
					}
				}
			}
		}
		"""
	}
}