mapping是对索引库中文档的约束,常见的mapping属性包括:
{
"id":"101",
"name":"张三",
"gender":"男",
"phone":"18822346606",
"account":1215524127,
"email":"[email protected]",
"birthday":"2000-08-21",
"age":22
}
ES通过Restful请求操作索引库、文档。请求内容用DSL语句来表示
PUT /索引库名
{
"mappings":{
"properties": {
"字段1":{
"type": "keyword"
},
"字段2":{
"properties":{
"子字段1":{
"type": "keyword"
},
"子字段2":{
"type": "keyword"
}
}
},
{。。。。。。}
}
}
}
DSL如下:创建一个名为product的索引
PUT /product
{
"mappings":{
"properties": {
"id":{
"type": "keyword"
},
"proName":{
"type": "text",
"analyzer": "ik_max_word",
"copy_to": "all"
},
"proPrice":{
"type": "text",
"analyzer": "ik_max_word",
"copy_to": "all"
}
"proPrice":{
"type": "float"
},
"dateOfManufacture":{
"type": "date"
},
"manufacturer":{
"type": "keyword",
"copy_to": "all"
},
"createTime":{
"type": "date"
},
"lastModifyTime":{
"type": "date"
},
"status":{
"type": "boolean"
},
"all":{
"type": "text",
"analyzer": "ik_smart"
}
}
}
}
GET /product
DELETE /product
PUT /索引库名/_mapping
{
"properties": {
"新字段名":{
"type": "integer"
}
}
}
例:
PUT /product/_mapping
{
"properties":{
"address":{
"type":"text",
"analyzer":"ik_smart"
}
}
}
1,新增文档
POST /product/_doc/1001
{
"id":"1001",
"proName":"干脆面",
"proType":"零食方便面",
"proPrice":1.5,
"dateOfManufacture":"2022-07-15 17:57:03",
"manufacture":"东厂",
"createTime":"2022-07-16 17:57:20",
"lastModifyTime":"2022-07-17 17:57:25",
"status":true
}
2,查询文档
GET /product/_doc/1001
3,删除文档
DELETE /product/_doc/1001
1,全量修改
PUT /product/_doc/1001
{
"id":"1001",
"proName":"干脆面",
"proType":"零食方便面",
"proPrice":1.5,
"dateOfManufacture":"2022-07-15 17:57:03",
"manufacture":"东厂",
"createTime":"2022-07-16 17:57:20",
"lastModifyTime":"2022-07-17 17:57:25",
"status":true
}
2,增量修改
POST /product/_update/1001
{
"doc":{
"proPrice":1.9
}
}
RestClient是ES官方提供了各种不同语言的客户端,用来操作ES。这些客户端的本质就是组装DSL语句,通过http请求发送给ES。官方文档地址:https://www.elastic.co/guide/en/elasticsearch/client/index.html
数据库表:
项目结构:
注意一点
因为SpringBoot默认的ES版本是7.6.2,所以我们需要覆盖默认的ES版本:
初始化RestHighLevelClient:为了方便测试,我就直接写在测试里面了,执行run,没有报错则说明初始化成功。
1,创建索引库
/**
* 创建索引库
* @throws IOException
*/
@Test
void createProductIndex() throws IOException {
//1,创建Request对象
CreateIndexRequest request = new CreateIndexRequest("product");
//2,准备请求的参数:DSL语句
request.source(PRODUCT_CREATE_MAPPING, XContentType.JSON);
//3,发送请求
client.indices().create(request, RequestOptions.DEFAULT);
}
代码中的索引库创建常量就是DSL语句的这一部分:
2,删除索引库
/**
* 删除索引库
* @throws IOException
*/
@Test
void deleteHotelIndex() throws IOException {
//1,创建Request对象
DeleteIndexRequest request = new DeleteIndexRequest("product");
//2,发送请求
client.indices().delete(request, RequestOptions.DEFAULT);
}
3,判断索引库是否存在
/**
* 判断索引库是否存在
* @throws IOException
*/
@Test
void existsHotelIndex() throws IOException {
//1,创建Request对象
GetIndexRequest request = new GetIndexRequest("product");
//2,发送请求
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists?"索引库存在!":"索引库不存在!");
}
1,创建文档
/**
* 创建文档
* @throws IOException
*/
@Test
void docCreate() throws IOException {
//获取数据
Product product = productService.getById(1001L);
//获取请求对象
IndexRequest request = new IndexRequest("product").id(product.getId().toString());
//准备JSON文档
request.source(JSON.toJSONString(product),XContentType.JSON);
//发送请求
client.index(request,RequestOptions.DEFAULT);
}
2,查询文档
/**
* 查询文档
* @throws IOException
*/
@Test
void docGet() throws IOException {
//获取请求对象
GetRequest request = new GetRequest("product","1001");
//发送请求
GetResponse response = client.get(request, RequestOptions.DEFAULT);
//解析结果
String json = response.getSourceAsString();
System.out.println(json);
}
3,更新文档
/**
* 修改文档
* @throws IOException
*/
@Test
void docUpdate() throws IOException {
//获取请求对象
UpdateRequest request = new UpdateRequest("product","1001");
//准备参数
request.doc(
"proPrice",1.9,
"proType","小吃干脆面"
);
//发送请求
client.update(request,RequestOptions.DEFAULT);
}
4,删除文档
/**
* 删除文档
* @throws IOException
*/
@Test
void docDelete() throws IOException {
DeleteRequest request = new DeleteRequest("product", "1001");
//发送请求
client.delete(request,RequestOptions.DEFAULT);
}
5,文档批处理操作
/**
* 文档批处理添加
* @throws IOException
*/
@Test
void addDocBatch() throws IOException {
//获取数据库数据
List<Product> productList = productService.list();
//创建bulk请求对象
BulkRequest bulkRequest = new BulkRequest();
//遍历操作
for (Product product : productList) {
//添加文档对象
bulkRequest.add(new IndexRequest("product")
.id(product.getId().toString())
.source(JSON.toJSONString(product),XContentType.JSON));
}
//发送批处理请求
client.bulk(bulkRequest,RequestOptions.DEFAULT);
}
需要源码可查看gitee源码中的es-learn文件夹