0,定义esHighClient

 1 @Configuration
2 public class RestClientConfig {
3
4 //类似:200.188.22.20:9300,200.188.22.21:9300
5 @Value("${spring.elasticsearch.rest.hosts}")
6 private String hosts;
7
8
9 @Bean
10 public RestHighLevelClient esRestHighLevelClient() {
11
12 HttpHost[] httpHosts = Arrays.stream(hosts.split(",")).map(x -> {
13 String[] hostInfo = x.split(":");
14 return new HttpHost(hostInfo[0], Integer.parseInt(hostInfo[1]));
15 }).toArray(HttpHost[]::new);
16
17 RestClientBuilder restClientBuilder = RestClient.builder(httpHosts).
18 setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder.setConnectTimeout(30000)
19 .setSocketTimeout(30000).setConnectionRequestTimeout(10000));
20
21 return new RestHighLevelClient(restClientBuilder);
22 }
23 }

1,定义涉及到nested类型的mapping

1 CreateIndexRequest request = new CreateIndexRequest("你定义的index名字");
2 //创建index
3 CreateIndexResponse response = esRestHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
4 PutMappingRequest request = new PutMappingRequest(index);
5 //由于es7以上版本无type的概念,本人学术不精,此字段强制设置type,只能用默认的_doc填入其中
6 request.type("_doc");
7 //定义刚刚创建的index的 mapping的json结构
8 request.source("{\"properties\":{\"deviceId\":{\"type\":\"keyword\"},\"indicatorId\":{\"type\":\"keyword\"},\"instanceName\":{\"type\":\"keyword\"},\"substance\":{\"type\":\"nested\",\"properties\":{\"value\":{\"type\":\"double\"},\"collectTime\":{\"type\":\"date\"},\"receiveTime\":{\"type\":\"date\"},\"dbTime\":{\"type\":\"date\"}}}}}", XContentType.JSON);
9 AcknowledgedResponse response = esRestHighLevelClient.indices().putMapping(request, RequestOptions.DEFAULT);

2,定义mapping对应的Java对象

 1 @Data
2 public class NestSeries {
3
4 //设备ID
5 @Field(type = FieldType.Keyword)
6 // @JsonProperty("device_id")
7 private String deviceId;
8 //监控项ID
9 @Field(type = FieldType.Keyword)
10 private String indicatorId;
11
12 //监控实例,eg:C盘,D盘
13 @Field(type = FieldType.Text)
14 private String instanceName;
15
16
17 //监控项当前性能值
18 @Field(type = FieldType.Nested)
19 private List<SubStanceSeries> substance;
20
21 }

3,定义mapping中nested子对象

@Data
public class SubStanceSeries {
//监控项当前性能值
@Field(type = FieldType.Double)
private Double value; //监控项采集前时间
@Field(type = FieldType.Date)
private long collectTime; //监控项采集后时间
@Field(type = FieldType.Date)
private long receiveTime; //监控项入库时间
@Field(type = FieldType.Date)
private long dbTime; }

4,定义对象转map的工具类(FasterJsonUtil同)

1 @Slf4j
2 public class JsonUtil {
3 private static ObjectMapper mapper = new ObjectMapper();
4    public static Map<String, Object> convertValueToMap(Object data) {
5 return mapper.convertValue(data, new TypeReference<Map<String, Object>>() {
6 });
7 }
8 }

5,判断此条数据是否存在,存在则更新,不存在则新增

 1 private boolean existSeries(PerformanceDo p, String indexName) {
2
3 try {
4 BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
5 Es7Util.addOptionalMustQuery(queryBuilder, "deviceId", p.getDeviceId());
6 Es7Util.addOptionalMustQuery(queryBuilder, "indicatorId", p.getIndicatorId());
7 if (StringUtils.isNotBlank(p.getInstanceName())) {
8 Es7Util.addOptionalMustQuery(queryBuilder, "instanceName", p.getInstanceName());
9 }
10
11 //通过client方式去查询
12 SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
13 sourceBuilder.query(queryBuilder);
14 //索引 各种组合条件
15 SearchRequest rq = new SearchRequest().indices(indexName).source(sourceBuilder);
16
17 SearchResponse rp = esRestHighLevelClient.search(rq, RequestOptions.DEFAULT);
18
19 //通过client方式去查询
20 if (rp.status() == RestStatus.OK && rp.getHits().getHits().length > 0) {
21 //说明存在记录
22 return true;
23 }
24 } catch (Exception e) {
25 log.error("查询series是否存在当前nest异常:",e);
26 }
27
28 return false;
29 }

