2

I'm trying to create a nested document in Elasticsearch.

Structure:

title,name,comments 
  • comments is a nested document - inside that - Comment & Star_Rating.
  • Inside Comment, name and address.

Here is the query mentioned below.

PUT /sounduu
    {
    "mappings": {
        "blogpost": {
            "properties": {
                "title": {
                    "type": "string"
                },
                "name": {
                    "type": "string"
                },
                "comments": {
                    "properties": {
                        "comment": {
                            "properties": {
                                "name": {
                                    "type": "string"
                                },
                                "address": {
                                    "type": "string"
                                }
                            }
                        },
                        "star_rating": {
                            "type": "long"
                        }
                    }
                }
            }
        }
    }
}


PUT /sounduu/blogpost/1
{
    "title": "someh_title",
    "name":"soundy",
    "comments": {
        "comment":"kuu",
        [{
             "name":"juwww",
             "address":"eeeey"
         },
         {
             "name":"jj",
             "address":oo"
        }]
    },
    "star_rating":6
}

Error :

{
   "error": {
      "root_cause": [
         {
            "type": "mapper_parsing_exception",
            "reason": "object mapping for [comments.comment] tried to parse field [comment] as object, but found a concrete value"
         }
      ],
      "type": "mapper_parsing_exception",
      "reason": "object mapping for [comments.comment] tried to parse field [comment] as object, but found a concrete value"
   },
   "status": 400
}

Can anyone help with this?

slm
  • 15,396
  • 12
  • 109
  • 124
Soundarya Thiagarajan
  • 574
  • 2
  • 13
  • 31

2 Answers2

0

In your PUT /sounduu/blogpost/1 request you are attempting to treat the "comment" property as both a nested object and a string.

Formatting your request's JSON, you can observe the issue:

{
    "title": "someh_title",
    "name": "soundy",
    "comments": {
        "comment": "kuu",
        [{
            "name": "juwww",
            "address": "eeeey"
        },
        {
            "name": "jj",
            "address": oo"
        }]
    },
    "star_rating":6
}

You either need to update your mapping to include a "text" property, and move the "comment": "kuu" content accordingly, or omit it from your request to work with your current mapping.

Example here - For me it seems logical to group everything like so:

PUT /sounduu
    {
    "mappings": {
        "blogpost": {
            "properties": {
                "title": {
                    "type": "string"
                },
                "name": {
                    "type": "string"
                },
                "comments": {
                    "properties": {
                        "text" : {
                            "type": "string"
                        },
                        "name": {
                            "type": "string"
                        },
                        "address": {
                            "type": "string"
                        }
                    }
                },
                "star_rating": {
                    "type": "long"
                }
            }
        }
    }
}

The indexing request would then look like:

{
    "title": "someh_title",
    "name": "soundy",
    "comments": [
        {
            "text": "kuu",
            "name": "juwww",
            "address": "eeeey"
        },
        {
            "text": "kuu2",
            "name": "jj",
            "address": oo"
        }
    ],
    "star_rating":6
}
ryanlutgen
  • 2,951
  • 1
  • 21
  • 31
0

If you are using elasticSearch higher version, then it is recommended to replace 'string' data type with 'text'. ElasticSearch community has discarded 'string'.

Reformed request should be :

 `PUT /sounduu
    {
    "mappings": {
        "blogpost": {
            "properties": {
                "title": {
                    "type": "text"
                },
                "name": {
                    "type": "text"
                },
                "comments": {
                    "properties": {
                        "text" : {
                            "type": "text"
                        },
                        "name": {
                            "type": "text"
                        },
                        "address": {
                            "type": "text"
                        }
                    }
                },
                "star_rating": {
                    "type": "long"
                }
                }
            }
        }
    }`
Akash Roy
  • 398
  • 5
  • 11