# What is future plan of scripted aggregations

**URL:** <https://discuss.elastic.co/t/what-is-future-plan-of-scripted-aggregations/132964>\
**Category:** Elasticsearch\
**Created:** [May 23, 2018, 9:29am UTC](https://discuss.elastic.co/t/what-is-future-plan-of-scripted-aggregations/132964 "2018-05-23T09:29:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![palashkulsh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/palashkulsh/32/22918_2.png) [@palashkulsh](https://discuss.elastic.co/u/palashkulsh)\
**Post date:** [May 23, 2018, 9:29am UTC](https://discuss.elastic.co/t/what-is-future-plan-of-scripted-aggregations/132964/1 "2018-05-23T09:29:35Z")

</div>

hello team,  
We are planning to use scripted aggregations feature of elasticsearch. But are apprehensive about it because doc [[https://www.elastic.co/guide/en/elasticsearch/reference/6.x/search-aggregations-metrics-scripted-metric-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/6.x/search-aggregations-metrics-scripted-metric-aggregation.html) ] for it says that it is experimental feature and can be removed in future GA releases.

So coming to my question, is there a word about this feature getting permanent or not.

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [May 24, 2018, 10:49am UTC](https://discuss.elastic.co/t/what-is-future-plan-of-scripted-aggregations/132964/2 "2018-05-24T10:49:09Z")

</div>

I think at this point we would probably not remove the scripted metric aggregation without a deprecation period and a suitable replacement for the types of analytics users are using it for. The experimental tag still exists because up to this point we have not been completely happy with its API and this is something we are working to solve with changes like [https://github.com/elastic/elasticsearch/pull/30111](https://github.com/elastic/elasticsearch/pull/30111).

I think there is likely to always be a place for writing custom aggregation logic using a script (at least while creating a plugin that adds a new aggregation is still complex) but I would always see it as a last resort. Many times I have seen users reach for the scripted\_metric aggregation before fully exploring whether their use case can be solved in a different way with the current aggregations. I am not saying that your case is one of these but I would advise exploring options for not using the scripted\_metric aggregation where you can.

Also, as we are always looking to expand the list of out of the box aggregations, if your use case feels like it might be generally useful then feel free to open an issue on the Elasticsearch Github repo and we can explore whether we should build a dedicated aggregation to solve use cases like yours.

---

<div class="post-metadata">

**Author:** ![palashkulsh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/palashkulsh/32/22918_2.png) [@palashkulsh](https://discuss.elastic.co/u/palashkulsh)\
**Post date:** [May 24, 2018, 11:25am UTC](https://discuss.elastic.co/t/what-is-future-plan-of-scripted-aggregations/132964/3 "2018-05-24T11:25:04Z")

</div>

thanks for the heads up and valuable information @colings86 .  
Didn't want to stray offtopic but the usecase I'm trying to solve is

currently we sum over single field. but I want to sum up over 1 field and if that field is not present then some another field should be added to the sum.

PS. I always wanted to thank someone from elasticsearch for such a great & heavy duty thing. So "thanks" 🙂

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [May 24, 2018, 11:52am UTC](https://discuss.elastic.co/t/what-is-future-plan-of-scripted-aggregations/132964/4 "2018-05-24T11:52:37Z")

</div>

So actually this is a use case where I would advise not using the scripted\_metric aggregation but instead to use the fact that on most aggregations that you can specify a `field` you can instead specify a `script`. See [https://www.elastic.co/guide/en/elasticsearch/reference/6.2/search-aggregations-metrics-sum-aggregation.html#\_script\_9](https://www.elastic.co/guide/en/elasticsearch/reference/6.2/search-aggregations-metrics-sum-aggregation.html#_script_9) for more information on this for the sum aggregation.

So for your use case I would use the sum aggregation but instead of specifying the `field` I would specify a `script` option something like the following (disclaimer: I have not tested this so the syntax might need a bit of tweaking):

```auto
{
	"size": 0,
	"aggs": {
		"my_sum": {
			"sum": {
				"script": {
					"source": "doc[params.primary_field].value == null ? doc[params.secondary_field].value : doc[params.primary_field].value",
					"params": {
						"primary_field": "my_field",
						"secondary_field": "my_field2"
					}
				}
			}
		}
	}
}

```

That way you don't need to worry about the complexity of the scripted\_metric aggregation.

---

<div class="post-metadata">

**Author:** ![palashkulsh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/palashkulsh/32/22918_2.png) [@palashkulsh](https://discuss.elastic.co/u/palashkulsh)\
**Post date:** [May 24, 2018, 2:16pm UTC](https://discuss.elastic.co/t/what-is-future-plan-of-scripted-aggregations/132964/5 "2018-05-24T14:16:03Z")

</div>

thank you so much Colin , Thank you so much.

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [May 24, 2018, 4:50pm UTC](https://discuss.elastic.co/t/what-is-future-plan-of-scripted-aggregations/132964/6 "2018-05-24T16:50:39Z")

</div>

> doc[params.primary\_field].value == null

Just a minor note: this won't work. If the field is missing, `.value` currently returns a "default" value. There is some work being done to make this better ([Handle missing and multiple values in script by mayya-sharipova · Pull Request #29611 · elastic/elasticsearch · GitHub](https://github.com/elastic/elasticsearch/pull/29611)), but in the meantime, you can check the size of the return object from doc.

```auto
doc[params.primary_field].size() == 0

```

---

<div class="post-metadata">

**Author:** ![palashkulsh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/palashkulsh/32/22918_2.png) [@palashkulsh](https://discuss.elastic.co/u/palashkulsh)\
**Post date:** [May 24, 2018, 6:28pm UTC](https://discuss.elastic.co/t/what-is-future-plan-of-scripted-aggregations/132964/7 "2018-05-24T18:28:58Z")

</div>

thanks @rjernst , worked like a charm. Thanks for your time.

---

<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:** [June 21, 2018, 6:29pm UTC](https://discuss.elastic.co/t/what-is-future-plan-of-scripted-aggregations/132964/8 "2018-06-21T18:29:00Z")

</div>

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