Elasticsearch mapping - different data types in same field Elasticsearch mapping - different data types in same field elasticsearch elasticsearch

Elasticsearch mapping - different data types in same field


You actually can index multiple datatypes into the same field using a multi-field mapping and the ignore_malformed parameter, if you are willing to query the specific field type if you want to do type specific queries (like comparisons).

This will allow elasticsearch to populate the fields that are pertinent for each input, and ignore the others. It also means you don’t need to do anything in your indexing code to deal with the different types.

For example, for a field called user_input that you want to be able to do date or integer range queries over if that is what the user has entered, or a regular text search if the user has entered a string, you could do something like the following:

PUT multiple_datatypes{  "mappings": {    "_doc": {      "properties": {        "user_input": {          "type": "text",          "fields": {            "numeric": {              "type": "double",              "ignore_malformed": true            },            "date": {              "type": "date",              "ignore_malformed": true            }          }        }      }    }  }}

We can then add a few documents with different user inputs:

PUT multiple_datatypes/_doc/1{  "user_input": "hello"}PUT multiple_datatypes/_doc/2{  "user_input": "2017-02-12"}PUT multiple_datatypes/_doc/3{  "user_input": 5}

And when you search for these, and have ranges and other type-specific queries work as expected:

// Returns only document 2GET multiple_datatypes/_search{  "query": {    "range": {      "user_input.date": {        "gte": "2017-01-01"      }    }  }}// Returns only document 3GET multiple_datatypes/_search{  "query": {    "range": {      "user_input.numeric": {        "lte": 9      }    }  }}// Returns only document 1GET multiple_datatypes/_search{  "query": {    "term": {      "user_input": {        "value": "hello"      }    }  }}

I wrote about this as a blog post here


No - you cannot have different datatypes for the same field within the same type.

e.g. the field index/type/value can not be both a string and a date.


A dynamic template can be used to set the datatype and analyzer based on the format of the field name

For example:set all fields with field names ending in "_dt" to type datetime.

But this won't help in your scenario, once the datatype is set you can't change it.