# Convert Field data type while re-indexing the Index In Elasticsearch

**URL:** <https://discuss.elastic.co/t/convert-field-data-type-while-re-indexing-the-index-in-elasticsearch/224378>\
**Category:** Elasticsearch\
**Created:** [March 20, 2020, 8:43am UTC](https://discuss.elastic.co/t/convert-field-data-type-while-re-indexing-the-index-in-elasticsearch/224378 "2020-03-20T08:43:42Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![avinash\_kumawat](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/avinash_kumawat/32/44393_2.png) [@avinash\_kumawat](https://discuss.elastic.co/u/avinash_kumawat)\
**Post date:** [March 20, 2020, 8:43am UTC](https://discuss.elastic.co/t/convert-field-data-type-while-re-indexing-the-index-in-elasticsearch/224378/1 "2020-03-20T08:43:42Z")

</div>

Hello All,

Is there any way to convert string data type into object type or nested type and vice-versa while re-indexing the Index using the POST \_reindex API in Elasticsearch?

Regards,  
Avinash Kumawat

---

<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:** [March 20, 2020, 9:03am UTC](https://discuss.elastic.co/t/convert-field-data-type-while-re-indexing-the-index-in-elasticsearch/224378/2 "2020-03-20T09:03:29Z")

</div>

With a `rename` processor in an ingest pipeline.

---

<div class="post-metadata">

**Author:** ![avinash\_kumawat](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/avinash_kumawat/32/44393_2.png) [@avinash\_kumawat](https://discuss.elastic.co/u/avinash_kumawat)\
**Post date:** [March 20, 2020, 12:39pm UTC](https://discuss.elastic.co/t/convert-field-data-type-while-re-indexing-the-index-in-elasticsearch/224378/3 "2020-03-20T12:39:23Z")

</div>

Hi David, my question is about to convert document field data type while re-indexing the Index for example string to integer for that i know there is a way "Convert Processor"

[https://www.elastic.co/guide/en/elasticsearch/reference/master/convert-processor.html#convert-processor](https://www.elastic.co/guide/en/elasticsearch/reference/master/convert-processor.html#convert-processor)

But i want to convert string into object/nested type and vice versa.

---

<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:** [March 20, 2020, 1:24pm UTC](https://discuss.elastic.co/t/convert-field-data-type-while-re-indexing-the-index-in-elasticsearch/224378/4 "2020-03-20T13:24:40Z")

</div>

Could you share a document before and what it should look like after?

---

<div class="post-metadata">

**Author:** ![avinash\_kumawat](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/avinash_kumawat/32/44393_2.png) [@avinash\_kumawat](https://discuss.elastic.co/u/avinash_kumawat)\
**Post date:** [March 23, 2020, 6:58am UTC](https://discuss.elastic.co/t/convert-field-data-type-while-re-indexing-the-index-in-elasticsearch/224378/5 "2020-03-23T06:58:19Z")

</div>

Hi David,

I have created one index with mapping like -

Step 1

```auto
PUT /test_field_type/
{
    "mappings" : {
        "properties" : {
            "field1" : { 
              "type" : "integer" 
            },
            "field2" : { 
              "type" : "text" 
            },
            "field3" : { 
              "type" : "float" 
            },
            "field4" : { 
              "type" : "double" 
            },
            "field5" : { 
              "type" : "long" 
            },
            "field6" : { 
              "type" : "boolean" 
            }
        }
    }
}

```

Step 2: Insert one document in the index -

```auto

POST /test_field_type/_doc
{
  "field1":111,
  "field2":"sample_text1",
  "field3":11.10,
  "field4":1111.100200,
  "field5":9876543231,
  "field6":true
}

```

Step 3:- Created the ingest pipeline to convert the field type

```auto

PUT _ingest/pipeline/string_convert_pipline
{
  "description": "converts the content of the field2 to string type",
  "processors" : [
    {
      "convert" : {
        "field" : "field1",
        "type": "string"
      }
    },
    {
      "convert" : {
        "field" : "field6",
        "type": "string"
      }
    },
    {
      "convert" : {
        "field" : "field5",
        "type": "string"
      }
    }
  ]
}

```

In the above pipeline i am converting field1, field5, field6 into string type.

Step 4: Created the second index with the mapping -

```auto
PUT /test_field_type_2/
{
    "mappings" : {
        "properties" : {
            "field1" : { 
              "type" : "text" 
            },
            "field2" : { 
              "type" : "text" 
            },
            "field3" : { 
              "type" : "float" 
            },
            "field4" : { 
              "type" : "double" 
            },
            "field5" : { 
              "type" : "text" 
            },
            "field6" : { 
              "type" : "text" 
            }
        }
    }
}

```

As you can notice the type of field1, field5, field6 is "Text" type where as in the first index the type of field1, field5, field6 is an integer, float, Boolean respectively.

Step 5: Now with the reindex API, i am coping the index "test\_field\_type" document into the new index "test\_field\_type\_2" -

```auto
POST _reindex
{
  "source": {
    "index": "test_field_type"
  },
  "dest": {
    "index": "test_field_type_2",
    "pipeline": "string_convert_pipline"
  }
}

```

and I have provided the pipeline to convert the data type while reindexing.

Step 6: When you search the "test\_field\_type\_2" the document will look like this-

```auto
GET /test_field_type_2/_search

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test_field_type_2",
        "_type" : "_doc",
        "_id" : "vB378nABNcAqCQrXDvLF",
        "_score" : 1.0,
        "_source" : {
          "field1" : "111",
          "field6" : "true",
          "field3" : 11.1,
          "field2" : "sample_text1",
          "field5" : "9876543231",
          "field4" : 1111.1002
        }
      }
    ]
  }
}

```

As you can notice field1, field5, field6 converted into the string type.

Now let's say I have one field in the index "skillset"

```auto
"skillset": ["node.js", "mongodb", "mysql"]

```

what I want to achieve is, while reindexing I want to convert "skillset" into  
an array of an object like this-

```auto
"skillset": [{
		"name": "node.js"
	}, {
		"name": "mongodb"
	}, {
		"name": "mysql"
	}]

```

So is there any way I can do that using elastic search reindex and pipeline functionality?

---

<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:** [March 23, 2020, 8:10am UTC](https://discuss.elastic.co/t/convert-field-data-type-while-re-indexing-the-index-in-elasticsearch/224378/6 "2020-03-23T08:10:11Z")

</div>

I see. Thanks for the detailed explanation.

I believe that you need to use an ingest script processor in such complex case.

---

<div class="post-metadata">

**Author:** ![avinash\_kumawat](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/avinash_kumawat/32/44393_2.png) [@avinash\_kumawat](https://discuss.elastic.co/u/avinash_kumawat)\
**Post date:** [March 23, 2020, 9:28am UTC](https://discuss.elastic.co/t/convert-field-data-type-while-re-indexing-the-index-in-elasticsearch/224378/7 "2020-03-23T09:28:46Z")

</div>

Thanks for the quick reply, I will try out ingest script processor.

---

<div class="post-metadata">

**Author:** ![avinash\_kumawat](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/avinash_kumawat/32/44393_2.png) [@avinash\_kumawat](https://discuss.elastic.co/u/avinash_kumawat)\
**Post date:** [March 23, 2020, 9:56am UTC](https://discuss.elastic.co/t/convert-field-data-type-while-re-indexing-the-index-in-elasticsearch/224378/8 "2020-03-23T09:56:46Z")

</div>

Hello David,

Thanks i have tried the ingest script processor and this worked for me

Step 1

```auto

PUT _ingest/pipeline/array_string_to_array_object
{
    "description": "convert an array of string into an array of an object",
    "processors": [
      {
        "script": {
          "source": """
            def skillset_new = []; 
            for (def i = 0; i < ctx.skillset.length; i++) {
              def obj = [params.key_name: ctx.skillset[i]];
              skillset_new.add(obj);
            }
            ctx.skillset = skillset_new;
          """,
          "params": {
            "key_name":"name"
          }
        }
      }
    ]
}

```

Step 2: Using the \_simulate API

```auto
POST /_ingest/pipeline/array_string_to_array_object/_simulate
{
  "docs": [
    {
      "_index": "skills",
      "_source": {
        "skillset":["node.js", "mongodb", "mysql"]
      }
    }
  ]
}

```

Result:-

```auto
{
  "docs" : [
    {
      "doc" : {
        "_index" : "skills",
        "_type" : "_doc",
        "_id" : "_id",
        "_source" : {
          "skillset" : [
            {
              "name" : "node.js"
            },
            {
              "name" : "mongodb"
            },
            {
              "name" : "mysql"
            }
          ]
        },
        "_ingest" : {
          "timestamp" : "2020-03-23T09:55:34.2929144Z"
        }
      }
    }
  ]
}

```

Regards,  
Avinash Kumawat

---

<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:** [March 23, 2020, 10:07am UTC](https://discuss.elastic.co/t/convert-field-data-type-while-re-indexing-the-index-in-elasticsearch/224378/9 "2020-03-23T10:07:44Z")

</div>

Amazing. Thanks for sharing your solution!

---

<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 20, 2020, 10:07am UTC](https://discuss.elastic.co/t/convert-field-data-type-while-re-indexing-the-index-in-elasticsearch/224378/10 "2020-04-20T10:07:44Z")

</div>

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