当前位置:   article > 正文

SpringBoot整合ElasticSearch

SpringBoot整合ElasticSearch

目录

版本选择

引入依赖

yml配置

创建实体

实现ElasticsearchRepository

测试

使用ElasticsearchRestTemplate


版本选择

Elasticsearch 7.17.3 对应依赖 Spring Data Elasticsearch 4.4.x,对应springboot版本2.7.x

引入依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
  4. </dependency>

yml配置

  1. spring:
  2. elasticsearch:
  3. uris: http://localhost:9200
  4. connection-timeout: 3s

创建实体

  1. @Data
  2. @AllArgsConstructor
  3. @Document(indexName = "employees")
  4. public class Employee {
  5. @Id
  6. private Long id;
  7. @Field(type= FieldType.Keyword)
  8. private String name;
  9. private int sex;
  10. private int age;
  11. @Field(type= FieldType.Text,analyzer="ik_max_word")
  12. private String address;
  13. private String remark;
  14. }

实现ElasticsearchRepository

该接口是框架封装的用于操作Elastsearch的高级接口

  1. @Repository
  2. public interface EmployeeRepository extends ElasticsearchRepository<Employee, Long> {
  3. List<Employee> findByName(String name);
  4. }

测试

  1. @Autowired
  2. EmployeeRepository employeeRepository;
  3. @Test
  4. public void testDocument(){
  5. Employee employee = new Employee(1L,"666",1,18,"北京","java");
  6. //插入文档
  7. employeeRepository.save(employee);
  8. //根据id查询
  9. Optional<Employee> result = employeeRepository.findById(1L);
  10. log.info(String.valueOf(result.get()));
  11. //根据name查询
  12. List<Employee> list = employeeRepository.findByName("666");
  13. log.info(String.valueOf(list.get(0)));
  14. }

使用ElasticsearchRestTemplate

       ElasticsearchRestTemplate模板类,封装了便捷操作Elasticsearch的模板方法,包括索引 / 映射 / CRUD 等底层操作和高级操作。

  1. @Autowired
  2. ElasticsearchRestTemplate elasticsearchRestTemplate;
  1. @Test
  2. public void testCreateIndex(){
  3. //创建索引
  4. IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(IndexCoordinates.of("employee_index"));
  5. if (indexOperations.exists()) {
  6. log.info("索引已经存在");
  7. }else {
  8. //创建索引
  9. indexOperations.create();
  10. }
  11. }
  12. @Test
  13. public void testDeleteIndex(){
  14. //删除索引
  15. IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(IndexCoordinates.of("employee_index"));
  16. indexOperations.delete();
  17. }

文档操作

  1. @Test
  2. public void testQueryDocument(){
  3. NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
  4. //查询
  5. builder.withQuery(QueryBuilders.matchQuery("address","公园"));
  6. // 设置分页信息
  7. builder.withPageable(PageRequest.of(0, 5));
  8. // 设置排序
  9. builder.withSort(SortBuilders.fieldSort("age").order(SortOrder.DESC));
  10. SearchHits<Employee> search = elasticsearchRestTemplate.search(builder.build(), Employee.class);
  11. List<SearchHit<Employee>> searchHits = search.getSearchHits();
  12. for (SearchHit hit: searchHits){
  13. log.info("返回结果:"+hit.toString());
  14. }
  15. }
  16. @Test
  17. public void testInsertBatch(){
  18. List<Employee> employees = new ArrayList<>();
  19. employees.add(new Employee("2","张三",1,25,"北京","java"));
  20. employees.add(new Employee("3","李四",1,28,"山西","pytion"));
  21. employees.add(new Employee("4","小红",0,26,"天津","php"));
  22. List<IndexQuery> queries = new ArrayList<>();
  23. for (Employee employee : employees) {
  24. IndexQuery indexQuery = new IndexQuery();
  25. indexQuery.setId(employee.getId());
  26. String json = JSONObject.toJSONString(employee);
  27. indexQuery.setSource(json);
  28. queries.add(indexQuery);
  29. }
  30. //bulk批量插入
  31. elasticsearchRestTemplate.bulkIndex(queries,Employee.class);
  32. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/130553
推荐阅读
相关标签
  

闽ICP备14008679号