Skip to content

Instantly share code, notes, and snippets.

@ijokarumawak
Last active June 22, 2021 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ijokarumawak/bc5c3bb20c656422196284a2381f3043 to your computer and use it in GitHub Desktop.
Save ijokarumawak/bc5c3bb20c656422196284a2381f3043 to your computer and use it in GitHub Desktop.
Can we turn off coercing without having to reindex? Yes, we can.

Create the num field as long with auto-mapping.

PUT coerce_test/_doc/1
{
  "num": 1
}

By default, coerce is true. That means a string value which can be converted into a number, can be ingested.

PUT coerce_test/_doc/2
{
  "num": "2"
}

Changing coerce of an existing field is possible

POST coerce_test/_mapping
{
  "properties": {
    "num": {
      "type": "long",
      "coerce": false
    }
  }
}

After that, string values will be rejected.

PUT coerce_test/_doc/3
{
  "num": "3"
}

Rejected result:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "mapper_parsing_exception",
        "reason" : "failed to parse field [num] of type [long] in document with id '3'. Preview of field's value: '3'"
      }
    ],
    "type" : "mapper_parsing_exception",
    "reason" : "failed to parse field [num] of type [long] in document with id '3'. Preview of field's value: '3'",
    "caused_by" : {
      "type" : "illegal_argument_exception",
      "reason" : "Long value passed as String"
    }
  },
  "status" : 400
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment