一、配置
pom依赖
引入pom依赖,我引用的版本中引入的elasticsearch版本是7.6.X。
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-elasticsearch -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>2.3.9.RELEASE</version>
</dependency>
配置文件:
spring:
elasticsearch:
rest:
uris: http://127.0.0.1:9200
二、创建测试代码
实体类
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
/**
* @description: 测试es实体
* @author:weirx
* @date:2021/4/12 15:33
* @version:3.0
*/
@Data
@Document(indexName = "test_es", shards = 3, replicas = 1)
public class TestEsDO {
@Id
private Long id;
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String name;
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String desc;
}
mapper
import com.cloud.bssp.user.entity.TestEsDO;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
/**
* @description: 测试es持久层
* @author:weirx
* @date:2021/4/12 15:35
* @version:3.0
*/
@Repository
public interface TestEsMapper extends ElasticsearchRepository<TestEsDO, Long> {
}
service
import com.cloud.bssp.user.entity.TestEsDO;
import java.util.List;
/**
* description: es测试service
*
* @author: weirx
* @time: 2021/4/12 16:28
*/
public interface TestEsService {
List<TestEsDO> getAll();
TestEsDO getById(Long id);
TestEsDO save(TestEsDO testEsDO);
void delete(Long id);
Page<TestEsDO> findByPageable();
}
serviceImpl
import java.util.List;
/**
* Description:
* Create Date: 2021-03-24T14:09:18.513
* Modified By:<br>
* Modified Date:<br>
* Why & What is modified:<br>
*
* @author weirx
* @version 1.0
*/
@Service
public class TestEsServiceImpl implements TestEsService {
@Autowired
private TestEsMapper testEsMapper;
@Override
public List<TestEsDO> getAll() {
Iterable<TestEsDO> all = testEsMapper.findAll();
List<TestEsDO> testEsDOS = IterUtil.toList(all);
return testEsDOS;
}
@Override
public TestEsDO getById(Long id) {
return testEsMapper.findById(id).get();
}
@Override
public TestEsDO save(TestEsDO testEsDO) {
return testEsMapper.save(testEsDO);
}
@Override
public void delete(Long id) {
testEsMapper.deleteById(id);
}
@Override
public Page<TestEsDO> findByPageable() {
//设置排序(排序方式,正序还是倒序,排序的 id)
Sort sort = Sort.by(Sort.Direction.DESC, "id");
//当前页,第一页从 0 开始,1 表示第二页
int currentPage = 0;
//每页显示多少条
int pageSize = 5;
//设置查询分页
PageRequest pageRequest = PageRequest.of(currentPage, pageSize, sort);
//查询条件
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "elasticsearch");
//分页查询
Page<TestEsDO> page = testEsMapper.search(termQueryBuilder,pageRequest);
return page;
}
}
controller
import com.cloud.bssp.user.dto.UserDTO;
import com.cloud.bssp.user.entity.TestEsDO;
import com.cloud.bssp.user.service.TestEsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* description: es测试控制器
*
* @author: weirx
* @time: 2021/4/12 16:29
*/
@Slf4j
@RestController
@RequestMapping("/es")
public class TestEsController {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
@Autowired
private TestEsService testEsService;
@RequestMapping("/getAll")
public List<TestEsDO> getAll() {
return testEsService.getAll();
}
@RequestMapping("/getById")
public TestEsDO getById(Long id) {
return testEsService.getById(id);
}
@RequestMapping("/save")
public TestEsDO save() {
TestEsDO testEsDO = new TestEsDO();
testEsDO.setId(1000L);
testEsDO.setName("elasticsearch");
testEsDO.setDesc("hello elasticsearch, 你好,elasticsearch");
return testEsService.save(testEsDO);
}
@RequestMapping("/delete")
public void delete(Long id) {
testEsService.delete(id);
}
@RequestMapping("/createIndex")
public void createIndex() {
elasticsearchRestTemplate.createIndex(TestEsDO1.class);
elasticsearchRestTemplate.putMapping(TestEsDO1.class);
}
@RequestMapping("/page")
public Page<TestEsDO> page() {
return testEsService.findByPageable();
}
}
三、测试
启动项目
关注以下两条日志信息,发现一条创建索引,一条是设置mapping的,索引名称是在实体类上配置的名称。
2021-04-12 16:34:02.949 WARN 36752 --- [/O dispatcher 1] o.e.c.RestClient : request [PUT http://127.0.0.1:9200/test_es?master_timeout=30s&include_type_name=true&timeout=30s] returned 1 warnings: [299 Elasticsearch-7.9.2-d34da0ea4a966c4e49417f2da2f244e3e97b4e6e "[types removal] Using include_type_name in create index requests is deprecated. The parameter will be removed in the next major version."]
2021-04-12 16:34:03.031 WARN 36752 --- [/O dispatcher 1] o.e.c.RestClient : request [PUT http://127.0.0.1:9200/test_es/_mapping/testesdo?master_timeout=30s&include_type_name=true&timeout=30s] returned 1 warnings: [299 Elasticsearch-7.9.2-d34da0ea4a966c4e49417f2da2f244e3e97b4e6e "[types removal] Using include_type_name in put mapping requests is deprecated. The parameter will be removed in the next major version."]
使用restTemplate创建一条索引,并创建mapping。
复制一份实体类:
package com.cloud.bssp.user.entity;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
/**
* @description: 测试es实体
* @author:weirx
* @date:2021/4/12 15:33
* @version:3.0
*/
@Data
@Document(indexName = "test_es_1", shards = 3, replicas = 1)
public class TestEsDO1 {
@Id
private Long id;
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String name;
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String desc;
}
调用http://localhost:8088/es/createIndex,查看kibana信息。
kibana新建数据
调用http://localhost:8088/es/save,查看kibana结果:
kibana根据id获取
调用http://localhost:8088/es/getById?id=1000,返回结果:
获取全部
调用http://localhost:8088/es/getAll,返回结果:
删除文档信息
调用http://localhost:8088/es/delete?id=1000,查看kibana:
分页条件查询
调用http://localhost:8088/es/page,查看结果:
结果
网友评论