In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to integrate elasticsearch in springboot". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to integrate elasticsearch in springboot" can help you solve the problem.
1. Introduce dependency
Org.springframework.boot spring-boot-starter-data-elasticsearch
2. Write entity mapping classes
Data@Document (indexName = "index", createIndex = true) public class Index {@ Id private String id; @ Field (type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart") private String content;}
3, write the access interface (if you need to create the index automatically, the interface must be written, otherwise the index will not be automatically detected and created when the project starts)
Repositorypublic interface IndexRepository extends ElasticsearchRepository {Page findByContent (String content, Pageable page);}
4. Test, using template and repository to test
@ SpringBootTestpublic class EsTest {@ Autowired ElasticsearchRestTemplate esTemplate; @ Autowired IndexRepository indexRepository; @ BeforeEach public void init () {System.out.println ("init"); indexRepository.deleteAll () IndexRepository.saveAll (ListUtil.of (new Index ("1", "is it a mess left by the United States to Iraq"), new Index ("2", "Ministry of Public Security: local school buses will enjoy the highest right of way"), new Index ("3", "conflict investigation between Chinese and South Korean fishing police: South Korean police detain an average of one Chinese fishing boat a day") New Index ("4", "suspect shot by an Asian man at the Chinese Consulate in Los Angeles has surrendered"), new Index ("5", "China's Heavenly Eye officially opens to the world next month to compete in FAST pulsar search")) @ Test void testRepositoryQuery () {Page pageList = indexRepository.findByContent ("China", PageRequest.of (0,10)); pageList.getContent () .forEach (e-> {System.out.println ("repositoryQuery = >" + e);}) } @ Test void testTemplateQuery () {BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery () .must (QueryBuilders.simpleQueryStringQuery ("China") .field ("content")) NativeSearchQuery query = new NativeSearchQueryBuilder () .withQuery (queryBuilder) .withPageable (PageRequest.of (0,10)) .build (); SearchHits search = esTemplate.search (query, Index.class) If (search.hasSearchHits ()) {search.getSearchHits () .forEach (e-> {System.out.println ("templateQuery = >" + e.getContent ());}) } init datatemplateQuery = > Index (id=3, content= Chinese and South Korean fishing police conflict investigation: South Korean police detain an average of one Chinese fishing boat per day) templateQuery = > Index (id=4, content= Chinese Consulate in Los Angeles has been shot by an Asian man suspect has surrendered) templateQuery = > Index (id=5, content= Chinese Heavenly Eye will officially open to the world next month will compete with FAST pulsar search) init datarepositoryQuery = > Index (id=3) Content= Chinese and South Korean fishing police conflict investigation: South Korean police detain an average of one Chinese fishing boat per day) repositoryQuery = > Index (id=4, content= Chinese Consulate in Los Angeles has been shot by an Asian man suspect has surrendered) repositoryQuery = > Index (content= Chinese Heavenly Eye officially opened to the world next month will compete with FAST pulsar search)
5. You can start a scheduled task and schedule ping to prevent Connection time out.
@ Scheduled (fixedRate = 15000) public void ping () {esTemplate.execute (client-> client.ping (RequestOptions.DEFAULT));} that's all for "how to integrate elasticsearch in springboot". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.