Home > Net >  ElasticSearch automatically converting field type inside Java Map from String to date when saving
ElasticSearch automatically converting field type inside Java Map from String to date when saving

Time:05-13

I have the following index mapping - a field called customFields contains other fields such as firstName, lastName, etc.

{
  "customFields": {
    "properties": {
      "firstName": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "lastName": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "address": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "dateOfBirth": {
        "type": "date"
      },
    }
  }
}

In my Spring project, customFields is a Map<String, Object>.

The dateOfBirth field accepts values in the YYYY-MM-DD format and is a String.

However, when its being saved on ElasticSearch, the type becomes date. I have been debugging and I haven't found anywhere in the application where the dateOfBirth type changes to date before being sent to ElasticSearch.

Is it possible ElasticSearch changes the type by itself in the background?

CodePudding user response:

You are not defining the Elasticsearch mapping explicitly for these fields, and when Elasticsearch see them it tries to match them with best data types, as your dateOfBirth is having a YYYY-MM-DD even if its enclosed in "" and you want them to be a text field, Elasticsearch maps it to date field due to its value.

if you want to avoid this, you need to define dateOfBirth explicitly with text data type in your Elasticsearch index mapping.

Hope this helps.

  • Related