首页 归档 关于 文件 Github
×

SpringBoot整合ElasticSearch-v6.8.1的使用 - 4.索引的创建与删除

2022-03-07 16:39:52
ElasticSearch
  • SpringBoot
本文总阅读量(次):
本文字数统计(字):615
本文阅读时长(分):3

SpringBoot整合ElasticSearch-v6.8.1的封装 - 1.配置类
SpringBoot整合ElasticSearch-v6.8.1的封装 - 2.接口类
SpringBoot整合ElasticSearch-v6.8.1的封装 - 3.支持类
SpringBoot整合ElasticSearch-v6.8.1的使用 - 5.索引的数据(增删改)
SpringBoot整合ElasticSearch-v6.8.1的使用 - 6.索引的数据(查询)

使用Java创建索引不需要mappings下的_doc节点;以下面结构进行操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"properties": {
"keyId": {
"type": "long"
},
"account": {
"type": "keyword"
},
"username": {
"type": "text"
},
"age": {
"type": "short"
},
"birthday": {
"type": "date",
"format": "yyyy-MM-dd",
"ignore_malformed": true
},
"address": {
"properties": {
"region": {
"type": "text",
"index": "true"
},
"location": {
"properties": {
"province": {
"type": "text",
"index": "true"
},
"city": {
"type": "text",
"index": "true"
}
}
}
}
},
"role": {
"properties": {
"attribute": {
"type": "nested"
}
}
},
"createdTime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||epoch_millis",
"ignore_malformed": false
}
}
}
}

判断ES索引是否存在

1
2
3
4
5
6
7
8
9
@Test
void 判断ES索引是否存在() {
String index = "my_index";
try {
System.out.println(elasticSearchService.checkIndexExists(index));
} catch (Exception e) {
e.printStackTrace();
}
}

创建索引

第一种方式创建索引:通过加载整体的mapping字符进行创建

1
2
3
4
5
6
7
8
9
10
@Test
void 创建ES索引1() {
String index = "my_index";
try {
String source = "索引mapping内容,即上述的json文本";
System.out.println(elasticSearchService.createIndex(index, source));
} catch (Exception e) {
e.printStackTrace();
}
}

第二种方式创建索引:通过标签代码进行组装mapping内容进行创建

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
@Test
void 创建ES索引2() {
String index = "my_index";
try {
// 设置settings映射
Settings settings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build();
// 复合参数
Map<String, Object> region = new LinkedHashMap<>();
region.put("type", "text");
region.put("index", "true");
Map<String, Object> location = new LinkedHashMap<>();
Map<String, Object> location_properties = new LinkedHashMap<>();
Map<String, Object> province = new LinkedHashMap<>();
province.put("type", "text");
province.put("index", "true");
Map<String, Object> city = new LinkedHashMap<>();
city.put("type", "text");
city.put("index", "true");
location_properties.put("province", province);
location_properties.put("city", city);
location.put("properties", location_properties);
// 设置mapping映射
XContentBuilder mapping = XContentFactory.jsonBuilder()
.startObject()
.startObject("properties")
// keyId
.startObject("keyId").field("type", "long").endObject()
// account
.startObject("account").field("type", "keyword").endObject()
// username
.startObject("username").field("type", "text").endObject()
// age
.startObject("age").field("type", "short").endObject()
// birthday
.startObject("birthday")
.field("type", "date")
.field("format", "yyyy-MM-dd")
.field("ignore_malformed", true).endObject()
// address
.startObject("address").startObject("properties")
.field("region", region)
.field("location", location)
.endObject().endObject()
// role
.startObject("role").startObject("properties")
.startObject("attribute").field("type", "nested").endObject()
.endObject().endObject()
// createdTime
.startObject("createdTime")
.field("type", "date")
.field("format", "yyyy-MM-dd HH:mm:ss||epoch_millis")
.field("ignore_malformed", true).endObject()
.endObject().endObject();
System.out.println(elasticSearchService.createIndex(index, settings, mapping));
} catch (Exception e) {
e.printStackTrace();
}
}

删除索引

删除索引的同时会删除索引内的全部数据

1
2
3
4
5
6
7
8
9
@Test
void 删除ES索引() {
String index = "my_index";
try {
System.out.println(elasticSearchService.deleteIndex(index));
} catch (Exception e) {
e.printStackTrace();
}
}
完
SpringBoot整合ElasticSearch-v6.8.1的使用 - 5.索引的数据(增删改)
SpringBoot整合ElasticSearch-v6.8.1的封装 - 3.支持类

本文标题:SpringBoot整合ElasticSearch-v6.8.1的使用 - 4.索引的创建与删除

文章作者:十二

发布时间:2022-03-07 16:39:52

最后更新:2022-03-16 18:00:43

原始链接:https://www.zhuqiaolun.com/2022/03/1646642392484/1646642392484/

许可协议:署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

头像

十二

我想起那天夕阳下的奔跑,那是我逝去的青春。

分类

  • Blog4
  • ElasticSearch13
  • Freemarker2
  • Git2
  • Go-FastDfs2
  • IDEA2
  • J-Package6
  • J-Tools21
  • Java2
  • JavaFx3
  • Kafka4
  • Linux2
  • Logger5
  • Maven5
  • MyBatis6
  • MyCat3
  • MySql2
  • Nginx5
  • OceanBase1
  • RabbitMq4
  • Redis6
  • SVN1
  • SpringBoot11
  • Tomcat6
  • WebService2
  • Windows2
  • kubernetes10

归档

  • 二月 20251
  • 十二月 20244
  • 八月 202416
  • 六月 20241
  • 九月 20231
  • 八月 20231
  • 七月 20232
  • 八月 20222
  • 三月 202214
  • 二月 20224
  • 十一月 20211
  • 七月 20215
  • 六月 20213
  • 五月 20213
  • 四月 20211
  • 三月 202116
  • 二月 20212
  • 一月 20211
  • 十一月 202014
  • 十月 20201
  • 九月 202014
  • 八月 20205
  • 七月 20204
  • 六月 20208
  • 五月 20208

作品

我的微信 我的文件

网站信息

本站运行时间统计: 载入中...
本站文章字数统计:96.9k
本站文章数量统计:132
© 2025 十二  |  鄂ICP备18019781号-1  |  鄂公网安备42118202000044号
驱动于 Hexo  | 主题 antiquity  |  不蒜子告之 阁下是第个访客
首页 归档 关于 文件 Github