Home > Software engineering >  Elasticsearch mapping - Different types of data in same field
Elasticsearch mapping - Different types of data in same field

Time:06-19

I am trying to create a mapping for my Elasticsearch that would make me insert an both an object 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"
            }]
        }
    ]
  }

Would it be possible for me to have two types of data in the same field? I am running Elasticsearch version 7.3.1.

CodePudding user response:

It's not possible. You will have to create two fields, a text field "value" and another nested "value" field (e.g. "value_txt" and "value_nested").

I don't know if your application can model the data to generate these two fields before indexing but if it is not possible you will have to create a script to check if the "value" is a String type, if it is you will set the value in the field " value_txt", otherwise in "value_nested".

CodePudding user response:

you can use the object type
https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html#:~:text=object,A JSON object.
it should be able to accept both the type not sure about the querying part

  • Related