# How do I set a field type using grok?

**URL:** <https://discuss.elastic.co/t/how-do-i-set-a-field-type-using-grok/24662>\
**Category:** Logstash\
**Created:** [June 30, 2015, 8:11pm UTC](https://discuss.elastic.co/t/how-do-i-set-a-field-type-using-grok/24662 "2015-06-30T20:11:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![naisanza](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/naisanza/32/44808_2.png) [@naisanza](https://discuss.elastic.co/u/naisanza)\
**Post date:** [June 30, 2015, 8:11pm UTC](https://discuss.elastic.co/t/how-do-i-set-a-field-type-using-grok/24662/1 "2015-06-30T20:11:02Z")

</div>

I know that `%{NUMBER:field:integer}` works, actually I think this works, too: `%{NUMBER:field:int}`

I use the grok filter heavily and would like to know if I can set the field type in a custom pattern without needing to use mutate to convert the field to an integer.

For instance, will any of these work by design?

`(?<field>regex):integer`  
or  
`((?<field>regex):integer)`  
or  
`(?<field:integer>regex)`

---

<div class="post-metadata">

**Author:** ![eperry](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eperry/32/551_2.png) [@eperry](https://discuss.elastic.co/u/eperry)\
**Post date:** [June 30, 2015, 8:19pm UTC](https://discuss.elastic.co/t/how-do-i-set-a-field-type-using-grok/24662/2 "2015-06-30T20:19:49Z")

</div>

The nice way of doing something like this is

%{NUMBER:fieldname:int}

---

<div class="post-metadata">

**Author:** ![theuntergeek](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/theuntergeek/32/44961_2.png) [@theuntergeek](https://discuss.elastic.co/u/theuntergeek)\
**Post date:** [June 30, 2015, 8:36pm UTC](https://discuss.elastic.co/t/how-do-i-set-a-field-type-using-grok/24662/3 "2015-06-30T20:36:58Z")

</div>

I don't think that the `:integer` type-casting will work, but you should make some simple tests with `output { stdout { codec => rubydebug } }` to find out.

While Logstash does some typing within (e.g. `:int`, `:float`, and `:string` (the default type))—and these are great for typing and comparison operations—Elasticsearch does it much more broadly, and to greater effect through [manually mapping the core numeric types](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-core-types.html#number):

> The type of the number. Can be float, double, integer, long, short, byte.

As with java typing, a float will take up less "space" than a double, and the same is true of the integer types: long \> integer \> short \> byte.

When you declare an integer with `:int` or mutate convert in Logstash, it will appear to be an integer in the JSON sent to Elasticsearch. Likewise, `:float` will appear to be a floating point value in the JSON. The trouble is that Elasticsearch doesn't know the scale you intend, so it guesses double for any floating point value, and long for any integer value (see the first line of the [Core Types documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-core-types.html#mapping-core-types)). This guesswork on Elasticsearch's part doesn't usually hamper anyone, but for performance and storage reasons, I'd recommend using the smallest type that will fit your data—if you can manually map and type your fields, that is.

---

<div class="post-metadata">

**Author:** ![naisanza](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/naisanza/32/44808_2.png) [@naisanza](https://discuss.elastic.co/u/naisanza)\
**Post date:** [June 30, 2015, 9:24pm UTC](https://discuss.elastic.co/t/how-do-i-set-a-field-type-using-grok/24662/4 "2015-06-30T21:24:02Z")

</div>

Very, very explanatory! Eventually, the storage sizes and performance needed will be likely with hundreds of terabytes. The current working datasets aren't that large, but the strains can already be felt with a moderately beefy VM (java core dumps, out of memory).

One thing I haven't tested out yet is the ability to re-index. With that, even if data is initially poorly indexed, re-indexing will allow those indexes to become more useful; without needed to reparse the original data.

---

<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:** [July 6, 2017, 5:35am UTC](https://discuss.elastic.co/t/how-do-i-set-a-field-type-using-grok/24662/5 "2017-07-06T05:35:55Z")

</div>


