Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch : map date as text?

I have json data that has a "product_ref" field that can take these values as an example:

"product_ref": "N/A"
"product_ref": "90323"
"product_ref": "SN3005"
"product_ref": "2015-05-23"

When pushing the data to the index i get a mapping error:

{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"mapper [root.product_ref] of different type, current_type [date], merged_type [text]"}],"type":"illegal_argument_exception","reason":"mapper [root.product_ref] of different type, current_type [date], merged_type [text]"},"status":400}

Any idea?

like image 595
Yacine Lazaar Avatar asked Feb 25 '18 11:02

Yacine Lazaar


2 Answers

There is something called date detection, and by default, it is enabled.

If date_detection is enabled (default), then new string fields are checked to see whether their contents match any of the date patterns specified in dynamic_date_formats. If a match is found, a new date field is added with the corresponding format.

You just need to disable it by modifying your mappings:

 PUT /products    

 {
  "mappings": {
     "doc": { 
        "date_detection": false, 
        "properties": { 
           "product_ref": { "type": "keyword"  }, 

         }
      }
   }
 }
like image 98
Eirini Graonidou Avatar answered Oct 10 '22 19:10

Eirini Graonidou


This is happening because ElasticSearch assumed you're indexing dates of a particular format, and a value which doesn't match that was attempted to be indexed. i.e. after indexing date, you index wrong format.

Make sure all the values are dates and none are empty,perhaps remove these in your ingestion layer.

EDIT: If you don't care to lose the date value you can use the dynamic mapping.

{
    "dynamic_templates": [
        {
            "integers": {
                "match_mapping_type": "date",
                "mapping": {
                    "type": "text"
                }
            }
        }
    ]
}
like image 33
aclowkay Avatar answered Oct 10 '22 19:10

aclowkay