6,新增一个document(类似于mysql插入一行数据)

 1 //新增性能数据存储 新增操作
2 private void addNestSeries(PerformanceDo p,String indexName) {
3
4 try {
5 NestSeries series = new NestSeries();
6 series.setDeviceId(p.getDeviceId());
7 series.setIndicatorId(p.getIndicatorId());
8 series.setInstanceName(p.getInstanceName());
9 List<SubStanceSeries> subStanceSeriesList = new ArrayList<>();
10 SubStanceSeries subStanceSeries = new SubStanceSeries();
11
12 if (StringUtils.isNotBlank(p.getValue())) {
13 subStanceSeries.setValue(Double.valueOf(p.getValue()));
14 }
15
16 subStanceSeries.setCollectTime(p.getCollectTime());
17 subStanceSeries.setReceiveTime(p.getReceiveTime());
18 subStanceSeries.setDbTime(p.getDbTime());
19 subStanceSeriesList.add(subStanceSeries);
20 series.setSubstance(subStanceSeriesList);
21
22 IndexRequest indexRequest = new IndexRequest(indexName, TYPE).source(FasterJsonUtil.convertValueToMap(series), XContentType.JSON);
23
24 esRestHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
25 } catch (Exception e) {
26 log.error("新增series的nest异常:",e);
27 }
28
29 }

7,局部更新nested类型内部数据

 1 //追加性能数据存储 更新操作
2 private void uptNestSeries(PerformanceDo p, String indexName) {
3
4 try {
5 UpdateByQueryRequest updateByQueryRequest = new UpdateByQueryRequest(indexName);
6
7 BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
8 Es7Util.addOptionalMustQuery(queryBuilder, "deviceId", p.getDeviceId());
9 Es7Util.addOptionalMustQuery(queryBuilder, "indicatorId", p.getIndicatorId());
10 if (StringUtils.isNotBlank(p.getInstanceName())) {
11 Es7Util.addOptionalMustQuery(queryBuilder, "instanceName", p.getInstanceName());
12 }
13
14 SubStanceSeries subStanceSeries = new SubStanceSeries();
15
16 if (StringUtils.isNotBlank(p.getValue())) {
17 subStanceSeries.setValue(Double.valueOf(p.getValue()));
18 }
19
20 subStanceSeries.setCollectTime(p.getCollectTime());
21 subStanceSeries.setReceiveTime(p.getReceiveTime());
22 subStanceSeries.setDbTime(p.getDbTime());
23
24
25 //通过查询 指定要更新的_id
26 updateByQueryRequest.setQuery(queryBuilder);
27 Map<String, Object> paramMap = new HashMap<>();
28 paramMap.put("detail", convertValueToMap(subStanceSeries));
29
30 //使用painless脚本 进行复杂结构的更新 我演示的是去更新嵌套类型comments 这里是添加comments
31 updateByQueryRequest.setScript(new Script(ScriptType.INLINE, "painless", "ctx._source.substance.add(params.detail)", paramMap));
32
33 esRestHighLevelClient.updateByQuery(updateByQueryRequest, RequestOptions.DEFAULT);
34
35 } catch (Exception e) {
36 log.error("更新series的nest异常:",e);
37 }
38 }

对应dml如下:
 1 POST /indexname20201118/_update_by_query
