# Using scripts with aggregation

**URL:** <https://discuss.elastic.co/t/using-scripts-with-aggregation/338378>\
**Category:** Elasticsearch\
**Created:** [July 14, 2023, 7:35am UTC](https://discuss.elastic.co/t/using-scripts-with-aggregation/338378 "2023-07-14T07:35:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_baba](https://avatars.discourse-cdn.com/v4/letter/_/ecb155/32.png) [@\_baba](https://discuss.elastic.co/u/_baba)\
**Post date:** [July 14, 2023, 7:35am UTC](https://discuss.elastic.co/t/using-scripts-with-aggregation/338378/1 "2023-07-14T07:35:35Z")

</div>

Hi, I have a use case where the value of a field name signal can be 0, 1, or 2. I have to perform aggregation on this field but there are few records in the index without the field.

For the field not\_exist, I need to assign the value 0 to it and then perform aggregation. How should I use script in this case.

Mapping is as follow:

````auto
  "product": {
    "mappings" : {
      "properties" : {
        "company_id" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "company_name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
      "signal" : {
          "type" : "long"
        },
}
}
}
}```

```POST product/_doc
{
			"company_id": 22,
			"company_name": "abc",
                        "signal": 0
}

POST product/_doc
{
			"company_id": 23,
			"company_name": "abcd",
                        "signal": 1
}

POST product/_doc
{
			"company_id": 24,
			"company_name": "ab",
                        "signal": 2
}

POST product/_doc
{
			"company_id": 25,
			"company_name": "abcde"
}```

Expected output:

```"buckets" : [
        {
          "key" : 0,
          "doc_count" : 2
        },
        {
          "key" : 1,
          "doc_count" : 1
        },
       {
          "key" : 2,
          "doc_count" : 1
        }
      ]```
````

---

<div class="post-metadata">

**Author:** ![PodarcisMuralis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/podarcismuralis/32/122606_2.png) [@PodarcisMuralis](https://discuss.elastic.co/u/PodarcisMuralis)\
**Post date:** [July 14, 2023, 9:13pm UTC](https://discuss.elastic.co/t/using-scripts-with-aggregation/338378/2 "2023-07-14T21:13:38Z")

</div>

```auto
POST product/_search
{
  "size": 0,
  "aggs": {
    "signal_agg": {
      "terms": {
        "script": {
          "source": "doc.containsKey('signal') ? doc['signal'].value : 0"
        }
      }
    }
  }
}

```

---

<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:** [August 11, 2023, 9:14pm UTC](https://discuss.elastic.co/t/using-scripts-with-aggregation/338378/3 "2023-08-11T21:14:12Z")

</div>

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