Elasticsearch嵌套父子映射

时间:2016-09-22 09:24:34

标签: elasticsearch elasticsearch-2.0

我想映射以下结构: - 我有博文 - 博客帖子可以有评论 - 注释可以有回复(也是注释),因此它应该是递归数据结构

POST ----- * - > COMMENT

评论----- * ---> COMMENT

这是我试过的:

n' ^ (1/p) = n

当然它不起作用。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

您试图像在OO编程中那样声明类型,而不是它在ES中的工作方式。您需要使用下面的parent-child relationships,即post没有名为comments的字段,但comment映射类型具有_parent元字段引用post父类型。

另外,为了对回复进行建模,我建议只使用另一个名为in_reply_to的字段,该字段将包含回复所涉及的注释的ID。那样容易多了!

PUT blogs
{
  "mappings": {
    "post": {
      "properties": {
        "title": { "type": "string"}
      }
    },
    "comment": {
      "_parent": {
        "type": "post" 
      },
      "properties": {
        "id": { 
          "type": "long"
        }
        "content": { 
          "type": "string"
        },
        "in_reply_to": { 
          "type": "long"
        }
      }
    }
  }
}

答案 1 :(得分:0)

 mappings: {
    "post": {
        "properties": {
            "content": { "type": "string" },
            "comment": {
                "properties" : {
                    "content": { "type": "string" },
                    "replies": {
                      "properties" : {
                          "content": { "type": "string" }
                      }
                  }
        }
    }
}
相关问题