# Command to change fieldtype of an index

**URL:** <https://discuss.elastic.co/t/command-to-change-fieldtype-of-an-index/186275>\
**Category:** Elasticsearch\
**Created:** [June 18, 2019, 2:08pm UTC](https://discuss.elastic.co/t/command-to-change-fieldtype-of-an-index/186275 "2019-06-18T14:08:43Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![pathri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pathri/32/54484_2.png) [@pathri](https://discuss.elastic.co/u/pathri)\
**Post date:** [June 18, 2019, 2:08pm UTC](https://discuss.elastic.co/t/command-to-change-fieldtype-of-an-index/186275/1 "2019-06-18T14:08:43Z")

</div>

I am trying changing the fieldtype using of a field from string to integer and not able to find out to correct syntax.  
preferably using \_mapping API  
Please help

Thanks in advance

---

<div class="post-metadata">

**Author:** ![Dominic\_Page](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dominic_page/32/48318_2.png) [@Dominic\_Page](https://discuss.elastic.co/u/Dominic_Page)\
**Post date:** [June 18, 2019, 2:27pm UTC](https://discuss.elastic.co/t/command-to-change-fieldtype-of-an-index/186275/2 "2019-06-18T14:27:04Z")

</div>

Hi @pathri

Once you a field has a fieldtype you can't change this.

If you don't have data in the index, try this:

1. Get the mapping using

GET \<my\_index\>/\_mapping

and copy the JSON to a safe place

1. delete the index, see:

[https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-delete-index.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-delete-index.html)

(I gave a link to avoid accidental deletion)

1. Create the index using this API

[https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html)

you can modify the mapping you saved and use that in the create request.

Alternatively, if you already have data, you will need to create a new index, following steps 1 and 3

and then use the \_reindex API, see [https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html)

to transfer your data to the new index.

Hope this helps!

- Dom

---

<div class="post-metadata">

**Author:** ![pathri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pathri/32/54484_2.png) [@pathri](https://discuss.elastic.co/u/pathri)\
**Post date:** [June 18, 2019, 3:02pm UTC](https://discuss.elastic.co/t/command-to-change-fieldtype-of-an-index/186275/3 "2019-06-18T15:02:05Z")

</div>

thanks for your help Dom 🙂

But this is a big process and also the time consuming one.  
and we are aware that the reindex takes long time and if the data in the index will be in TBs then it is going to take much longer time ☹

no other way to handle this scenario, like using \_ingest API ??

---

<div class="post-metadata">

**Author:** ![Dominic\_Page](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dominic_page/32/48318_2.png) [@Dominic\_Page](https://discuss.elastic.co/u/Dominic_Page)\
**Post date:** [June 18, 2019, 4:36pm UTC](https://discuss.elastic.co/t/command-to-change-fieldtype-of-an-index/186275/4 "2019-06-18T16:36:10Z")

</div>

Hi @pathri

The \_ingest api will allow you to add a pipeline which can change the type of incoming data

What it won't do is allow you to change the already-existing fieldtype on the already existing field or fields. That's because existing indexes will already have data structures in place to store the original fieldtype.

You could also add (the same) pipeline to a reindex request

I understand your concern about the cost in time of a reindex. It will also require space. You might consider adding a query to a reindex to break it down into smaller operations

---

<div class="post-metadata">

**Author:** ![pathri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pathri/32/54484_2.png) [@pathri](https://discuss.elastic.co/u/pathri)\
**Post date:** [June 19, 2019, 6:52am UTC](https://discuss.elastic.co/t/command-to-change-fieldtype-of-an-index/186275/5 "2019-06-19T06:52:40Z")

</div>

alright 😃  
thanks for clearing all doubts,  
just 1 question left which is : i was going through ingest API documentation and saw that i can run the created pipeline on a particular document but is it possible to run it on a complete index in 1 go.

Simple example:  
i have created a pipeline to delete the "message" field from apache log, i am able to do it for a particular document but wanted to do for all documents, what should be the query for that?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![Dominic\_Page](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dominic_page/32/48318_2.png) [@Dominic\_Page](https://discuss.elastic.co/u/Dominic_Page)\
**Post date:** [June 19, 2019, 8:40am UTC](https://discuss.elastic.co/t/command-to-change-fieldtype-of-an-index/186275/6 "2019-06-19T08:40:17Z")

</div>

Hi @pathri

If you are deleting a field from an existing document or documents you can do this in-place in the same index using the "Update by Query" API, see

[https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html)

If you don't specify a query to scope the operation, this will act on ALL the documents in the index.

You can specify a pipeline on the command line, for example:

`POST twitter/_update_by_query?pipeline=set-foo``

and this will run the pipeline on all the documents in the index

---

<div class="post-metadata">

**Author:** ![pathri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pathri/32/54484_2.png) [@pathri](https://discuss.elastic.co/u/pathri)\
**Post date:** [June 20, 2019, 10:11am UTC](https://discuss.elastic.co/t/command-to-change-fieldtype-of-an-index/186275/7 "2019-06-20T10:11:51Z")

</div>

cool 🙂

this query is working but getting time out  
{  
"statusCode": 504,  
"error": "Gateway Time-out",  
"message": "Client request timeout"  
}

this index is having 924851 documents in it and documents are nothing but apache log.

---

<div class="post-metadata">

**Author:** ![Dominic\_Page](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dominic_page/32/48318_2.png) [@Dominic\_Page](https://discuss.elastic.co/u/Dominic_Page)\
**Post date:** [June 20, 2019, 3:44pm UTC](https://discuss.elastic.co/t/command-to-change-fieldtype-of-an-index/186275/8 "2019-06-20T15:44:51Z")

</div>

Hi @pathri is this relating to an update\_by\_query?

---

<div class="post-metadata">

**Author:** ![Dominic\_Page](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dominic_page/32/48318_2.png) [@Dominic\_Page](https://discuss.elastic.co/u/Dominic_Page)\
**Post date:** [June 21, 2019, 7:26am UTC](https://discuss.elastic.co/t/command-to-change-fieldtype-of-an-index/186275/9 "2019-06-21T07:26:04Z")

</div>

Hi @pathri please can you raise this as a new Topic as I'll be out of touch in the short term

Cheers

---

<div class="post-metadata">

**Author:** ![pathri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pathri/32/54484_2.png) [@pathri](https://discuss.elastic.co/u/pathri)\
**Post date:** [June 21, 2019, 7:38am UTC](https://discuss.elastic.co/t/command-to-change-fieldtype-of-an-index/186275/10 "2019-06-21T07:38:01Z")

</div>

Hi @Dominic_Page: yes it was for the update query only

`POST twitter/_update_by_query?pipeline=delete-message`

sorry for the delayed response got stuck with something

---

<div class="post-metadata">

**Author:** ![Dominic\_Page](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dominic_page/32/48318_2.png) [@Dominic\_Page](https://discuss.elastic.co/u/Dominic_Page)\
**Post date:** [June 21, 2019, 7:58am UTC](https://discuss.elastic.co/t/command-to-change-fieldtype-of-an-index/186275/11 "2019-06-21T07:58:38Z")

</div>

Hi @pathri

you might want to take a look at this thread

> [@Update API timeout](https://discuss.elastic.co/t/update-api-timeout/152760/3):
>
> @spinscale i'm getting the following exception { "statusCode": 504, "error": "Gateway Time-out", "message": "Client request timeout" }

where the user had the same issue while using the same API

Hope this helps!

---

<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:** [July 19, 2019, 7:58am UTC](https://discuss.elastic.co/t/command-to-change-fieldtype-of-an-index/186275/12 "2019-07-19T07:58:40Z")

</div>

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