# How to update a field type of existing index in Elasticsearch

**URL:** https://discuss.elastic.co/t/how-to-update-a-field-type-of-existing-index-in-elasticsearch/53892
**Category:** Elasticsearch
**Created:** [June 24, 2016, 1:57pm UTC](https://discuss.elastic.co/t/how-to-update-a-field-type-of-existing-index-in-elasticsearch/53892 "2016-06-24T13:57:13Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![loez1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/loez1/32/9274_2.png) [@loez1](https://discuss.elastic.co/u/loez1)
#### Post date: [June 24, 2016, 1:57pm UTC](https://discuss.elastic.co/t/how-to-update-a-field-type-of-existing-index-in-elasticsearch/53892/1 "2016-06-24T13:57:13Z")

</div>

Hi,

I need to change field type, but I don't know how knowing that I'm using logstash for importing data from CSV file so I didn't specify the field type manually and I think the default value is a string, but I need to change it to long or integer, what is the command that allows updating mapping settings or at least fields type?

Thank you!

---

<div class="post-metadata">

### Author: ![Josh\_J\_Luo](https://avatars.discourse-cdn.com/v4/letter/j/dbc845/32.png) [@Josh\_J\_Luo](https://discuss.elastic.co/u/Josh_J_Luo)
#### Post date: [June 24, 2016, 2:09pm UTC](https://discuss.elastic.co/t/how-to-update-a-field-type-of-existing-index-in-elasticsearch/53892/2 "2016-06-24T14:09:35Z")

</div>

You cannot do that. If you want to force a non-default data type, in most cases you need to explicitly specify it in the mapping definition (there are some exception such as geo\_point, which can be recognized by name)

---

<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: [June 24, 2016, 2:12pm UTC](https://discuss.elastic.co/t/how-to-update-a-field-type-of-existing-index-in-elasticsearch/53892/3 "2016-06-24T14:12:23Z")

</div>

You could change the logstash index template and provide your own mapping.

Or create manually a mapping (before sending the first data): [https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html)

If you already indexed docs, you can't change the mapping so you will need to reindex.

---

<div class="post-metadata">

### Author: ![loez1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/loez1/32/9274_2.png) [@loez1](https://discuss.elastic.co/u/loez1)
#### Post date: [June 24, 2016, 2:56pm UTC](https://discuss.elastic.co/t/how-to-update-a-field-type-of-existing-index-in-elasticsearch/53892/4 "2016-06-24T14:56:56Z")

</div>

I tried to create manually a mapping when I check the result it seems working even with having some weirds warnings when I use logstash command but concretly it behaves like a string I mean when I try to do for exemple 1+1 (1 is my field value) it returns 11 not 2 !!!!

---

<div class="post-metadata">

### Author: ![Josh\_J\_Luo](https://avatars.discourse-cdn.com/v4/letter/j/dbc845/32.png) [@Josh\_J\_Luo](https://discuss.elastic.co/u/Josh_J_Luo)
#### Post date: [June 24, 2016, 3:03pm UTC](https://discuss.elastic.co/t/how-to-update-a-field-type-of-existing-index-in-elasticsearch/53892/5 "2016-06-24T15:03:55Z")

</div>

Where did you try your 1+1, in a script?

---

<div class="post-metadata">

### Author: ![loez1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/loez1/32/9274_2.png) [@loez1](https://discuss.elastic.co/u/loez1)
#### Post date: [June 24, 2016, 3:10pm UTC](https://discuss.elastic.co/t/how-to-update-a-field-type-of-existing-index-in-elasticsearch/53892/6 "2016-06-24T15:10:26Z")

</div>

Exactly ! I'm tring to do this

```
POST /{index}/{type}/{id}/_update
{
    "script" : "ctx._source.{field}+=1"
}
```

---

<div class="post-metadata">

### Author: ![Josh\_J\_Luo](https://avatars.discourse-cdn.com/v4/letter/j/dbc845/32.png) [@Josh\_J\_Luo](https://discuss.elastic.co/u/Josh_J_Luo)
#### Post date: [June 24, 2016, 3:33pm UTC](https://discuss.elastic.co/t/how-to-update-a-field-type-of-existing-index-in-elasticsearch/53892/7 "2016-06-24T15:33:20Z")

</div>

I would first check GET /{index}/{type}/\_mapping to see if the field is indeed long

---

<div class="post-metadata">

### Author: ![loez1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/loez1/32/9274_2.png) [@loez1](https://discuss.elastic.co/u/loez1)
#### Post date: [June 24, 2016, 3:52pm UTC](https://discuss.elastic.co/t/how-to-update-a-field-type-of-existing-index-in-elasticsearch/53892/8 "2016-06-24T15:52:53Z")

</div>

I used something like that

```
PUT {index}
{
"mappings": {
    "{type}" : {
        "properties" : {
            {field} : {"type" : "long"}
        }
    }
}
}
```

---

<div class="post-metadata">

### Author: ![Josh\_J\_Luo](https://avatars.discourse-cdn.com/v4/letter/j/dbc845/32.png) [@Josh\_J\_Luo](https://discuss.elastic.co/u/Josh_J_Luo)
#### Post date: [June 24, 2016, 3:58pm UTC](https://discuss.elastic.co/t/how-to-update-a-field-type-of-existing-index-in-elasticsearch/53892/9 "2016-06-24T15:58:33Z")

</div>

You should check if the field type has changed. You are not able to change a field type if it is already there. For example if you put a data entry without defining a field mapping, a field mapping with default type will be created and you cannot change it. You have to delete and recreate the index

---

<div class="post-metadata">

### Author: ![loez1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/loez1/32/9274_2.png) [@loez1](https://discuss.elastic.co/u/loez1)
#### Post date: [June 24, 2016, 4:10pm UTC](https://discuss.elastic.co/t/how-to-update-a-field-type-of-existing-index-in-elasticsearch/53892/10 "2016-06-24T16:10:17Z")

</div>

Yes that exactly what I did ! I deleted my index and I created the mapping and then I sended the data from CSV file with logstash. that seems working when I check the new type but logstash display a warning

`"error"=>"WriteFailureException; nested: MapperParsingException[failed to parse [field]]; nested: NumberFormatException[For input string: \"field\"]; "}}, :level=>:warn}`

---

<div class="post-metadata">

### Author: ![loez1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/loez1/32/9274_2.png) [@loez1](https://discuss.elastic.co/u/loez1)
#### Post date: [June 29, 2016, 9:30am UTC](https://discuss.elastic.co/t/how-to-update-a-field-type-of-existing-index-in-elasticsearch/53892/11 "2016-06-29T09:30:26Z")

</div>

@dadoonet How can I change the logstash file conf to include fields type knowing that i'm using this syntax :

```
 input {
  file {
    type => "............."
	path => ..........
	start_position => "beginning"
	sincedb_path => "/dev/null"
  }
}

filter {
	if [type] == ".........." {
		if [message] =~ /^COL1,COL2,COL3/ {
			drop { }
		}
	  	csv {
		    	columns => ["COL1","COL2","COL3"]
		    	separator => ";"
		    	source => message
		    	remove_field => ["message","host","path","@version","@timestamp"]
		}
	}
}
 
output {

  	if [type] == ".........." {
  	    elasticsearch {
		    hosts => "localhost:9200"
		    index => ".........."
		    document_type => ".........."
	  	}
  	}
  	
}

```

Thank you !

---

<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: [June 29, 2016, 9:44am UTC](https://discuss.elastic.co/t/how-to-update-a-field-type-of-existing-index-in-elasticsearch/53892/12 "2016-06-29T09:44:31Z")

</div>

This is more a question for #logstash group but here we go: [https://www.elastic.co/guide/en/logstash/2.3/plugins-outputs-elasticsearch.html#plugins-outputs-elasticsearch-template](https://www.elastic.co/guide/en/logstash/2.3/plugins-outputs-elasticsearch.html#plugins-outputs-elasticsearch-template)

---

<div class="post-metadata">

### Author: ![loez1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/loez1/32/9274_2.png) [@loez1](https://discuss.elastic.co/u/loez1)
#### Post date: [June 30, 2016, 11:55am UTC](https://discuss.elastic.co/t/how-to-update-a-field-type-of-existing-index-in-elasticsearch/53892/13 "2016-06-30T11:55:18Z")

</div>

Thank you very much for your time ! Indeed I found the solution here

[https://www.elastic.co/guide/en/logstash/current/plugins-filters-mutate.html#plugins-filters-mutate-convert](https://www.elastic.co/guide/en/logstash/current/plugins-filters-mutate.html#plugins-filters-mutate-convert)

and here

[https://www.elastic.co/guide/en/logstash/current/plugins-filters-csv.html#plugins-filters-csv-convert](https://www.elastic.co/guide/en/logstash/current/plugins-filters-csv.html#plugins-filters-csv-convert)

---

<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 5, 2017, 10:39pm UTC](https://discuss.elastic.co/t/how-to-update-a-field-type-of-existing-index-in-elasticsearch/53892/14 "2017-07-05T22:39:14Z")

</div>


