简体   繁体   中英

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.

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.

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.

If I index a parent_message it is simple

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

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). But how would I set the join_field only for child messages?

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

        }
    }
}
'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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