Elasticsearch中的嵌套类型:“索引文档时,无法将对象映射从嵌套更改为非嵌套”

时间:2016-04-12 12:16:06

标签: elasticsearch nested mapping

我尝试将一些嵌套文档索引到Elasticsearch(v2.3.1)映射中,该映射如下所示(based on this example from the documentation)

PUT /my_index
{
  "mappings": {
    "blogpost": {
      "properties": {
        "title": { "type": "string" },
        "comments": {
          "type": "nested", 
          "properties": {
            "name":    { "type": "string"  },
            "comment": { "type": "string"  }
          }
        }
      }
    }
  }
}

但是,我不明白我的JSON文档必须是什么样才能适应该映射。我试过

PUT /my_index/some_type/1
{
  "title": "some_title",
  "comments": {
    "name": "some_name",
    "comment": "some_comment"
  }
}

以及

PUT /my_index_some_type/1
{
  "title": "some_title",
  "comments": [
      {
        "name": "some_name",
        "comment": "some_comment"
      }
  ]
}

两者都导致

{

    "error": 

{

    "root_cause": 

[

            {
                "type": "remote_transport_exception",
                "reason": "[Caiman][172.18.0.4:9300][indices:data/write/index[p]]"
            }
        ],
        "type": "illegal_argument_exception",
        "reason": "object mapping [comments] can't be changed from nested to non-nested"
    },
    "status": ​400

}

索引嵌套文档的正确格式是什么?非常感谢任何工作示例,SO或其他页面上的大多数示例都集中在嵌套查询上,而不是之前如何对文档编制索引。

1 个答案:

答案 0 :(得分:17)

您似乎真的在创建some_type类型的文档,而comments默认为正常object(即不是nested),这是不允许的您已在同一索引中的comments映射类型中有一个名为blogpost的嵌套对象。

尝试使用它,它应该可以工作:

PUT /my_index/blogpost/1
{
  "title": "some_title",
  "comments": {
    "name": "some_name",
    "comment": "some_comment"
  }
}