Hello,World

bulk使用updateRequest().upsert()方法保存子类doc 与父类doc对应不上?

Elasticsearch | 作者 codeKing007 | 发布于2019年03月25日 | 阅读数:5093

环境:
es5.3.3
client:transport
1.索引情况:
{
"casefolder": {
"customfield": {
"_parent": {
"type": "medicalrecord"
},
"_routing": {
"required": true
},
"properties": {
"fieldValue": {
"type": "keyword",
"ignore_above": 10922
},
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"medicalrecord": {
"properties": {
"address": {
"type": "keyword",
"copy_to": [
"fullFieldName"
]
},
"age": {
"type": "keyword",
"copy_to": [
"fullFieldName"
]
},
"ageUnit": {
"type": "keyword"
},
"basicInformation": {
"type": "keyword",
"copy_to": [
"fullFieldName"
]
},
"caseCode": {
"type": "keyword",
"copy_to": [
"fullFieldName"
]
},
"caseCodeType": {
"type": "keyword"
},
"cell": {
"type": "keyword",
"copy_to": [
"fullFieldName"
]
},
"contactPersonName": {
"type": "keyword",
"copy_to": [
"fullFieldName"
]
},
"createTimeStr": {
"type": "date",
"ignore_malformed": true,
"format": "yyyy-MM-dd"
},
"email": {
"type": "text",
"copy_to": [
"fullFieldName"
]
},
"encounterTime": {
"type": "date",
"ignore_malformed": true
},
"fullFieldName": {
"type": "keyword"
},
"gender": {
"type": "keyword",
"copy_to": [
"fullFieldName"
]
},
"iDCardNumber": {
"type": "keyword",
"copy_to": [
"fullFieldName"
]
},
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"introducer": {
"type": "keyword",
"copy_to": [
"fullFieldName"
]
},
"otherCaseCode": {
"type": "keyword",
"copy_to": [
"fullFieldName"
]
},
"otherCaseCodeType": {
"type": "keyword"
},
"otherMemo": {
"type": "keyword",
"copy_to": [
"fullFieldName"
]
},
"patientName": {
"type": "keyword",
"copy_to": [
"fullFieldName"
]
},
"patientOccupation": {
"type": "keyword",
"copy_to": [
"fullFieldName"
]
},
"tel": {
"type": "text",
"copy_to": [
"fullFieldName"
]
},
"uid": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"updateTimeStr": {
"type": "date",
"ignore_malformed": true
},
"userId": {
"type": "integer"
},
"version": {
"type": "long"
}
}
}
}
}
}

我的代码:
保存子类type customfield  关联有parentId的doc到es中(前提是parentType:medicalrecord的doc已经存在于es中)
public <T extends IndexVo> boolean batchIndex(String index, String type, List<String> parentIdList, List<T> data) {

if (parentIdList.size() != data.size()) {
throw new SearchException("批量数据与parentId数量不匹配!");
}

BulkRequestBuilder bulkRequest = esBase.getClient().prepareBulk();
for (int i = 0; i < data.size(); i++) {
T t = data.get(i);
String json = JSONObject.toJSONString(t);
IndexRequest indexRequest = new IndexRequest(index, type, t.get_id()).source(json).parent(parentIdList.get(i));
UpdateRequest updateRequest = new UpdateRequest(index, type, t.get_id()).parent(parentIdList.get(i)).doc(json).upsert(indexRequest);

bulkRequest.add(updateRequest);
}
BulkResponse bulkResponse = bulkRequest.execute().actionGet();
.......
}

对于data参数如下,其中每个参数的id一直是固定的,value会根据不同的records而不同;
        CustomFieldIndexVo customFieldIndexVo = new CustomFieldIndexVo();
customFieldIndexVo.setFieldValue("144");
customFieldIndexVo.set_id("68e5c41e-2fbd-4dfa-bef5-901ceadece28");
indexVos.add(customFieldIndexVo);

CustomFieldIndexVo customFieldIndexVo1 = new CustomFieldIndexVo();
customFieldIndexVo1.setFieldValue("224");
customFieldIndexVo1.set_id("0edcf173-8af2-4818-9056-4c2d1ceafb0b");
indexVos.add(customFieldIndexVo1);

CustomFieldIndexVo customFieldIndexVo2 = new CustomFieldIndexVo();
customFieldIndexVo2.setFieldValue("乔治4");
customFieldIndexVo2.set_id("dc038e90-df9c-4ccb-b50a-ed050f803b3a");
indexVos.add(customFieldIndexVo2);

CustomFieldIndexVo customFieldIndexVo3 = new CustomFieldIndexVo();
customFieldIndexVo3.setFieldValue("严选4");
customFieldIndexVo3.set_id("15ed58c9-94b7-4763-9333-b0abf7198415");
indexVos.add(customFieldIndexVo3);

CustomFieldIndexVo customFieldIndexVo4 = new CustomFieldIndexVo();
customFieldIndexVo4.setFieldValue("跨栏4");
customFieldIndexVo4.set_id("662b002c-c6f1-482f-9e49-9c101d967d22");
indexVos.add(customFieldIndexVo4);
parentIdList参数集合中的id都相同,即对应的medicalrecordid
["D5286126-B1FC-4A3B-AE17-776799239C6F",
"D5286126-B1FC-4A3B-AE17-776799239C6F",
"D5286126-B1FC-4A3B-AE17-776799239C6F",
"D5286126-B1FC-4A3B-AE17-776799239C6F",
"D5286126-B1FC-4A3B-AE17-776799239C6F"]"

 
问题描述:
执行完后,medicalrecordid子类中的customfield doc不全,部分customfield doc会关联到其他parenId 的medicalrecordid。
 例如:
customfield 中 乔治4应该对应 medicalrecordid 中的 记录张三,实际对应的是medicalrecordid的 记录李四;
在张三这条记录中只有传入五个子类的其中几个,比如:严选4就可能存在;
 
 
希望大家帮忙解决一下
已邀请:

laoyang360 - 《一本书讲透Elasticsearch》作者,Elastic认证工程师 [死磕Elasitcsearch]知识星球地址:http://t.cn/RmwM3N9;微信公众号:铭毅天下; 博客:https://elastic.blog.csdn.net

赞同来自:

指定路由了吗?你mapping中路由是true,必须手动指定的

要回复问题请先登录注册