媒介
- 由 Spring 提供,是 Spring 在 ES 官方接口基础之上的二次封装,使用简朴,易于上手;
- 缺点是更新太慢,SpringBoot 2.2.x 才提供对 es7.x 的支持,版本关联性很大,不易维护;
不外在此照旧讲一下 starter 启动器下的 集成步骤,万一官方更新速率加快了呢。
Java High Level Rest Client方式 去集成 Elasticsearch(这个是目前公司常用的方式)
添加maven依靠
- <!--spring boot 整合 elasticsearch -->
- <!--不用填写具体版本,spring boot 会自动找与之适配的 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
- </dependency>
复制代码 配置application.properties
旧版本
- server.port=8091
- spring.elasticsearch.rest.uris=http://127.0.0.1:9200
- spring.elasticsearch.rest.username=elastic
- spring.elasticsearch.rest.password=1234567
复制代码 新版本(去掉了rest)
- server.port=8091
- spring.elasticsearch.uris=http://127.0.0.1:9200
- spring.elasticsearch.username=elastic
- spring.elasticsearch.password=1234567
复制代码 测试实体类
创建一个员工的实体类
关键注解 @Document、@Id、@Field
- package com.example.springbootfull.elasticsearch.bean;
- import org.springframework.data.annotation.Id;
- import org.springframework.data.elasticsearch.annotations.DateFormat;
- import org.springframework.data.elasticsearch.annotations.Document;
- import org.springframework.data.elasticsearch.annotations.Field;
- import java.math.BigDecimal;
- import java.util.Date;
- /**
- * 员工实体类
- * @Document:作用在类上,标记实体类为文档对象。 indexName(索引名称,相当于数据库的表名称)
- * @Id:作用在成员变量上,标记一个字段作为id主键。 在Elasticsearch中,文档ID是唯一的,用于标识文档。
- * @Field:定义Java对象属性与Elasticsearch文档字段之间的映射关系。
- * @author
- */
- @Document(indexName = "employee_info")
- public class EmployeeInfo {
- @Id
- private Long id;
- /**
- * 工号
- */
- @Field(name = "job_no")
- private String jobNo;
- /**
- * 姓名
- */
- @Field(name = "name")
- private String name;
- /**
- * 英文名
- */
- @Field(name = "english_name")
- private String englishName;
- /**
- * 工作岗位
- */
- private String job;
- /**
- * 性别
- */
- private Integer sex;
- /**
- * 年龄
- */
- private Integer age;
- /**
- * 薪资
- */
- private BigDecimal salary;
- /**
- * 入职时间
- */
- @Field(name = "job_day", format = DateFormat.date_time)
- private Date jobDay;
- /**
- * 备注
- */
- private String remark;
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public String getJobNo() {
- return jobNo;
- }
- public void setJobNo(String jobNo) {
- this.jobNo = jobNo;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getEnglishName() {
- return englishName;
- }
- public void setEnglishName(String englishName) {
- this.englishName = englishName;
- }
- public String getJob() {
- return job;
- }
- public void setJob(String job) {
- this.job = job;
- }
- public Integer getSex() {
- return sex;
- }
- public void setSex(Integer sex) {
- this.sex = sex;
- }
- public Integer getAge() {
- return age;
- }
- public void setAge(Integer age) {
- this.age = age;
- }
- public BigDecimal getSalary() {
- return salary;
- }
- public void setSalary(BigDecimal salary) {
- this.salary = salary;
- }
- public Date getJobDay() {
- return jobDay;
- }
- public void setJobDay(Date jobDay) {
- this.jobDay = jobDay;
- }
- public String getRemark() {
- return remark;
- }
- public void setRemark(String remark) {
- this.remark = remark;
- }
- public EmployeeInfo() {
- }
- public EmployeeInfo(Long id, String jobNo, String name, String englishName, String job, Integer sex, Integer age, BigDecimal salary, Date jobDay, String remark) {
- this.id = id;
- this.jobNo = jobNo;
- this.name = name;
- this.englishName = englishName;
- this.job = job;
- this.sex = sex;
- this.age = age;
- this.salary = salary;
- this.jobDay = jobDay;
- this.remark = remark;
- }
- @Override
- public String toString() {
- return "EmployeeInfo{" +
- "id=" + id +
- ", jobNo='" + jobNo + '\'' +
- ", name='" + name + '\'' +
- ", englishName='" + englishName + '\'' +
- ", job='" + job + '\'' +
- ", sex=" + sex +
- ", age=" + age +
- ", salary=" + salary +
- ", jobDay=" + jobDay +
- ", remark='" + remark + '\'' +
- '}';
- }
- }
复制代码 方式一:继续 ElasticsearchRepository(适合简朴查询)
ElasticsearchRepository 提供了一个高级的抽象,使得你可以在不编写任何实当代码的情况下,直接使用预界说的CRUD方法和查询方法。
业务层接口继续 ElasticsearchRepository 类
泛型的参数分别是实体类型和主键类型
例如:
- public interface EmployeeInfoRepository extends ElasticsearchRepository<EmployeeInfo, Long> {
- }
复制代码 然后就可以直接使用了
直接使用
添加单个文档
- package com.example.springbootfull.elasticsearch.controller;
- import com.example.springbootfull.elasticsearch.bean.EmployeeInfo;
- import com.example.springbootfull.elasticsearch.service.EmployeeInfoRepository;
- import com.example.springbootfull.elasticsearch.service.EmployeeInfoService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import java.math.BigDecimal;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- @RestController
- @RequestMapping("/employeeInfo")
- public class EmployeeElasticController {
- @Autowired
- private EmployeeInfoRepository elasticRepository;
- @RequestMapping("/save")
- public String save() throws Exception {
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
- EmployeeInfo employeeInfo = new EmployeeInfo(6001L, "2001", "张三", "zhangsan", "Java", 1, 19, new BigDecimal("12500.01"), simpleDateFormat.parse("2019-09-10"), "备注");
- elasticRepository.save(employeeInfo);
- return "success";
- }
-
- }
复制代码 执行后,es上面新增成功了。
想自界说自己的Repository接口
就要遵守 自界说方法命名规范
【自界说方法命名约定】:
例如:我们来按照年龄区间查询,界说如许的一个方法findByAgeBetween:
- /**
- * 需要继承ElasticsearchRepository接口
- * 由于Item实体类中id为Long类型
- */
- public interface EmployeeInfoRepository extends ElasticsearchRepository<EmployeeInfo, Long>
-
- /**
- * 方法名必须遵守SpringData的规范
- * 年龄区间查询
- */
- List<EmployeeInfo> findByAgeBetween(int age1, int age2);
- }
复制代码 然后,再通过saveAll新增多些数据,再进行自界说的方法进行查询
- package com.example.springbootfull.elasticsearch.controller;
- import com.example.springbootfull.elasticsearch.bean.EmployeeInfo;
- import com.example.springbootfull.elasticsearch.service.EmployeeInfoRepository;
- import com.example.springbootfull.elasticsearch.service.EmployeeInfoService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import java.math.BigDecimal;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- @RestController
- @RequestMapping("/employeeInfo")
- public class EmployeeElasticController {
- @Autowired
- private EmployeeInfoRepository elasticRepository;
- @RequestMapping("/saveAll")
- public String saveAll() throws Exception {
- List<EmployeeInfo> list = new ArrayList<>();
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
- list.add(new EmployeeInfo(1001L, "2001", "张三", "zhangsan", "Java", 1, 19, new BigDecimal("12500.01"), simpleDateFormat.parse("2019-09-10"), "备注"));
- list.add(new EmployeeInfo(1002L, "2002", "李四", "lisi", "PHP", 1, 18, new BigDecimal("11600.01"), simpleDateFormat.parse("2019-09-10"), "备注"));
- list.add(new EmployeeInfo(1003L, "2003", "王五", "wangwu", "C++", 1, 20, new BigDecimal("9900.01"), simpleDateFormat.parse("2019-09-10"), "备注"));
- list.add(new EmployeeInfo(1004L, "2004", "赵六", "zhaoliu", "Java Leader", 1, 20, new BigDecimal("20000.01"), simpleDateFormat.parse("2019-09-10"), "备注"));
- list.add(new EmployeeInfo(1005L, "2005", "小五", "xiaowu", "H5", 1, 17, new BigDecimal("10600.01"), simpleDateFormat.parse("2019-09-10"), "备注"));
- list.add(new EmployeeInfo(1006L, "2006", "小六", "xaioliu", "web", 1, 20, new BigDecimal("12600.01"), simpleDateFormat.parse("2019-09-10"), "备注"));
- list.add(new EmployeeInfo(1007L, "2007", "小七", "xiaoqi", "app", 1, 22, new BigDecimal("20000.01"), simpleDateFormat.parse("2019-09-10"), "备注"));
- list.add(new EmployeeInfo(1008L, "2008", "小八", "xaioba", "Java", 1, 21, new BigDecimal("11000.01"), simpleDateFormat.parse("2019-09-10"), "备注"));
- list.add(new EmployeeInfo(1009L, "2009", "小九", "xiaojiu", "Java", 1, 20, new BigDecimal("14000.01"), simpleDateFormat.parse("2019-09-10"), "备注"));
- list.add(new EmployeeInfo(1010L, "2010", "大十", "dashi", "Java", 1, 20, new BigDecimal("13000.01"), simpleDateFormat.parse("2019-09-10"), "备注"));
- elasticRepository.saveAll(list);
- return "success -> " + list.size();
- }
- @RequestMapping("/findByAgeBetween")
- public String findByAgeBetween(){
- elasticRepository.findByAgeBetween(10,20);
- return "success";
- }
-
- }
复制代码 使用 findByAgeBetween 查询后的结果
方式二:使用ElasticsearchRestTemplate(更适合用于复杂查询)
与 ElasticsearchRepository 相比,ElasticsearchRestTemplate 更适合用于复杂查询 。
比如 多个条件组合、范围查询、模糊查询、聚合查询等复杂场景
还支持分页、排序、过滤等高级功能
添加单个文档
- @RestController
- @RequestMapping("/employeeInfo")
- public class EmployeeElasticController {
- @Autowired
- private ElasticsearchRestTemplate elasticsearchRestTemplate;
-
- @RequestMapping("/template/save")
- public String templateSave() throws Exception {
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
- EmployeeInfo employeeInfo = new EmployeeInfo(8888L, "2001", "张八", "zhangsan", "Java", 1, 19, new BigDecimal("12500.01"), simpleDateFormat.parse("2019-09-10"), "备注");
- elasticsearchRestTemplate.save(employeeInfo);
- return "success";
- }
复制代码
简朴查询-通过id
- @RestController
- @RequestMapping("/employeeInfo")
- public class EmployeeElasticController {
- @Autowired
- private ElasticsearchRestTemplate elasticsearchRestTemplate;
-
- @RequestMapping("/getEmployeeInfo")
- public EmployeeInfo getEmployeeInfo() {
- return elasticsearchRestTemplate.get("6001", EmployeeInfo.class);
- }
复制代码 查询结果如下
批量添加、删除、复杂的查询 等等 其余的我就不多说了
【参考文章】
【1】SpringBoot-starter-data整合Elasticsearch
【2】SpringBoot之ElasticsearchRestTemplate常用示例
【3】Spring Data Elasticsearch篇(3):ElasticsearchRepository文档操作
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |