You could consider elasticsearch to be schema-less. And when just exploring elasticsearch this is great! You can index documents containing different fields without having to worry about the different fields. If the field does not exist yet in elasticsearch, it is automatically added to the ‘mapping’ <= this is how a schema is called in es. But when you start doing a bit more advanced things, you have to be really careful with this ‘automatic mapping’. The default settings for a field that are defined by elasticsearch, might not always be exactly what you need. And changing for example a field from type string to type date requires you to reindex all documents. Unless, you just create a new field with a slightly different name. I guess this is the easiest way regarding to elasticsearch and as long as not a lot of coding for that field has been done, refactoring your code might not take that much time. Of course, in case all your documents contain/need a value for this field, they all still need to be reindexed…
But being sometimes a kind of a perfectionist, I don’t like to rename fields to something that I consider as not the right name for a field.
So there I was, having a field called “colors” as being automatically mapped as a string:
“colors”: {
“type”: “string”
}
But now I wanted to make a facet of it and a multi-field type would be more appropriate in my case.
A while ago, I had been reading the article “changing mapping with zero downtime” on the elasticsearch website. So I was already thinking of reindexing all the documents using an alias.
But I have learned that elasticsearch is continuously improving and to read the documentation again first before doing something I haven’t done before.
Especially because I was not so keen on having to reindex all documents again. Although it would have been a good practice in case it would be needed urgently somewhere in the future…
Anyway, I found myself really lucky when reading the following in the put mapping api of the es documentation:
“core type mapping can be upgraded to multi_field type”
So instead of spending time on reindexing documents, I just ran the following code and my problem was solved.
curl -XPUT ‘http://localhost:9200/clothes/doc/_mapping’ -d ‘
{
“doc” : {
“properties” : {
“colors”: {
“type”: “multi_field”,
“fields”: {
“colors”: {
“type”: “string”
},
“untouched”: {
“type”: “string”,
“index”: “not_analyzed”
}
}
}
}
}
}
‘
Great!
By the way: to view your mapping you have to use the _mapping command:
e.g curl -XGET curl -XGET ‘http://localhost:9200/clothes/doc/_mapping?pretty=true’
and I suggest you use the Chrome Sense plugin for this