2

我正在尝试在 Elasticsearch 中创建一个嵌套文档。

结构:

title,name,comments 
  • 评论是一个嵌套文档 - 在里面 - 评论和 Star_Rating。
  • 内部评论,姓名和地址。

这是下面提到的查询。

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": {
      "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
}

有人能帮忙吗?

4

2 回答 2

0

在您的PUT /sounduu/blogpost/1请求中,您试图将“comment”属性视为嵌套对象和字符串。

格式化请求的 JSON,您可以观察到问题:

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

您要么需要更新映射以包含“文本”属性,并"comment": "kuu"相应地移动内容,要么从您的请求中省略它以使用当前映射。

此处的示例-对我来说,将所有内容分组似乎是合乎逻辑的:

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"
                }
            }
        }
    }
}

索引请求将如下所示:

{
    "title": "someh_title",
    "name": "soundy",
    "comments": [
        {
            "text": "kuu",
            "name": "juwww",
            "address": "eeeey"
        },
        {
            "text": "kuu2",
            "name": "jj",
            "address": oo"
        }
    ],
    "star_rating":6
}
于 2016-10-12T19:35:13.117 回答
0

如果您使用的是 elasticSearch 更高版本,则建议将 'string' 数据类型替换为 'text'。ElasticSearch 社区已丢弃“字符串”。

改革后的要求应该是:

 `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"
                }
                }
            }
        }
    }`
于 2019-11-12T09:05:49.083 回答