|
@@ -537,21 +537,88 @@ public class EsService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /*POST /patent/_update_by_query
|
|
|
- {
|
|
|
- "script": {
|
|
|
- "source": "ctx._source.project_id = 1",
|
|
|
- "lang": "painless"
|
|
|
- },
|
|
|
- "query": {
|
|
|
- "term": {
|
|
|
- "_id": "yYVNzowBmzIo81_44OnV"
|
|
|
+ //获取父文档相关的所有自定义子文档,按照时间按倒序,首个改为最新数据,其他if——new改为0:不是最新
|
|
|
+ public Integer getIds(String parentId, Integer projectId, Integer taskId, String fieldId) throws Exception {
|
|
|
+ int ifNew = 0;
|
|
|
+ SearchRequest.Builder builder = new SearchRequest.Builder();
|
|
|
+ //设置查询索引
|
|
|
+ builder.index("patent");
|
|
|
+ Query q1 = QueryBuilders.hasParent(i -> i.parentType("patent").query(j -> j.ids(IdsQuery.of(k -> k.values(parentId)))));
|
|
|
+ Query q2 = QueryBuilders.exists(i -> i.field("custom_field"));
|
|
|
+ Query q3 = null;
|
|
|
+ if (taskId != null) {
|
|
|
+ q3 = QueryBuilders.term(i -> i.field("custom_field.task_id").value(taskId));
|
|
|
+ } else {
|
|
|
+ q3 = QueryBuilders.term(i -> i.field("custom_field.project_id").value(projectId));
|
|
|
+ }
|
|
|
+ Query q4 = QueryBuilders.term(i -> i.field("custom_field.field").value(fieldId));
|
|
|
+ Query finalQ = q3;
|
|
|
+ Query bool = QueryBuilders.bool(i -> i.must(q1, q2, finalQ, q4));
|
|
|
+ builder.query(bool);
|
|
|
+
|
|
|
+ List<SortOptions> optionsList = new ArrayList<>();
|
|
|
+ SortOptions sortOptions = SortOptions.of(i -> i.field(j -> j.field("custom_field.create_time").order(SortOrder.Desc).missing(-1)));
|
|
|
+ optionsList.add(sortOptions);
|
|
|
+ builder.sort(optionsList);
|
|
|
+
|
|
|
+ SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
|
|
|
+ List<Hit<Patent>> hits = response.hits().hits();
|
|
|
+ Map<String, ESCustomField> map = new HashMap<>();
|
|
|
+ for (Hit<Patent> hit : hits) {
|
|
|
+ String id = hit.id();
|
|
|
+ Patent patent = hit.source();
|
|
|
+ map.put(id, patent.getESCustomField());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!CollectionUtils.isEmpty(map)) {
|
|
|
+ if (map.size() > 1) {
|
|
|
+ for (String id : map.keySet()) {
|
|
|
+ ESCustomField esCustomField = new ESCustomField();
|
|
|
+ esCustomField.setIfNew(0);
|
|
|
+ Patent patent = new Patent();
|
|
|
+ patent.setESCustomField(esCustomField);
|
|
|
+ this.updateCustomFieldIfNew(patent, id);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Iterator<Map.Entry<String, ESCustomField>> iterator = map.entrySet().iterator();
|
|
|
+ if (iterator.hasNext()) {
|
|
|
+ Map.Entry<String, ESCustomField> firstEntry = iterator.next();
|
|
|
+ String key = firstEntry.getKey();
|
|
|
+ ESCustomField esCustomField = new ESCustomField();
|
|
|
+ esCustomField.setIfNew(1);
|
|
|
+ Patent patent = new Patent();
|
|
|
+ patent.setESCustomField(esCustomField);
|
|
|
+ ifNew = this.updateCustomFieldIfNew(patent, key);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ifNew;
|
|
|
+ }
|
|
|
+
|
|
|
+ //更新自定义文档是否是最新数据
|
|
|
+ public Integer updateCustomFieldIfNew(Patent patent, String id) throws IOException {
|
|
|
+ ESCustomField customField = patent.getESCustomField();
|
|
|
+ String source = "ctx._source.custom_field.if_new = " + customField.getIfNew();
|
|
|
+ InlineScript inlineScript = InlineScript.of(i -> i.lang("painless").source(source));
|
|
|
+ Script script = Script.of(i -> i.inline(inlineScript));
|
|
|
+ Query query = QueryBuilders.term(i -> i.field("_id").value(id));
|
|
|
+ UpdateByQueryRequest request = UpdateByQueryRequest.of(i -> i.index("patent").script(script).refresh(true).query(query));
|
|
|
+ try {
|
|
|
+ client.updateByQuery(request);
|
|
|
+ return 1;
|
|
|
+ } catch (IOException e) {
|
|
|
+ return -1;
|
|
|
}
|
|
|
}
|
|
|
- }*/
|
|
|
+
|
|
|
//更新子文档
|
|
|
- public Integer updateByQuery(Patent patent, String id) throws IOException {
|
|
|
+ public Integer updateByQuery(Patent patent, String id) throws Exception {
|
|
|
ESCustomField customField = patent.getESCustomField();
|
|
|
+ String parentId = patent.getPatentJoin().getParent();
|
|
|
+ Integer num = this.getIds(parentId, customField.getProjectId(), customField.getTaskId(), customField.getField());
|
|
|
+ if (num < 1) {
|
|
|
+ throw new XiaoShiException("子文档是否最新更新失败");
|
|
|
+ }
|
|
|
+
|
|
|
String valueField = "[";
|
|
|
List<String> fieldValueList = customField.getFieldValue();
|
|
|
if (!CollectionUtils.isEmpty(fieldValueList)) {
|
|
@@ -586,6 +653,7 @@ public class EsService {
|
|
|
String dateStr = "\'" + s + "\'";
|
|
|
String projectId = "ctx._source.custom_field.project_id = " + customField.getProjectId() + ";";
|
|
|
String taskId = "ctx._source.custom_field.task_id = " + customField.getTaskId() + ";";
|
|
|
+// String ifNew = "ctx._source.custom_field.if_new = " + customField.getIfNew() + ";";
|
|
|
String field = "ctx._source.custom_field.field=" + customField.getField() + ";";
|
|
|
String fieldType = "ctx._source.custom_field.field_type = " + customField.getFieldType() + ";";
|
|
|
String personId = "ctx._source.custom_field.person_id = " + customField.getPersonId() + ";";
|