在企业环境中,Elasticsearch的索引CRUD(创建Create、读取Read、更新Update、删除Delete)操作是非常基础且频繁使用的功能。这些操作对于管理和维护数据至关重要,尤其是在处理大规模数据集和需要实时搜索与分析的应用场景中。
目录
Mapping映射属性
索引库的CRUD
基于Kibana的DevTools
创建索引库和映射
查询索引库
修改索引库
删除索引库
基于Java客户端(Java REST Client)
初始化RestHighLevelClient
准备索引映射
创建索引库
删除索引库
查询索引库
在Elasticsearch中,Index就类似数据库表,Mapping映射就类似表的结构。我们要向es中存储数据,必须先创建Index和Mapping。
Mapping映射属性
Mapping是对索引库中文档的约束,常见的Mapping属性包括:
字段数据类型,常见的简单类型有:
1.字符串:text(可分词的文本)、keyword(精确值,例如:品牌、国家、ip地址)
2.数值:long、integer、short、byte、double、float
3.布尔:boolean
4.日期:date
5.对象:object index:是否创建索引,默认为true
6.analyzer:使用哪种分词器
7.properties:该字段的子字段信息
举例说明:(下面的json文档)
{
"age": 21,
"weight": 52.1,
"isMarried": false,
"info": "谢谢欣赏x",
"email": "zy@123.cn",
"score": [99.1, 99.5, 98.9],
"name": {
"firstName": "云",
"lastName": "赵"
}
}
对应的每个字段映射(Mapping):
索引库的CRUD
基于Kibana的DevTools
创建索引库和映射
# 创建索引库
PUT /user
{
"mappings": {
"properties": {
"info":{
"type": "text",
"analyzer": "ik_smart",
"index": true
},
"age":{
"type": "byte"
},
"email":{
"type": "keyword",
"index": false
},
"name":{
"type": "object",
"properties": {
"firstName":{
"type": "keyword"
},
"lastName":{
"type": "keyword"
}
}
}
}
}
}
实现效果如下:
查询索引库
GET /user
实现效果如下:
修改索引库
倒排索引结构虽然不复杂,但是一旦数据结构改变(比如改变了分词器),就需要重新创建倒排索引,这简直是灾难。因此索引库一旦创建,无法修改mapping。
但是允许添加新的字段到mapping中
# 修改索引库
PUT /user/_mapping
{
"properties": {
"id": {
"type": "byte"
}
}
}
实现效果如下:
删除索引库
删除完后再查询
# 删除索引库
DELETE /user
# 查询索引库
GET /user
实现效果如下:
说明删除成功
基于Java客户端(Java REST Client)
ES官方提供了各种不同语言的客户端,用来操作ES。这些客户端的本质就是组装DSL语句,通过http请求发送给ES。
新增一个测试项目es-demo(就一个普通的spring boot项目)
初始化RestHighLevelClient
在pom.xml添加es的依赖
<!-- es 依赖-->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.12.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.12.1</version>
</dependency>
在elasticsearch提供的API中,与elasticsearch一切交互都封装在一个名为RestHighLevelClient
的类中,必须先完成这个对象的初始化,建立与elasticsearch的连接。
private RestHighLevelClient client;
/**
* 创建ES客户端
*/
@BeforeEach
void setUp() {
client = new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://自己的虚拟机地址:9200")
));
}
/**
* 关闭ES客户端
* @throws IOException
*/
@AfterEach
void tearDown() throws IOException {
if (client != null) {
client.close();
}
}
/**
* 测试连接
*/
@Test
void testConnection() {
System.out.println("client = " + client);
}
运行测试连接,效果如下:
解读:连接ES客户端成功
接下来,对索引的CRUD是需要通过对映射来实现的。
准备索引映射
PUT /items
{
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"name":{
"type": "text",
"analyzer": "ik_max_word"
},
"price":{
"type": "integer"
},
"stock":{
"type": "integer"
},
"image":{
"type": "keyword",
"index": false
},
"category":{
"type": "keyword"
},
"brand":{
"type": "keyword"
},
"sold":{
"type": "integer"
},
"commentCount":{
"type": "integer",
"index": false
},
"isAD":{
"type": "boolean"
},
"updateTime":{
"type": "date"
}
}
}
}
基于Java客户端的CRUD代码基本分为三步:
1.创建Request对象。因为是创建索引库的操作,因此Request是CreateIndexRequest
。
2.添加请求参数。其实就是Json格式的Mapping映射参数。因为json字符串很长,这里是定义了静态字符串常量MAPPING_TEMPLATE
,让代码看起来更加优雅。
3.发送请求。client.indices
()
方法的返回值是IndicesClient
类型,封装了所有与索引库操作有关的方法。例如创建索引、删除索引、判断索引是否存在等。
创建索引库
/**
* 创建索引
* @throws IOException
*/
@Test
void testCreateIndex() throws IOException {
// 1.创建Request对象
CreateIndexRequest request = new CreateIndexRequest("items");
// 2.准备请求参数
request.source(MAPPING_TEMPLATE, XContentType.JSON);
// 3.发送请求
client.indices().create(request, RequestOptions.DEFAULT);
}
为了验证索引是否创建成功,创建一个方法
/**
* 判断索引是否存在
* @throws IOException
*/
@Test
void testGetIndex() throws IOException {
// 1.创建Request对象
GetIndexRequest request = new GetIndexRequest("items");
// 2.发送请求
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println("存在索引 = " + exists);
}
实现效果如下:
items索引创建成功
删除索引库
/**
* 删除索引
* @throws IOException
*/
@Test
void testDeleteIndex() throws IOException {
// 1.创建Request对象
DeleteIndexRequest request = new DeleteIndexRequest("items");
// 2.发送请求
client.indices().delete(request, RequestOptions.DEFAULT);
}
调用前面的判断索引是否存在方法,实现效果如下:
删除索引items成功
查询索引库
就是修改判断索引是否存在方法,主要返回内容一大堆。
/**
* 查询索引
* @throws IOException
*/
@Test
void testQueryIndex() throws IOException {
// 1.创建Request对象
GetIndexRequest request = new GetIndexRequest("items");
// 2.发送请求
GetIndexResponse getIndexResponse = client.indices().get(request, RequestOptions.DEFAULT);
System.out.println("getIndexResponse = " + getIndexResponse);
}
实现效果如下:
查询索引items成功
注意:开发项目使用的是基于基于Java客户端(Java REST Client)来开发的。基于Kibana的DevTools一般用来测试。