简体   繁体   English

使用父子关系重新索引Elasticsearch索引

[英]Reindexing Elasticsearch index with parent and child relationship

we currently have a 'message' that can have a link to a 'parent' message. 我们目前有一条“消息”,可以链接到“父”消息。 Eg a reply would have the original message as the parent_id. 例如,回复将原始消息作为parent_id。

PUT {
  "mappings": {
    "message": {
      "properties": {
        "subject": {
          "type": "text"
         },
         "body" : {
            "type" : "text"
         },
         "parent_id" : {
            "type" : "long"
          }
        }
      }
    }
  }
}

Currently we didn't have an elasticsearch parent child join on the document as parent and child weren't allowed to be of the same type. 目前,我们没有弹性搜索父子加入文档,因为父级和子级不允许属于同一类型。 Now with 5.6 and the drive by elastic to get rid of types we are now trying to use the new parent and child join in 5.6 which. 现在用5.6和弹性驱动器来摆脱类型我们现在正试图使用​​新的父和子加入5.6中。

PUT {
  "settings": {
    "mapping.single_type": true
  },
  "mappings": {
    "message": {
      "properties": {
        "subject": {
          "type": "text"
         },
         "body" : {
            "type" : "text"
         },
         "join_field": {
            "type" : "join",
            "relations": {
                "parent_message":"child_message"
            }
        }
        }
      }
    }
  }
}

I know I will have to create a new index for this and then reindex everything with _reindex but I am not quite sure how I would do that. 我知道我必须为此创建一个新索引,然后用_reindex重新索引所有内容,但我不太确定我会怎么做。

If I index a parent_message it is simple 如果我索引parent_message很简单

PUT localhost:9200/testm1/message/1 
{
        "subject": "Message 1",
         "body" : "body 1"
}
PUT localhost:9200/testm1/message/3?routing=1
{
        "subject": "Message Reply to 1",
         "body" : "body 3",
          "join_field": {
            "name": "child_message",
            "parent": "1"
    }
 }

A search would now return 搜索现在将返回

{
                "_index": "testm1",
                "_type": "message",
                "_id": "2",
                "_score": 1,
                "_source": {
                    "subject": "Message 2",
                    "body": "body 2"
                }
            },
            {
                "_index": "testm1",
                "_type": "message",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "subject": "Message 1",
                    "body": "body 1"
                }
            },
            {
                "_index": "testm1",
                "_type": "message",
                "_id": "3",
                "_score": 1,
                "_routing": "1",
                "_source": {
                    "subject": "Message Reply to 1",
                    "body": "body 3",
                    "join_field": {
                        "name": "child_message",
                        "parent": "1"
                    }
                }
            }

I tried to create the new index (testmnew) and then just do a _reindex 我试图创建新索引(testmnew)然后只做一个_reindex

POST _reindex
{
    "source": {
        "index" : "testm"
    },
    "dest" :{
        "index" : "testmnew"
    },
    "script" : {
        "inline" : """
        ctx._routing = ctx._source.parent_id;
 --> Missing need to set join_field here as well I guess <--
        """
        }
}

The scripting is still not quite clear to me. 脚本编写对我来说仍然不太清楚。 But am I on the right path here? 但我在正确的道路上吗? Would I simply set the _routing on the messages (would be null on parent messages). 我是否只需在消息上设置_routing(在父消息上为null)。 But how would I set the join_field only for child messages? 但是,我如何仅为子消息设置join_field?

This is the re-indexing script that I've used in the end: 这是我最后使用的重新索引脚本:

curl -XPOST 'localhost:9200/_reindex' -H 'Content-Type: application/json' -d'
{
    "source": {
        "index" : "testm"
    },
    "dest" :{
        "index" : "testmnew"
    },
    "script" : {
        "lang" : "painless",
        "source" : "if(ctx._source.parent_id != null){ctx._routing = ctx._source.parent_id; ctx._source.join_field=  params.cjoin; ctx._source.join_field.parent = ctx._source.parent_id;}else{ctx._source.join_field = params.parent_join}",
        "params" : {
            "cjoin" :{
                "name": "child_message",
                "parent": 1
            },
            "parent_join" : {"name": "parent_message"}

        }
    }
}
'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM