1.搭建ES的服务
导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>配置ES
...
spring:
application:
name: hrm-es-service
data:
elasticsearch:
cluster-name: elasticsearch
cluster-nodes: 127.0.0.1:9300 #9200是图形界面端,9300代码端
...
2.创建EmployeeDoc
/**
* 针对于 Employee 表的文档映射
* indexName:索引库
* type:类型(表类型)
*/
@Document(indexName = "hrm" , type = "employee")
public class EmployeeDoc {
//对应文档的id
@Id
private Long id;
@Field(type = FieldType.Keyword) //指定为 不分词
private String userName;
private int age;
@Field(type =FieldType.Text,analyzer = "ik_max_word",searchAnalyzer = "ik_max_word")
private String intr;
...
3.创建索引库
@RunWith(SpringRunner.class)
@SpringBootTest(classes = EsServiceApplication2050.class)
public class ESTest {
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Test
public void testCreateIndex() {
//创建索引
elasticsearchTemplate.createIndex(EmployeeDoc.class);
//做文档映射
elasticsearchTemplate.putMapping(EmployeeDoc.class);
}
}
4.定义ElasticsearchRepository
@Repository
public interface EmployeeElasticsearchRepository extends ElasticsearchRepository<EmployeeDoc,Long> {}
5.基本CRUD
@Autowired
private EmployeeElasticsearchRepository employeeElasticsearchRepository;
//初始化
@Test
public void testCreateIndex() {
//创建索引
elasticsearchTemplate.createIndex(EmployeeDoc.class);
//做文档映射
elasticsearchTemplate.putMapping(EmployeeDoc.class);
}
//添加数据
@Test
public void testAdd(){
//准备数据
EmployeeDoc employeeDoc = new EmployeeDoc();
employeeDoc.setAge(18);
employeeDoc.setUserName("王大锤");
employeeDoc.setIntr("我爱中国");
//添加数据到es
employeeElasticsearchRepository.save(employeeDoc);
}
//添加数据
@Test
public void testUpdate(){
//准备数据
EmployeeDoc employeeDoc = new EmployeeDoc();
employeeDoc.setId(5L); //有id就是修改
employeeDoc.setAge(18);
employeeDoc.setUserName("周伯通");
employeeDoc.setIntr("深得童子通真传");
//添加数据到es
employeeElasticsearchRepository.save(employeeDoc);
}
//删除数据
@Test
public void testDelete(){
employeeElasticsearchRepository.deleteById(5L);
}
//获取数据
@Test
public void testGet(){
Optional<EmployeeDoc> optional = employeeElasticsearchRepository.findById(5L);
System.out.println(optional.get());
}
6.高级查询分页
//通过索引库,高级查询和分页
/**
* 用户名为 隔壁老王
* age在 10 - 20
* 按照 id排序降序
* 分页 每页 2条 取第一页
*/
@Test
public void testSearchAndPage() throws Exception{
//1.创建查询建造器对象
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
//2.给查询建造器添加查询条件对象
//1.创建组合查询条件对象
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
//2.添加查询条件:分词查询:username
List<QueryBuilder> must= boolQuery.must();
must.add(QueryBuilders.matchQuery("username","周伯通" ));
//2.添加查询条件:范围查询:age:10到30
List<QueryBuilder> filter = boolQuery.filter();
filter.add(QueryBuilders.rangeQuery("age").lte(30).gte(10));
//3.给查询建造器添加查询条件对象
queryBuilder.withQuery(boolQuery);
//3.给查询建造器添加分页对象:从第0页开始,每页2条
queryBuilder.withPageable(PageRequest.of(0,2 ));
//4.给查询建造器添加排序
//1.创建排序对象:id,降序
FieldSortBuilder sortBuilder = new FieldSortBuilder("id").order(SortOrder.DESC);
//2.给查询建造器添加排序
queryBuilder.withSort(sortBuilder);
//5.创建查询对象
NativeSearchQuery searchQuery = queryBuilder.build();
//6.传给search方法,执行高级查询分页排序功能,得到分页对象
Page<EmployeeDoc> page = employeeElasticsearchRepository.search(searchQuery);
//7.获取结果
//获得总条数
int totalPages = page.getTotalPages();
System.out.println(totalPages);
//获得当前页数据
Iterator<EmployeeDoc> iterator = page.getContent().iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}