目录
前言
俗话说的好呀!技多不压身。即使学会了一种方法来操作es,我建议还是再学一种。之前分享了用spring.data的API,ElasticsearchRestTemplate来简单操作Elasticsearch的增删改查等功能~,用的是spring官方整合的api,而这次分享的是Elastic官方的api,我认为两者的api各有优劣
- spirng官方的操作简单;es的较为繁琐复杂
- spring的代码量少;es的代码量多
- spring的版本更新维护慢,还停留在7.x版本;es的紧追随版本脚步,不用担心版本问题
- spring的功能相比于es较少
相信各位心里已经有底了
本文章也算不上教程,只是整合了我自己一下学过的内容,写本文目的既是为了给大家一个快速学习的路径,也是为了方便我以后自己复习~
多说一句,各位认为es的灵魂是什么呢?我认为是它的查询。es诞生的意义就是为了更快速的查询。但本文章只是初级的入门,查询部分不做详解,具体高级查询聚合查询等等请看鄙人后续的文章~
项目构建与导包
1.1 建项目
构建随意构建,真无所谓的。我个人推荐构建时只导入这个就可以了。
1.2 导包
导包就讲究了,建议导下面3个包,具体版本看你自己的es
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.15.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.15.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.3</version>
</dependency>
开始咯
提前说明一下,所有运行的结果都可以在kibana中查看到,大家可以去kibana上看看自己的操作有没有成功哦
老规矩在springtest中学习
首先需要在springtest类中配一个客户端,这是所有操作的基础,具体里面的配置需要你自己改成你es的ip和端口就行了
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("192.168.10.120", 9200, "http"))
);
2.1 索引Index
2.1.1 创建索引
本次创建索引就不创建对应的映射mapping了,high-level-client创建mapping我认为极其复杂的,需要非常清楚es原生的语法,对新手来说很不友好。因此本文章就不多做介绍,需要看mapping创建的可以去看其他博主的文章。
创建一个book1的索引
// 创建索引
CreateIndexRequest request = new CreateIndexRequest("book1");
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
2.1.2 查询索引
// 查询索引
GetIndexRequest request = new GetIndexRequest("book1");
GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT);
// 相应状态
System.out.println("response.getSetting() = " + response.getSettings());
2.1.3 删除索引
// 删除索引
DeleteIndexRequest request = new DeleteIndexRequest("book1");
AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
2.2 文档Document
2.2.1 创建文档
首先需要一个对应的实体类做映射
@Data
public class Book1 {
private String title;
private Double price;
}
插入数据
// 创建请求
IndexRequest request = new IndexRequest();
request.index("book1");
// 创建数据
Book1 book1 = new Book1();
book1.setTitle("雪落香杉树");
book1.setPrice(39.8);
book1.setAuthor("戴维·伽特森");
// 对象转换json
ObjectMapper objectMapper = new ObjectMapper();
String userJson = objectMapper.writeValueAsString(book1);
request.source(userJson, XContentType.JSON);
// 插入数据,返回结果
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
2.2.2 更新文档
根据id修改文档
// 创建请求
UpdateRequest request = new UpdateRequest();
request.index("book1").id("aRyys4IBhICGg2eQuapD");
request.doc(XContentType.JSON, "price", 99.8);
// 更新数据,返回结果
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
2.2.3 根据id查询文档
最简单的查询文档的方式
// 创建请求
GetRequest request = new GetRequest();
request.index("book1").id("aRyys4IBhICGg2eQuapD");
// 查询数据,返回结果
GetResponse response = client.get(request, RequestOptions.DEFAULT);
// 打印结果
System.out.println("response.getSourceAsMap() = " + response.getSourceAsMap());
//查询结果
response.getSourceAsMap() = {price=99.8, author=戴维·伽特森, title=雪落香杉树}
2.2.4 删除文档
// 创建请求
DeleteRequest request = new DeleteRequest();
request.index("book1").id("aRyys4IBhICGg2eQuapD");
// 删除数据,返回结果
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
2.2.5 bulk批量插入文档
一下插入3本书
// 创建请求
BulkRequest request = new BulkRequest();
request.add(
new IndexRequest().index("book1").source(XContentType.JSON, "title", "雪落香杉树", "price", 88.9, "author", "戴维·伽特森"),
new IndexRequest().index("book1").source(XContentType.JSON, "title", "百年孤独", "price", 58.9, "author", "加西亚·马尔克斯"),
new IndexRequest().index("book1").source(XContentType.JSON, "title", "霍乱时期的爱情", "price", 138.8, "author", "加西亚·马尔克斯")
);
// 插入数据,返回结果
BulkResponse responses = client.bulk(request, RequestOptions.DEFAULT);
结束了~
想说的话
本篇只是es的入门中的入门,基本操作中的基本操作。
以后用到的时候到这里CV就可以了
欲知后续查询如何,请听下回在下分解~