我们目前有一条消息'可以链接到父母'信息。例如。回复会将原始邮件作为parent_id。
PUT {
"mappings": {
"message": {
"properties": {
"subject": {
"type": "text"
},
"body" : {
"type" : "text"
},
"parent_id" : {
"type" : "long"
}
}
}
}
}
}
目前我们没有弹性搜索父子加入文件,因为父母和子女不允许属于同一类型。现在使用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"
}
}
}
}
}
}
}
我知道我必须为此创建一个新索引,然后用_reindex重新索引所有内容,但我不太确定我会怎么做。
如果我索引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"
}
}
现在将返回搜索
{
"_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"
}
}
}
我尝试创建新索引(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 <--
"""
}
}
脚本编写对我来说仍然不太清楚。但我在正确的道路上吗?我是否只需在消息上设置_routing(在父消息上为null)。但是,我如何仅为子消息设置join_field?
答案 0 :(得分:2)
这是我最后使用的重新索引脚本:
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"}
}
}
}
'