# Change all the fields to lowercase throw ingest pipeline

**URL:** <https://discuss.elastic.co/t/change-all-the-fields-to-lowercase-throw-ingest-pipeline/358816>\
**Category:** Elastic Search\
**Tags:** painless\
**Created:** [May 6, 2024, 11:18am UTC](https://discuss.elastic.co/t/change-all-the-fields-to-lowercase-throw-ingest-pipeline/358816 "2024-05-06T11:18:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![han\_fatzot](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/han_fatzot/32/134164_2.png) [@han\_fatzot](https://discuss.elastic.co/u/han_fatzot)\
**Post date:** [May 6, 2024, 11:18am UTC](https://discuss.elastic.co/t/change-all-the-fields-to-lowercase-throw-ingest-pipeline/358816/1 "2024-05-06T11:18:18Z")

</div>

how can i change all the fields to lowercase throw ingest pipeline  
how can i do it?  
i tried it but an error compile accorded

```auto
{
  "processors": [
    {
      "script": {
        "lang": "painless",
        "source": """
          def fields = ctx._source.keySet();
          for (field in fields) {
            ctx._source.put(field + ".text", ctx._source.get(field));
          }
        """
      }
    }
  ]
}

```

Is this method the best practice or can another method be used for creating all the fields in lowercase

---

<div class="post-metadata">

**Author:** ![Kathleen\_DeRusso](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kathleen_derusso/32/132039_2.png) [@Kathleen\_DeRusso](https://discuss.elastic.co/u/Kathleen_DeRusso)\
**Post date:** [May 6, 2024, 12:12pm UTC](https://discuss.elastic.co/t/change-all-the-fields-to-lowercase-throw-ingest-pipeline/358816/2 "2024-05-06T12:12:38Z")

</div>

Rather than using painless, have you tried using a [lowercase filter](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-lowercase-tokenfilter.html) on the fields you wish to lowercase?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [May 6, 2024, 5:45pm UTC](https://discuss.elastic.co/t/change-all-the-fields-to-lowercase-throw-ingest-pipeline/358816/3 "2024-05-06T17:45:59Z")

</div>

I think he is asking to lowercase the field names. 😊

---

<div class="post-metadata">

**Author:** ![han\_fatzot](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/han_fatzot/32/134164_2.png) [@han\_fatzot](https://discuss.elastic.co/u/han_fatzot)\
**Post date:** [May 19, 2024, 2:30pm UTC](https://discuss.elastic.co/t/change-all-the-fields-to-lowercase-throw-ingest-pipeline/358816/4 "2024-05-19T14:30:42Z")

</div>

how to do it in index template

---

<div class="post-metadata">

**Author:** ![ashishtiwari1993](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ashishtiwari1993/32/135241_2.png) [@ashishtiwari1993](https://discuss.elastic.co/u/ashishtiwari1993)\
**Post date:** [May 19, 2024, 8:43pm UTC](https://discuss.elastic.co/t/change-all-the-fields-to-lowercase-throw-ingest-pipeline/358816/5 "2024-05-19T20:43:04Z")

</div>

This is worked for me -

## Pipeline

```auto
PUT _ingest/pipeline/lowercase_fields
{
  "processors": [
    {
      "script": {
        "lang": "painless",
        "source": """
          def keys = new HashSet(ctx.keySet());
          for (key in keys) {
            def lowerKey = key.toLowerCase();
            if (lowerKey != key) {
              ctx[lowerKey] = ctx.remove(key);
            }
          }
        """
      }
    }
  ]
}

```

## Ingest Data

```auto
POST lowercase-test/_doc?pipeline=lowercase_fields
{
  "FieldName1": "value1",
  "FieldName2": "value2",
  "FieldName3": "value3"
}

```

## Search

```auto
GET lowercase-test/_search

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "lowercase-test",
        "_id": "BtGWko8BZFU3azzCAle4",
        "_score": 1,
        "_source": {
          "fieldname1": "value1",
          "fieldname2": "value2",
          "fieldname3": "value3"
        }
      }
    ]
  }
}

```

---

<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:** [June 16, 2024, 8:43pm UTC](https://discuss.elastic.co/t/change-all-the-fields-to-lowercase-throw-ingest-pipeline/358816/6 "2024-06-16T20:43:07Z")

</div>

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