Search code examples
elasticsearchelasticsearch-mapping

How to create a sub object in Elastic-search 7.x


Earlier I was using 1.x version and was creating the sub objects mapping using below syntax.

"foo": {
            "type": "integer",
            "doc_values": true
        },
"foo.bar": {
            "type": "integer",
            "doc_values": true
        },
"foo.bar.baz": {
            "type": "integer",
            "doc_values": true
        },

But now when I am using same mapping syntax in ES 7.x I am getting below error:-

{
    "error": {
        "root_cause": [
            {
                "type": "illegal_argument_exception",
                "reason": "Can't merge a non object mapping [foo] with an object mapping [foo]"
            }
        ],
        "type": "illegal_argument_exception",
        "reason": "Can't merge a non object mapping [foo] with an object mapping [foo]"
    },
    "status": 400
}

I am came accros this SO post Can’t merge a non object mapping with an object mapping error in machine learning(beta) module But, Note I am not updating the mapping, instead I am crating a new mapping still getting this error, please advise what to do?


Solution

  • An example of object type. Refer here for more info

    "mappings": {
        "properties": { 
          "user": { 
            "properties": {
              "age":  { "type": "integer" },
              "name": { 
                "properties": {
                  "first": { "type": "text" },
                  "last":  { "type": "text" }
                }
              }
            }
          }
        }
    

    In below name can be defined as object and further properties can be added using name.firstname etc. In your mapping foo is of type integer and then you are adding foo.bar so it is throwing error. foo must be of type object.

    "properties" : {
                "name" : {
                    "type" : "object"
                },
                "name.firstname":{
                  "type" : "text"
                },
                "name.lastname":{
                  "type" : "text"
                }
            }