您好,登录后才能下订单哦!
小编给大家分享一下Springboot如何集成Elasticsearch,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
<!-- 指定 Springboot 版本 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.7</version> <relativePath/> </parent> <!-- 相关jar包 --> <dependencies> <!-- SpringBoot核心jar包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- web开发包:包含Tomcat和Springmvc --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- ES 核心包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> <!-- lombok 实体简化插件 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies>
elasticsearch: host: 127.0.0.1 port: 9200
@Data @Configuration @ConfigurationProperties(prefix = "elasticsearch") public class ElasticSearchConfig extends AbstractElasticsearchConfiguration { private String host; private Integer port; @Override public RestHighLevelClient elasticsearchClient() { return new RestHighLevelClient(RestClient.builder(new HttpHost(host, port))); } }
@Data @Document(indexName = "users") public class User { @Id @Field(type = FieldType.Long) private Long id; @Field(type = FieldType.Text) private String name; @Field(type = FieldType.Keyword) private String sex; @Field(type = FieldType.Integer) private Integer age; @Field(type = FieldType.Text) private String address; }
(5.1)contrller 层
@RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; }
(5.2)service 层
public interface UserService { }
(5.3)service 实现层(UserDao 见 5.4)
@Service public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; }
(5.4)dao 层
@Repository public interface UserDao extends ElasticsearchRepository<User, Long> { }
整个相关类创建完成后,项目结构应该如下图所示:
这里以实际功能举例,并列出 controller 和 service 层的实现类写法作为参考。
主流的一些功能,ES 已经给你封装,默认给与了支持(如:示例1-6),这些相关的实现不需要我们再写 dao 层,但一些较为业务性的查询,还需要自己手动来写。
controller 层:
@PostMapping("/save") public String save(@RequestBody User user) { long id = System.currentTimeMillis(); user.setId(id); userService.save(user); return "新增成功,id为:" + id; }
service 层:
@Override public Long save(User user) { userDao.save(user); return user.getId(); }
测试效果:
【注】新增文档和修改文档操作,dao 层都是调用的 save() 方法,当 id 不存在的时候,ES(ElasticSearch)就会执行新增操作,当 id 对象已经存在的时候,就会执行修改操作。
controller 层:
@PostMapping("/update") public String update(@RequestBody User user) { userService.save(user); return "修改成功"; }
service 层:
@Override public Long save(User user) { userDao.save(user); return user.getId(); }
测试效果:
controller 层:
@GetMapping("/{id}") public User getById(@PathVariable("id") Long id) { return userService.getById(id); }
service 层:
@Override public User getById(Long id) { Optional<User> find = userDao.findById(id); if (find.isPresent()) { return find.get(); } return null; }
测试效果:
controller 层:
@DeleteMapping("/{id}") public String deleteById(@PathVariable("id") Long id) { userService.deleteById(id); return "删除成功"; }
service 层:
@Override public void deleteById(Long id) { userDao.deleteById(id); }
测试效果:
controller 层:
@GetMapping("/all") public List<User> all() { return userService.getAll(); }
service 层:
@Override public List<User> getAll() { Iterable<User> users = userDao.findAll(); if (users == null) { return Collections.emptyList(); } List<User> userList = new ArrayList<User>(); users.forEach(o -> userList.add(o)); return userList; }
测试效果:
如:查询name包含"玛丽"的数据
controller 层:
@GetMapping("/listByName") public List<User> listByName(String name) { return userService.getListByName(name); }
service 层:
@Override public List<User> getListByName(String name) { return userDao.findByName(name); }
dao 层:测试效果:
如:查询 name包含"玛丽",年龄为25的数据。
controller 层:
@GetMapping("/listByNameAndAge") public List<User> listByNameAndAge(String name, Integer age) { return userService.getListByNameAndAge(name, age); }
service 层:
@Override public List<User> getListByNameAndAge(String name, Integer age) { return userDao.findByNameAndAge(name, age); }
dao 层:
List<User> findByNameAndAge(String name, Integer age);
controller 层:
@GetMapping("/listOrderByAgeDesc") public List<User> listOrderByAgeDesc() { return userService.getListOrderByAgeDesc(); }
service 层:
@Override public List<User> getListOrderByAgeDesc() { return userDao.findByOrderByAgeDesc(); }
dao 层:
List<User> findByOrderByAgeDesc();
测试效果:
controller 层:
@GetMapping("/listOrderByAgeAsc") public List<User> listOrderByAgeAsc() { return userService.getListOrderByAgeAsc(); }
service 层:
@Override public List<User> getListOrderByAgeAsc() { return userDao.findByOrderByAgeAsc(); }
dao 层:
List<User> findByOrderByAgeAsc();
测试效果:
controller 层:
@GetMapping("/page") public Page<User> page(Integer pageNum, Integer pageSize) { return userService.getPage(pageNum, pageSize); }
service 层:
@Override public Page<User> getPage(int pageNum, int pageSize) { // 注意,这里的pageNum是从0开始的,如果要查询第1页数据,pageNum应该为0 Pageable pageable = PageRequest.of(pageNum, pageSize); return userDao.findAll(pageable); }
测试效果:
controller 层:
@GetMapping("/listGreaterThanAge") public List<User> listGreaterThanAge(Integer age) { return userService.getListGreaterThanAge(age); }
service 层:
@Override public List<User> getListGreaterThanAge(Integer age) { return userDao.findByAgeGreaterThan(age); }
dao 层:
List<User> findByAgeGreaterThan(Integer age);
测试效果:
controller 层:
@GetMapping("/listGreaterThanEqualAge") public List<User> listGreaterThanEqualAge(Integer age) { return userService.getListGreaterThanEqualAge(age); }
service 层:
@Override public List<User> getListGreaterThanEqualAge(Integer age) { return userDao.findByAgeGreaterThanEqual(age); }
dao 层:
List<User> findByAgeGreaterThanEqual(Integer age);
测试效果:
controller 层:
@GetMapping("/listLessThanAge") public List<User> listLessThanAge(Integer age) { return userService.getListLessThanAge(age); }
service 层:
@Override public List<User> getListLessThanAge(Integer age) { return userDao.findByAgeLessThan(age); }
dao 层:
List<User> findByAgeLessThan(Integer age);
测试效果:
controller 层:
@GetMapping("/listLessThanEqualAge") public List<User> listLessThanEqualAge(Integer age) { return userService.getListLessThanEqualAge(age); }
service 层:
@Override public List<User> getListLessThanEqualAge(Integer age) { return userDao.findByAgeLessThanEqual(age); }
dao 层:
List<User> findByAgeLessThanEqual(Integer age);
测试效果:
1. 获取Elasticsearch中所有的index:
curl -XGET 'localhost:9200/_cat/indices?v&pretty'
2. 获取某索引下所有的type:
获取elasticsearch索引下所有的type定义
curl -XGET 'localhost:9200/elasticsearch/_mapping'
3. 删除索引:
删除elasticsearch2索引
curl -XDELETE 'localhost:9200/elasticsearch2?pretty'
4. 删除某索引下的某type数据:
删除索引elasticsearch下的article类型
curl -XDELETE 'http://localhost:9200/elasticsearch/article/'
5. 查看某索引的某type下所有的数据:
查找elasticsearch索引中所有的类型为Company的数据
http://localhost:9200/elasticsearch/Company/_search
以上是“Springboot如何集成Elasticsearch”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。