# How to aggregate over 'ip' field using script

**URL:** <https://discuss.elastic.co/t/how-to-aggregate-over-ip-field-using-script/148817>\
**Category:** Elasticsearch\
**Created:** [September 17, 2018, 11:07am UTC](https://discuss.elastic.co/t/how-to-aggregate-over-ip-field-using-script/148817 "2018-09-17T11:07:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sayali\_hinge](https://avatars.discourse-cdn.com/v4/letter/s/df788c/32.png) [@sayali\_hinge](https://discuss.elastic.co/u/sayali_hinge)\
**Post date:** [September 17, 2018, 11:07am UTC](https://discuss.elastic.co/t/how-to-aggregate-over-ip-field-using-script/148817/1 "2018-09-17T11:07:26Z")

</div>

I am trying to perform terms aggregation over a field of type 'ip' using inline script like this :  
\> {

> ```
> "aggs":{
> "by_ipaddress":{
> "terms":{
> "script" : {
> "inline" : "doc['ipAddressFrom'].value",
> "lang" : "painless"
> }
> }
> }
> }
> }
> 
> ```

It throws following exception :  
\> "reason": {

> ```
> "type": "script_exception",
> "reason": "runtime error",
> "caused_by": {
> "type": "array_index_out_of_bounds_exception",
> "reason": null
> },
> "script_stack": [],
> "script": "doc['ipAddressFrom'].value",
> "lang": "painless"
> }
> }
> 
> ```

But when I aggregate over the same field :

> {  
> "aggs":{  
> "by\_ipaddress":{  
> "terms":{  
> "field":"ipAddressFrom"  
> }  
> }  
> }  
> }

It works.

Mapping for the field "ipAddressFrom" is :

> ```
> "ipAddressFrom" : {
> "type" : "ip"
> }
> 
> ```

Please let me know how to use ip fields in script.

---

<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:** [October 15, 2018, 11:07am UTC](https://discuss.elastic.co/t/how-to-aggregate-over-ip-field-using-script/148817/2 "2018-10-15T11:07:31Z")

</div>

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