Change all the fields to lowercase throw ingest pipeline

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

{
  "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

Rather than using painless, have you tried using a lowercase filter on the fields you wish to lowercase?

I think he is asking to lowercase the field names. :blush:

how to do it in index template

This is worked for me -

Pipeline

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

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

Search

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"
        }
      }
    ]
  }
}
2 Likes