前言
这里默认读者已经安装好ElasticSearch,因为本文不会涉及到ElasticSearch的安装
在整合SpringBoot之前,需要先确定版本,以下是版本对应表(摘自官方)
导入依赖
我这里使用的是最新的版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
yml配置
spring:
elasticsearch:
uris: 43.142.61.XXX:9200 #最新的版本只需要这样配置就可以连接上es
实体类
package com.th.elasticsearch.pojo;
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;
@Data
@Document(indexName = "user") //user是索引的名称
public class User {
@Id
private int id;
@Field(type = FieldType.Keyword)
private String name;
@Field(type = FieldType.Keyword)
private int age;
@Field(type = FieldType.Boolean)
private boolean sex;
}
mapper
在Spring中,操作es像操作数据库一样
package com.th.elasticsearch.mapper;
import com.th.elasticsearch.pojo.User;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface UserMapper extends ElasticsearchRepository<User, Integer> {
//自定义查询方法
List<User> findAllByNameAndAge(String name,Integer age);
}
自定义查询方法的方法名有一套自己的规则,与Spring Data JPA类似,下面是一部分规则,更多的规则可到官网进行查询
Keyword | Sample | Elasticsearch Query String |
---|---|---|
And | findByNameAndPrice | {“bool” : {“must” : [ {“field” : {“name” : “?”}}, {“field” : {“price” : “?”}} ]}} |
Or | findByNameOrPrice | {“bool” : {“should” : [ {“field” : {“name” : “?”}}, {“field” : {“price” : “?”}} ]}} |
Is | findByName | {“bool” : {“must” : {“field” : {“name” : “?”}}}} |
Not | findByNameNot | {“bool” : {“must_not” : {“field” : {“name” : “?”}}}} |
Between | findByPriceBetween | {“bool” : {“must” : {“range” : {“price” : {“from” : ?,“to” : ?,“include_lower” : true,“include_upper” : true}}}}} |
Like | findByNameLike | {“bool” : {“must” : {“field” : {“name” : {“query” : “?*”,“analyze_wildcard” : true}}}}} |
In | findByNameIn(Collectionnames) | {“bool” : {“must” : {“bool” : {“should” : [ {“field” : {“name” : “?”}}, {“field” : {“name” : “?”}} ]}}}} |
OrderBy | findByAvailableTrueOrderByNameDesc | {“sort” : [{ “name” : {“order” : “desc”} }],“bool” : {“must” : {“field” : {“available” : true}}}} |
另外,由于与jap类似,所以是可以使用@Query
的,这个可以自行去探索,本文只进行最简单的入门级整合。
测试
package com.th.elasticsearch;
import com.th.elasticsearch.mapper.UserMapper;
import com.th.elasticsearch.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import javax.annotation.Resource;
import java.util.List;
@SpringBootTest
class ElasticSearchApplicationTests {
@Resource
private ElasticsearchRestTemplate template;
@Autowired
private UserMapper userMapper;
//创建索引
@Test
public void createIndex() {
//创建User对象的索引,事实上,系统初始化会自动创建索引
boolean b = template.indexOps(User.class).create();
System.out.println(b);
}
//删除索引
@Test
public void deleteIndex() {
boolean b = template.indexOps(User.class).delete();
System.out.println(b);
}
//新增文档
@Test
public void createMapping() {
User user = new User();
user.setId(1);
user.setName("张三");
user.setAge(18);
user.setSex(false);
User save = userMapper.save(user);
System.out.println(save);
}
//查看全部文档
@Test
public void findAll() {
Iterable<User> all = userMapper.findAll();
System.out.println(all);
}
//按条件查询
@Test
public void findCondition() {
//按照条件查询,需要自己去自定义查询方法
//这里使用自定义的根据姓名和价格的查询方法
List<User> list = userMapper.findAllByNameAndAge("张三", 18);
System.out.println(list);
}
}
至此整合Spring Boot就结束了,当然这只是最简单的入门级操作,一些更高级的操作需要在此基础上进行拓展。