# Any issue store integer values in text field

**URL:** <https://discuss.elastic.co/t/any-issue-store-integer-values-in-text-field/296143>\
**Category:** Elasticsearch\
**Created:** [February 3, 2022, 5:03am UTC](https://discuss.elastic.co/t/any-issue-store-integer-values-in-text-field/296143 "2022-02-03T05:03:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jsjs2](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@jsjs2](https://discuss.elastic.co/u/jsjs2)\
**Post date:** [February 3, 2022, 5:03am UTC](https://discuss.elastic.co/t/any-issue-store-integer-values-in-text-field/296143/1 "2022-02-03T05:03:06Z")

</div>

Hi,

I have type text field, the value for this field sometimes comes in as integer, it is store in ES as integer. any issue when querying this field later on? will the integer value be indexed as string value by ES?

thanks,  
Shan

---

<div class="post-metadata">

**Author:** ![linkerc](https://avatars.discourse-cdn.com/v4/letter/l/13edae/32.png) [@linkerc](https://discuss.elastic.co/u/linkerc)\
**Post date:** [February 3, 2022, 10:02pm UTC](https://discuss.elastic.co/t/any-issue-store-integer-values-in-text-field/296143/2 "2022-02-03T22:02:10Z")

</div>

The only issue is the very first document's detected "type".  
People often store documents in monthly or some sort of time base fashion. So if the very first document for the new (say monthly) index shows up as an integer, that month's field will be automatically mapped to integer.  
This will create an issue for subsequent documents with "text" value. Those documents will not be stored.

The solution is to use mapping to control what that field's type is. In your case should be "text" since it could be mixed. And because it's a mixed typed field, your only solution is "text".  
There's no issue storing integer as text cuz you can index & search text without issue.

In general, if a field will only be integer, then storing it as integer will be more efficient than text.  
Just use a mapping to fix fields you care about. It's deterministic so you don't have to worry about this particular field being accidentally mapped to integer and cause write to drop bunch of documents.

We had an issue with auto type detection between integer and float. We needed float but documents showing up with integer causing the auto detection to restrict mapping to integer only. Learned the hard way.

---

<div class="post-metadata">

**Author:** ![jsjs2](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@jsjs2](https://discuss.elastic.co/u/jsjs2)\
**Post date:** [February 4, 2022, 5:48pm UTC](https://discuss.elastic.co/t/any-issue-store-integer-values-in-text-field/296143/3 "2022-02-04T17:48:14Z")

</div>

Just to make sure I understand it correctly. right now, I have my field mapped as type text; the data for this field could be 123 or "123", internally ES will store and index 123 as text, both cases will make no difference for search, correct?

---

<div class="post-metadata">

**Author:** ![linkerc](https://avatars.discourse-cdn.com/v4/letter/l/13edae/32.png) [@linkerc](https://discuss.elastic.co/u/linkerc)\
**Post date:** [February 4, 2022, 8:37pm UTC](https://discuss.elastic.co/t/any-issue-store-integer-values-in-text-field/296143/4 "2022-02-04T20:37:45Z")

</div>

correct. You must use "text" since that field is mixed. You have no choice.  
It won't impact searching.  
You just need to ensure "order" when doing comparison.  
Is "123" still greater than "2" for example. I don't remember.

We don't do comparison that much so it's not an issue for us storing number as text. And we often do so when it makes sense. Storing text has the benefit of capturing unforeseen values like "null", "N/A", or "None", etc.  
We sometime can't guarantee upstream data format; therefore, storing as text has such benefit of capturing bad values and ask the engineers to fix their payload, etc...  
If we just force to a restrictive format, the end result is the possibility of losing payload (ES won't store it) and you will not know until the customer is complaining.

Just a real deployment experience to share with you.

---

<div class="post-metadata">

**Author:** ![jsjs2](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@jsjs2](https://discuss.elastic.co/u/jsjs2)\
**Post date:** [February 9, 2022, 7:33pm UTC](https://discuss.elastic.co/t/any-issue-store-integer-values-in-text-field/296143/5 "2022-02-09T19:33:52Z")

</div>

I did some testing, I have a field 'age' with type integer, both {"age":21}, {"age":"21"} got indexed fine, I got {"age":21}, {"age":"21"} in search results. I think even the mapping of this field is integer, ES does not store string value as integer, is it?  
this document {"age":"21w"} will be rejected, which is expected.

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [February 10, 2022, 1:26am UTC](https://discuss.elastic.co/t/any-issue-store-integer-values-in-text-field/296143/6 "2022-02-10T01:26:21Z")

</div>

> [@jsjs2](#):
>
> I got {"age":21}, {"age":"21"} in search results

I suppose they are obtained from `_source`. `_source` stores just the same as the input. You may check by `_field` the way elasticsearch index them.

> **[Retrieve selected fields from a search | Elasticsearch Guide \[8.11\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-fields.html)**

```auto
PUT /test_integer/
{
  "mappings": {
    "properties": {
      "age": {
        "type":"integer"
      }
    }
  }
}

POST /test_integer/_bulk
{"index":{}}
{"age":"12"}
{"index":{}}
{"age":12}

GET /test_integer/_search?filter_path=hits.hits._source,hits.hits.fields
{
  "fields": [
    "age"
  ]
}

```

```auto
{
  "hits" : {
    "hits" : [
      {
        "_source" : {
          "age" : "12"
        },
        "fields" : {
          "age" : [
            12
          ]
        }
      },
      {
        "_source" : {
          "age" : 12
        },
        "fields" : {
          "age" : [
            12
          ]
        }
      }
    ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![jsjs2](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@jsjs2](https://discuss.elastic.co/u/jsjs2)\
**Post date:** [February 10, 2022, 5:21pm UTC](https://discuss.elastic.co/t/any-issue-store-integer-values-in-text-field/296143/7 "2022-02-10T17:21:26Z")

</div>

Thanks.

---

<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:** [March 10, 2022, 5:22pm UTC](https://discuss.elastic.co/t/any-issue-store-integer-values-in-text-field/296143/8 "2022-03-10T17:22:14Z")

</div>

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