Elasticsearch mapping - Different types of data in same field

I am trying to create a mapping for my Elasticsearch that would make me insert both an array and a string into my field "value". The following shows the data I want inserted:

  {
    "Data": [{
            "key": "keyName",
            "value": "valueName"
        },
        {
            "key": "keyName",
            "value": "valueName"
        },
        {
            "key": "keyName",
            "value": [{
                "type": "FailedName",
                "message": "FailedMessage",
                "path": "FailedPath"
            }]
        }
    ]
  }

I am running into this error when inserting the mentioned data:

{
  "error": {
    "root_cause": [
      {
        "type": "mapper_parsing_exception",
        "reason": "failed to parse field [customData.value] of type [text] in document with id '0ltFcoEBP0oW3Bg9b2S_'. Preview of field's value: '{path=FailedPath, type=FailedName, message=FailedMessage}'"
      }
    ],
    "type": "mapper_parsing_exception",
    "reason": "failed to parse field [customData.value] of type [text] in document with id '0ltFcoEBP0oW3Bg9b2S_'. Preview of field's value: '{path=FailedPath, type=FailedName, message=FailedMessage}'",
    "caused_by": {
      "type": "illegal_state_exception",
      "reason": "Can't get text on a START_OBJECT at 8:23"
    }
  },
  "status": 400
}

We have looked into the documentation and seems like this is not supported. I am now asking for some advice on how I could make this work.

I am running Elasticsearch version 7.3.1.

Here are some links that may be interesting to this topic:
Add Array/Object support for elasticsearch connector via _meta field mapping

Elasticsearch version 7.3 is EOL and no longer supported. Please upgrade ASAP.

(This is an automated response from your friendly Elastic bot. Please report this post if you have any suggestions or concerns :elasticheart: )

Hi,
The problem here is that you try to index a document which has 2 different types for the same field (Data.value). First it is encoutered as String, and then as an object.
Elasticsearch has a dynamically guessed mapping by default, but it does define the schema, and you cannot have the same field mapped both as object and as a core (scalar) type.
So you can either :

  • change your data model and give the field another name when it contains an array or object, so each field has a consistent type.
  • ignore the field value in the index (the document will still be stored and retrieved completely, but you wont be able to run queries based on the value). Sample mapping for this:
PUT testindex
{
  "mappings": {
    "properties": {
      "Data": {
        "type": "object",
        "properties": {
          "value": {
            "enabled": false
          }
        }
      }
    }
  }
}
2 Likes