# How to add leading zeros to a numeric field using the ingest pipeline?

**URL:** <https://discuss.elastic.co/t/how-to-add-leading-zeros-to-a-numeric-field-using-the-ingest-pipeline/300885>\
**Category:** Elasticsearch\
**Tags:** ingest-pipeline\
**Created:** [March 28, 2022, 7:11pm UTC](https://discuss.elastic.co/t/how-to-add-leading-zeros-to-a-numeric-field-using-the-ingest-pipeline/300885 "2022-03-28T19:11:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Patr123](https://avatars.discourse-cdn.com/v4/letter/p/ac91a4/32.png) [@Patr123](https://discuss.elastic.co/u/Patr123)\
**Post date:** [March 28, 2022, 7:11pm UTC](https://discuss.elastic.co/t/how-to-add-leading-zeros-to-a-numeric-field-using-the-ingest-pipeline/300885/1 "2022-03-28T19:11:15Z")

</div>

I have a field that needs to be a 3 digit field always. so if the field value is 1 it should be coming in as 001 and if its 11 then it should be coming in as 011. I am parsing this field from a different field using the grok processor and when I simulate the document its showing me 3 digit field but in discover its removing the leading zeros from the field.  
I am also not seeing the mutate and replace processor in ingest pipeline using which I know can be useful to my case as shown in the below link:

> [@Can I add leading zeros to a field?](https://discuss.elastic.co/t/can-i-add-leading-zeros-to-a-field/122081/2):
>
> Do you always want to leading zeroes or do you always want the string to be five characters long? (Either way the answer would be to use a mutate filter's gsub option, but an exact answer depends on the details.)

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [March 29, 2022, 8:28am UTC](https://discuss.elastic.co/t/how-to-add-leading-zeros-to-a-numeric-field-using-the-ingest-pipeline/300885/2 "2022-03-29T08:28:12Z")

</div>

Hey,

just to be sure. if your field should have leading zeros, it must be a string value and cannot be a number. Is this OK for you?

See this example

```auto
POST _ingest/pipeline/_simulate
{
  "docs": [
    {
      "_source": {
        "value": 1
      }
    },
    {
      "_source": {
        "value": 99
      }
    }
  ],
  "pipeline": {
    "processors": [
      {
        "script": {
          "source": """
          ctx.x = String.format("%03d", new def[] { ctx.value });
          """
        }
      }
    ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![Patr123](https://avatars.discourse-cdn.com/v4/letter/p/ac91a4/32.png) [@Patr123](https://discuss.elastic.co/u/Patr123)\
**Post date:** [March 30, 2022, 3:36pm UTC](https://discuss.elastic.co/t/how-to-add-leading-zeros-to-a-numeric-field-using-the-ingest-pipeline/300885/3 "2022-03-30T15:36:43Z")

</div>

Hello Spinscale,

Yes, I changed the mapping from numbers to text and the values are coming in as needed.

Thank you.

---

<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:** [April 27, 2022, 3:36pm UTC](https://discuss.elastic.co/t/how-to-add-leading-zeros-to-a-numeric-field-using-the-ingest-pipeline/300885/4 "2022-04-27T15:36:50Z")

</div>

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