Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to integrate Elasticsearch with Springboot

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to integrate Springboot Elasticsearch, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

Integrated configuration step 1: join Maven related dependencies org.springframework.boot spring-boot-starter-parent 2.5.7 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-elasticsearch Org.projectlombok lombok true step 2: configure the host and port of elasticsearch elasticsearch: host: 127.0.0.1 port: 9200

Step 3: configure the Elaseticsearch client @ 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));}}

Step 4: create a document entity @ 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;}

Step 5: create the controller,service, dao layer

(5.1) contrller layer

@ RestController@RequestMapping ("/ user") public class UserController {@ Autowired private UserService userService;}

(5.2) service layer

Public interface UserService {}

(5.3) service implementation layer (UserDao see 5.4)

@ Servicepublic class UserServiceImpl implements UserService {@ Autowired private UserDao userDao;}

(5.4) dao layer

@ Repositorypublic interface UserDao extends ElasticsearchRepository {}

After the entire related class is created, the project structure should look like the following figure:

Realization of related functions

This paper gives an example of the actual function, and lists the implementation class writing of the controller and service layers as a reference.

Some mainstream functions have been encapsulated by ES and supported by default (such as example 1-6). These related implementations do not need us to write the dao layer, but some of the more business queries need to be written manually.

1. Add document

Controller layer:

@ PostMapping ("/ save") public String save (@ RequestBody User user) {long id = System.currentTimeMillis (); user.setId (id); userService.save (user); return "added successfully, id is:" + id;}

Service layer:

@ Overridepublic Long save (User user) {userDao.save (user); return user.getId ();}

Test results:

two。 Modify the document

[note] to add and modify documents, the dao layer calls the save () method. When id does not exist, ES (ElasticSearch) will perform the new operation, and when the id object already exists, it will perform the modification operation.

Controller layer:

@ PostMapping ("/ update") public String update (@ RequestBody User user) {userService.save (user); return "modified successfully";}

Service layer:

@ Overridepublic Long save (User user) {userDao.save (user); return user.getId ();}

Test results:

3. Query documents according to ID

Controller layer:

@ GetMapping ("/ {id}") public User getById (@ PathVariable ("id") Long id) {return userService.getById (id);}

Service layer:

@ Overridepublic User getById (Long id) {Optional find = userDao.findById (id); if (find.isPresent ()) {return find.get ();} return null;}

Test results:

4. Delete documents according to ID

Controller layer:

@ DeleteMapping ("/ {id}") public String deleteById (@ PathVariable ("id") Long id) {userService.deleteById (id); return "deleted successfully";}

Service layer:

@ Overridepublic void deleteById (Long id) {userDao.deleteById (id);}

Test results:

5. Query all documents

Controller layer:

@ GetMapping ("/ all") public List all () {return userService.getAll ();}

Service layer:

@ Overridepublic List getAll () {Iterable users = userDao.findAll (); if (users = = null) {return Collections.emptyList ();} List userList = new ArrayList (); users.forEach (o-> userList.add (o)); return userList;}

Test results:

6. Conditional query (single condition)

For example, query name contains "Mary" data

Controller layer:

@ GetMapping ("/ listByName") public List listByName (String name) {return userService.getListByName (name);}

Service layer:

@ Overridepublic List getListByName (String name) {return userDao.findByName (name);}

Dao layer: test results:

7. Conditional query (multiple conditions)

For example, the query name contains "Mary", age 25 data.

Controller layer:

@ GetMapping ("/ listByNameAndAge") public List listByNameAndAge (String name, Integer age) {return userService.getListByNameAndAge (name, age);}

Service layer:

Overridepublic List getListByNameAndAge (String name, Integer age) {return userDao.findByNameAndAge (name, age);}

Dao layer:

List findByNameAndAge (String name, Integer age)

8. Paging query (descending)

Controller layer:

@ GetMapping ("/ listOrderByAgeDesc") public List listOrderByAgeDesc () {return userService.getListOrderByAgeDesc ();}

Service layer:

@ Overridepublic List getListOrderByAgeDesc () {return userDao.findByOrderByAgeDesc ();}

Dao layer:

List findByOrderByAgeDesc ()

Test results:

9. Paging query (ascending order)

Controller layer:

@ GetMapping ("/ listOrderByAgeAsc") public List listOrderByAgeAsc () {return userService.getListOrderByAgeAsc ();}

Service layer:

@ Overridepublic List getListOrderByAgeAsc () {return userDao.findByOrderByAgeAsc ();}

Dao layer:

List findByOrderByAgeAsc ()

Test results:

10. Paging query

Controller layer:

@ GetMapping ("/ page") public Page page (Integer pageNum, Integer pageSize) {return userService.getPage (pageNum, pageSize);}

Service layer:

@ Overridepublic Page getPage (int pageNum, int pageSize) {/ / Note that pageNum starts with 0. If you want to query page 1 data, pageNum should be 0 Pageable pageable = PageRequest.of (pageNum, pageSize); return userDao.findAll (pageable);}

Test results:

11. Range query (>)

Controller layer:

@ GetMapping ("/ listGreaterThanAge") public List listGreaterThanAge (Integer age) {return userService.getListGreaterThanAge (age);}

Service layer:

@ Overridepublic List getListGreaterThanAge (Integer age) {return userDao.findByAgeGreaterThan (age);}

Dao layer:

List findByAgeGreaterThan (Integer age)

Test results:

twelve。 Range query (> =)

Controller layer:

@ GetMapping ("/ listGreaterThanEqualAge") public List listGreaterThanEqualAge (Integer age) {return userService.getListGreaterThanEqualAge (age);}

Service layer:

@ Overridepublic List getListGreaterThanEqualAge (Integer age) {return userDao.findByAgeGreaterThanEqual (age);}

Dao layer:

List findByAgeGreaterThanEqual (Integer age)

Test results:

13. Range query (<)

Controller layer:

@ GetMapping ("/ listLessThanAge") public List listLessThanAge (Integer age) {return userService.getListLessThanAge (age);}

Service layer:

@ Overridepublic List getListLessThanAge (Integer age) {return userDao.findByAgeLessThan (age);}

Dao layer:

List findByAgeLessThan (Integer age)

Test results:

14. Range query (

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report