# Renaming field during insert based on type

**URL:** <https://discuss.elastic.co/t/renaming-field-during-insert-based-on-type/168654>\
**Category:** Elasticsearch\
**Created:** [February 15, 2019, 9:50pm UTC](https://discuss.elastic.co/t/renaming-field-during-insert-based-on-type/168654 "2019-02-15T21:50:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erik\_Tribou](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/erik_tribou/32/40998_2.png) [@Erik\_Tribou](https://discuss.elastic.co/u/Erik_Tribou)\
**Post date:** [February 15, 2019, 9:50pm UTC](https://discuss.elastic.co/t/renaming-field-during-insert-based-on-type/168654/1 "2019-02-15T21:50:34Z")

</div>

Could I have some guidance on the correct way to append a suffix indicating type to a field name during the insertion process?

So for example if inserting { "a": "2015-09-21"} we would create a document like {"a\_date" : 2015-09-21 (as date type) }

and

{"a" : "abc"} would create a document like {"a\_string" : "abc" }

Neither document would have a key for "a".

Dynamic Templates seem to get part of the way there as you can differentiate based on type and add new keys, but it looks like I can't get rid of the original key.

If I need an ingest plugin to accomplish this could someone provide a piece of code to perform a basic name transformation based on type?

---

<div class="post-metadata">

**Author:** ![gbrown](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gbrown/32/34482_2.png) [@gbrown](https://discuss.elastic.co/u/gbrown)\
**Post date:** [February 15, 2019, 11:24pm UTC](https://discuss.elastic.co/t/renaming-field-during-insert-based-on-type/168654/2 "2019-02-15T23:24:26Z")

</div>

I would indeed use ingest pipelines for a case like this. For your example case, you can use the [`date` processor](https://www.elastic.co/guide/en/elasticsearch/reference/6.6/date-processor.html) to extract a date and `ignore_failure: true` to skip the step if parsing the date fails, the [`set` processor](https://www.elastic.co/guide/en/elasticsearch/reference/6.6/set-processor.html) with a condition to copy `a` to `a_string` only if the field `a_date` does not exist, and finally a `remove` processor to remove the field `a` from the document.

Here's an example pipeline:

```auto
PUT _ingest/pipeline/maybe-dates
{
  "processors": [
    {
      "date": {
        "field": "a",
        "target_field": "a_date",
        "formats": ["yyyy-MM-dd"],
        "ignore_failure": true
      }
    },
    {
      "set": {
        "if": "ctx.a_date == null", 
        "field": "a_string",
        "value": "{{a}}"
      }
    },
    {
      "remove": {
        "field": "a"
      }
    }
  ]
}

```

And here's a call to the [simulate pipeline API](https://www.elastic.co/guide/en/elasticsearch/reference/6.6/simulate-pipeline-api.html) with a couple of example docs demonstrating that it works.

```auto
POST _ingest/pipeline/maybe-dates/_simulate
{
  "docs": [
    {
      "_source": {
        "a": "asdf"
      }
    },
    {
      "_source": {
        "a": "2019-02-15"
      }
    }
  ]
}

```

Which should return something like:

```auto
{
  "docs" : [
    {
      "doc" : {
        "_index" : "_index",
        "_type" : "_type",
        "_id" : "_id",
        "_source" : {
          "a_string" : "asdf"
        },
        "_ingest" : {
          "timestamp" : "2019-02-15T23:20:21.918Z"
        }
      }
    },
    {
      "doc" : {
        "_index" : "_index",
        "_type" : "_type",
        "_id" : "_id",
        "_source" : {
          "a_date" : "2019-02-15T00:00:00.000Z"
        },
        "_ingest" : {
          "timestamp" : "2019-02-15T23:20:21.918Z"
        }
      }
    }
  ]
}

```

If you need other types, you can do something similar with the [`convert` processor](https://www.elastic.co/guide/en/elasticsearch/reference/6.6/convert-processor.html) instead of `date`.

---

<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 15, 2019, 11:24pm UTC](https://discuss.elastic.co/t/renaming-field-during-insert-based-on-type/168654/3 "2019-03-15T23:24:28Z")

</div>

Closed on March 15, 2019, 11:24pm UTC

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