# Convert Bytes to Gigabytes

**URL:** <https://forum.opensearch.org/t/convert-bytes-to-gigabytes/1076>\
**Category:** Alerting\
**Created:** [July 8, 2019, 9:46am UTC](https://forum.opensearch.org/t/convert-bytes-to-gigabytes/1076 "2019-07-08T09:46:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![yangcaixing](https://avatars.discourse-cdn.com/v4/letter/y/13edae/32.png) [@yangcaixing](https://forum.opensearch.org/u/yangcaixing)\
**Post date:** [July 8, 2019, 9:46am UTC](https://forum.opensearch.org/t/convert-bytes-to-gigabytes/1076/1 "2019-07-08T09:46:31Z")

</div>

Hi all,  
I have a question about ELK query, I use DSL to query client ip and traffic, but the totally sum traffic is default format bytes, how can I calculate and convert to Gigabytes, if anyone can help me, thanks a lot.

 ![image](https://us1.discourse-cdn.com/flex019/uploads/mauve_hedgehog/original/1X/b6b39a1ac426328cb13359a5933526e8908c1be8.png)

---

<div class="post-metadata">

**Author:** ![mihirsoni](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.opensearch.org/mihirsoni/32/57_2.png) [@mihirsoni](https://forum.opensearch.org/u/mihirsoni)\
**Post date:** [July 8, 2019, 6:49pm UTC](https://forum.opensearch.org/t/convert-bytes-to-gigabytes/1076/2 "2019-07-08T18:49:54Z")

</div>

Hi @yangcaixing

There are couple of ways to solve this, simple way would be 🙂

Convert your threshold to bytes (i.e 1GB = 1073741824 bytes source [https://whatsabyte.com/P1/byteconverter.htm](https://whatsabyte.com/P1/byteconverter.htm)) and use this as condition.

Another approach would be to do this calculation as part of your query, you can use script\_field to do this calculation.

---

<div class="post-metadata">

**Author:** ![yangcaixing](https://avatars.discourse-cdn.com/v4/letter/y/13edae/32.png) [@yangcaixing](https://forum.opensearch.org/u/yangcaixing)\
**Post date:** [July 9, 2019, 6:03am UTC](https://forum.opensearch.org/t/convert-bytes-to-gigabytes/1076/3 "2019-07-09T06:03:00Z")

</div>

Hi @mihirsoni,  
I try to use the script\_field in query as below ,but it doesn’t works, I don’t know if the script location is right or not, could you help check this thanks very much.  
“aggregations”: {  
“client\_host”: {  
“terms”: {  
“field”: “flow.client\_hostname”,  
“size”: 10,  
“min\_doc\_count”: 1,  
“shard\_min\_doc\_count”: 0,  
“show\_term\_doc\_count\_error”: false,  
“order”: [  
{  
“total\_bytes”: “desc”  
},  
{  
“\_count”: “desc”  
},  
{  
“\_key”: “asc”  
}  
]  
},  
“aggregations”: {  
“total\_bytes”: {  
“sum”: {  
“field”: “flow.bytes”  
},  
“script\_fields”: {  
“test”: {  
“script”: {  
“lang”: “painless”,  
“source”: “doc[‘flow.bytes’].value /1024 /1024 /1024”  
}  
}  
}  
},  
“bytes\_bucket\_filter”: {  
“bucket\_selector”: {  
“buckets\_path”: {  
“totalBytes”: “total\_bytes”  
},  
“script”: {  
“source”: “params.totalBytes \> 1”,  
“lang”: “painless”  
},  
“gap\_policy”: “skip”  
}  
}  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![yangcaixing](https://avatars.discourse-cdn.com/v4/letter/y/13edae/32.png) [@yangcaixing](https://forum.opensearch.org/u/yangcaixing)\
**Post date:** [July 10, 2019, 2:39am UTC](https://forum.opensearch.org/t/convert-bytes-to-gigabytes/1076/4 "2019-07-10T02:39:20Z")

</div>

Hi @mihirsoni,  
I used below script to calculate, but it seems wrong, the value always equal 0, would please help have a look, thanks a lot.

 ![image](https://us1.discourse-cdn.com/flex019/uploads/mauve_hedgehog/original/1X/713b7d3e7687cd43c4b1821cb3ee5e37108bd197.png)

---

<div class="post-metadata">

**Author:** ![yangcaixing](https://avatars.discourse-cdn.com/v4/letter/y/13edae/32.png) [@yangcaixing](https://forum.opensearch.org/u/yangcaixing)\
**Post date:** [July 11, 2019, 7:31am UTC](https://forum.opensearch.org/t/convert-bytes-to-gigabytes/1076/5 "2019-07-11T07:31:01Z")

</div>

Hi @mihirsoni  
I have solved the case , thanks so much for the support from beginning, it make a great help for me to achieve the goals, hope you been well all the time.😃

---

<div class="post-metadata">

**Author:** ![mashraf](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.opensearch.org/mashraf/32/730_2.png) [@mashraf](https://forum.opensearch.org/u/mashraf)\
**Post date:** [August 17, 2020, 6:33am UTC](https://forum.opensearch.org/t/convert-bytes-to-gigabytes/1076/6 "2020-08-17T06:33:39Z")

</div>

Hi @yangcaixing hope you doing fine, i am facing the same issue in retrieving the sum in bytes and wants to show the value in GBs or MBs could you please help with this.

.

 ![image](https://us1.discourse-cdn.com/flex019/uploads/mauve_hedgehog/original/1X/4fe2cffc953c334a87a7f6fae2d3df5e62055fb6.png)
