我正在尝试在Elasticsearch中创建一个嵌套文档。
结构:
title,name,comments
以下是下面提到的查询。
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
}
任何人都可以帮忙吗?
答案 0 :(得分: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
}
您需要更新映射以包含“text”属性,并相应地移动"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
}
答案 1 :(得分:0)
如果您使用的是ElasticSearch更高版本,则建议将“字符串”数据类型替换为“文本”。 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"
}
}
}
}
}`