2 {
3 "script": {
4 "source": "ctx._source.substance.add(params.detail)",
5 "lang": "painless",
6 "params": {
7 "detail": {
8 "value": 4,
9 "collectTime": 4405691110000,
10 "receiveTime": 4405691110000,
11 "dbTime": 4405691110000
12
13 }
14 }
15 },
16 "query": {
17 "bool": {
18 "must": [
19 {
20 "match_phrase": {
21 "deviceId": "1212121-f796-4e74-8007-929631212121"
22 }
23 },
24 {
25 "match_phrase": {
26 "indicatorId": "12121212SYS_USAGE"
27 }
28 },
29 {
30 "match_phrase": {
31 "instanceName": "/dev/mapper/1212121"
32 }
33 }
34 ]
35 }
36 }
37 }

8,实现结果展示如下:

  1 {
2 "took": 1,
3 "timed_out": false,
4 "_shards": {
5 "total": 1,
6 "successful": 1,
7 "skipped": 0,
8 "failed": 0
9 },
10 "hits": {
11 "total": {
12 "value": 1,
13 "relation": "eq"
14 },
15 "max_score": 9.95262,
16 "hits": [
17 {
18 "_index": "indexname20201119",
19 "_type": "_doc",
20 "_id": "izhV33UBQMSXiw121212",
21 "_score": 9.95262,
22 "_source": {
23 "indicatorId": "LINUX_PR1212S_1212121",
24 "instanceName": "test",
25 "substance": [
26 {
27 "receiveTime": 121215692836100,
28 "collectTime": 11521292835000,
29 "dbTime": 1169821207832,
30 "value": 4
31 },
32 {
33 "receiveTime": 121215692896125,
34 "collectTime": 1121292895000,
35 "dbTime": 112121769904778,
36 "value": 4
37 },
38 {
39 "receiveTime": 16121212956112,
40 "collectTime": 1605692955000,
41 "dbTime": 1605121913140,
42 "value": 5
43 },
44 {
45 "receiveTime": 12121212,
46 "collectTime": 1601211215001,
47 "dbTime": 1212121,
48 "value": 4
49 },
50 {
51 "receiveTime": 121212176098,
52 "collectTime": 161212125001,
53 "dbTime":11215769930788,
54 "value": 6
55 },
56 {
57 "receiveTime": 12121136104,
58 "collectTime": 1212693135000,
59 "dbTime": 11212769939631,
60 "value": 7
61 },
62 {
63 "receiveTime": 12121296103,
64 "collectTime":2121193195000,
65 "dbTime": 1121219948989,
66 "value": 4
67 },
68 {
69 "receiveTime": 121212156141,
70 "collectTime": 12121293255000,
71 "dbTime": 121212571214,
72 "value": 4
73 },
74 {
75 "receiveTime": 1605693316109,
76 "collectTime": 1605693315001,
77 "dbTime": 1605769965907,
78 "value": 4
79 },
80 {
81 "receiveTime": 12121212115,
82 "collectTime": 1121212375001,
83 "dbTime": 1121219974723,
84 "value": 8
85 },
86 {
87 "receiveTime": 121212113,
88 "collectTime": 160212121435001,
89 "dbTime": 16051212183159,
90 "value": 4
91 },
92 {
93 "receiveTime": 1605693496107,
94 "collectTime": 1605693495000,
95 "dbTime": 1605769992147,
96 "value": 4
97 },
98 {
99 "receiveTime": 1605693556103,
100 "collectTime": 1605693555000,
101 "dbTime": 1605770001828,
102 "value": 4
103 }
104 ],
105 "deviceId": "121212126-4e74-121212123f5"
106 }
107 }
108 ]
109 }
110 }




java中elasticsearch7.x关于nested类型的api使用,新增+更新的更多相关文章

  1. 【转】java中byte数组与int类型的转换(两种方式)----不错

    原文网址:http://blog.csdn.net/piaojun_pj/article/details/5903009 java中byte数组与int类型的转换,在网络编程中这个算法是最基本的算法, ...

  2. java中读取特殊文件的类型

    java中读取特殊文件的类型: 第一种方法(字符拼接读取): public static String getType(String s){ String s1=s.substring(s.index ...

  3. Java中的基本类型和包装类型区别

    首先看一下几个测试题,验证一下java中对基本类型和包装类型的理解,看看最后输出的答案对不对,答案在这篇博客中哦: // 第一题: 基本类型和包装类型 int a = 100; Integer b = ...

  4. Java中的两种异常类型及其区别?

    Java中的两种异常类型是什么?他们有什么区别? Throwable包含了错误(Error)和异常(Excetion两类) Exception又包含了运行时异常(RuntimeException, 又 ...

  5. JAVA中如何获取变量的类型

    JAVA中如何获取变量的类型? package xiya; public class Demo { public static void main(String[] args) { String ty ...

  6. java中如何理解:其他类型 + string 与 自增类型转换和赋值类型转换

    java中如何理解:其他类型 + string 与 自增类型转换和赋值类型转换 一.字符串与其他类型连接 public class DemoString{ public static void mai ...

  7. 在java 中,数组与 List<T> 类型的相互转换

    在java中,数组与List<T> 之前进行互相转换,转换方法可总结为以下几种: 一. 将 数组转换成List<T> 1. 使用 Collections 的addAll 方法 ...

  8. java中存储mysql数据库时间类型

    Mysql 与 java 的时间类型 MySql的时间类型有 Java中与之对应的时间类型   date                 java.sql.Date   Datetime        ...

  9. JAVA中char和String/值类型和引用类型的区别

    import java.util.*; class test { public static void main(String[] args) { char a[] = {'b', 'a', 'c'} ...

随机推荐

  1. Seaborn中几种作图方式

    趋势 sns.pointplot - 点图 ,比折线图好使 sns.lineplot - 折线图最适合显示一段时间内的趋势,多条线可以用来显示多个组中的趋势. 关系 - 可以使用许多不同的图表类型来理 ...

  2. 攻防世界-web2

    打开题目直接一梭子代码,给我们了个加密的密文,然我们解出flag 审计一波 先时将flag字符串逆序,然后每一个ascii码数值+1,然后base64加密,然后逆序,然后再rot13加密 得到加密的密 ...

  3. Java网关服务-AIO(三)

    Java网关服务-AIO(三) 概述 前两节中,我们已经获取了body的总长度,剩下的就是读出body,处理请求 ChannelServerHandler ChannelServerHandler即从 ...

  4. MongoDB用户,角色管理 --- MongoDB基础用法(三)

    用户管理 用户创建 MongoDB采用基于角色的访问控制(RBAC)来确定用户的访问. 授予用户一个或多个角色,确定用户对MongoDB资源的访问权限和用户可以执行哪些操作. 用户应该只有最小权限集才 ...

  5. Web前端_流式布局(百分比布局)

    移动Web_流式布局(百分比布局) writer:late at night codepeasant 1(百分比布局) ☞核心知识点 1.流式布局(百分比布局) 2.视口设置 ☞今日目标 1. 能够使 ...

  6. GPRS DTU工作的原理与应用场景

    GPRS DTU是属于物联网无线数据终端设备的中一种,它主要是利用公用运营商的GPRS网络(又称G网)来为用户提供无线长距离数据传输的功能.一般都是采用的高性能工业级8/16/32位通信处理器和工业级 ...

  7. Pycharm同步远程服务器调试

    Pycharm同步远程服务器调试 1.需要准备工具 xftp:上传项目文件 xshell:连接Linux系统调试,执行命令 PyCharm:调试python代码 这些软件可以自行网上搜索下载,也可以关 ...

  8. Python常用组件、命令大总结(持续更新)

    Python开发常用组件.命令(干货) 持续更新中-关注公众号"轻松学编程"了解更多. 1.生成6位数字随机验证码 import random import string def ...

  9. LWJGL3的内存管理

    LWJGL3的内存管理 LWJGL3 (Lightweight Java Game Library 3),是一个支持OpenGL,OpenAl,Opengl ES,Vulkan等的Java绑定库.&l ...

  10. uniApp 列表

    普通列表 例图: 代码片段: <template> <view class="teacher"> <view class="teacher- ...