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

An example of the integration of ElasticSearch and Spring boot for full-text retrieval

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Full-text retrieval

1. Full-text search concept:

(1) data structure

Structured: only data with fixed format or limited length, such as database, metadata, etc.

Unstructured: data with indefinite length or no fixed format, such as mail, word documents, etc.

(2) unstructured data retrieval:

Sequential scanning method: suitable for files with small amount of data

Full-text search: convert unstructured data into structured data, then create an index and search

(3) concept: full-text search is a document retrieval method that matches all text field search items in a document.

two。 Implementation principle of full-text search

3. Full-text search implementation technology: java-based open source implementation of Lucene,ElasticSearch (with its own distributed management function), Solr

Introduction to 4.ElasticSearch:

Concept:

(1) highly scalable open source full-text search and analysis engine

(2) Fast, near-real multi-big data for storage, search and analysis

(3) to support enterprise applications with complex data search requirements

Features and introduction:

(1) distributed

(2) High availability

(3) for types, multiple data types are supported

(4) Multi-API

(5) document-oriented

(6) different non-writing

(7) near real-time: querying every n seconds, writing to disk

(8) based on Lucene

(9) Apache protocol

Integration of 5.ElasticSearch and Spring Boot

(1) configuration environment: ElasticSearch,Spring Data ElasticSearch,JNA

(2) install ElasticSearch, download the package, decompress and start it directly. Here we especially talk about some abnormal problems of ElasticSearch, which must be corresponding to the version, and the second port problem must be paid attention to.

(3) establish Spring Boot project

(4) We modify the pom.xml file to add related dependencies

(5) before the project code is written, we must install ElasticSearch locally and be compatible with the Spring Boot version. Secondly, pay attention to the port number. When integrating, the port number of the ElasticSearch service is 9200, while the client port number is 9300.

Next we start the locally installed ElasticSearch and then start our project:

4.0.0 com.dhtt.spring.boot.blog spring.data.action 0.0.1-SNAPSHOT jar spring.data.action Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.1.0.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot Spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-data-elasticsearch org.springframework.data spring-data-elasticsearch org.springframework.boot spring-boot-starter-thymeleaf org .springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-devtools org.springframework.boot spring-boot-starter-test test net.java.dev.jna jna 4 . 5.1 org.elasticsearch elasticsearch mysql mysql-connector-java 5.1.46 org.hibernate hibernate-core 5.3.7.Final Org.springframework.boot spring-boot-maven-plugin

Start the project to test, observe whether the configuration of the project is correct, whether the project can be started successfully, and after the project is started successfully.

(5) configure the application.properties file as follows:

# thymeleaf configure spring.thymeleaf.encoding=UTF-8# hot deployment static files without cache. Observe the file modification effect in real time. Spring.thymeleaf.cache=false# uses html5 standard spring.thymeleaf.mode=HTML5spring.thymeleaf.suffix=.htmlspring.resources.chain.strategy.content.enabled=true#elasticsearch server address spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300# connection timeout spring.data.elasticsearch.properties.transport.tcp.connect_timeout=120s # node name Default elasticsearch#spring.data.elasticsearch.cluster-name=elasticsearch # spring.data.elasticsearch.repositories.enable=true#spring.data.elasticsearch.properties.path.logs=./elasticsearch/log#spring.data.elasticsearch.properties.path.data=./elasticsearch/data# database connection configuration spring.datasource.url=jdbc:mysql://localhost:3306/blog_test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=falsespring.datasource.username=rootspring.datasource.password=qitao1996spring.datasource.driver-class-name=com.mysql.jdbc.Driver#jpa configuration Spring.jpa.show-sql=truespring.jpa.hibernate.ddl-auto=create-drop

(6) perform background coding:

Document class EsBlog:

Package com.dhtt.spring.boot.blog.spring.data.action.entity;import java.io.Serializable;import javax.persistence.Id;import org.springframework.data.elasticsearch.annotations.Document;/** * EsBlog entity (document) class * * @ author QiTao * * / @ Document (indexName= "blog", type= "blog") / / specify document public class EsBlog implements Serializable {/ * / private static final long serialVersionUID = 4745983033416635193L; @ Id private String id Private String title; private String summary; private String content; protected EsBlog () {super ();} public EsBlog (String title, String summary, String content) {super (); this.title = title; this.summary = summary; this.content = content;} public String getId () {return id } public void setId (String id) {this.id = id;} public String getTitle () {return title;} public void setTitle (String title) {this.title = title;} public String getSummary () {return summary;} public void setSummary (String summary) {this.summary = summary;} public String getContent () {return content } public void setContent (String content) {this.content = content;} @ Override public String toString () {return "EsBlog [id=" + id + ", title=" + title + ", summary=" + summary + ", content=" + content + "]";}}

Resource library, define data query interface:

Package com.dhtt.spring.boot.blog.spring.data.action.repository;import org.springframework.data.domain.Page;import org.springframework.data.domain.PageRequest;import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;import com.dhtt.spring.boot.blog.spring.data.action.entity.EsBlog / * EsBlogRepository interface * * @ author QiTao * * / public interface EsBlogRepository extends ElasticsearchRepository {/ * pagination, query, remove duplicates * * @ param title * @ param summary * @ param content * @ param pageable * @ return * / Page findDistinctEsBlogByTitleContainingOrSummaryContainingOrContentContaining (String title, String summary, String content, PageRequest pageRequest);}

Finally, write the Controller class:

Package com.dhtt.spring.boot.blog.spring.data.action.web.user;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.domain.Page;import org.springframework.data.domain.PageRequest;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController Import com.dhtt.spring.boot.blog.spring.data.action.entity.EsBlog;import com.dhtt.spring.boot.blog.spring.data.action.repository.EsBlogRepository;@RestController@RequestMapping ("/ blogs") public class BlogController {@ Autowired private EsBlogRepository esBlogRepository @ GetMapping public List list (@ RequestParam (value = "title") String title, @ RequestParam (value = "summary") String summary, @ RequestParam (value = "content") String content, @ RequestParam (value = "pageIndex", defaultValue = "0") int pageIndex, @ RequestParam (value = "pageSize", defaultValue = "10") int pageSize) {/ / add test data esBlogRepository.deleteAll () EsBlogRepository.save ("climbing the Yellow Crane Tower", "Wang Zhiyi's Yellow Crane Tower", "hundreds of days follow the mountain, the Yellow River flows surging eastward toward the sea, if you want to see the scenery of thousands of miles, please climb another tall building"); esBlogRepository.save ("Acacia", "Acacia of Wang Wei", "glittering red beans are produced in Lingnan, spring should grow with luxuriant leaves, may you pick more, this game can best forgive your feelings"). EsBlogRepository.save (new EsBlog ("quiet Night thoughts", "Li Bai's quiet Night thoughts", "there is a bright moonlight in front of the bed, like a cold frost on the ground, looking up at the bright moon in the sky, looking down for your hometown"); / / query to get PageRequest pageRequest=PageRequest.of (pageIndex,pageSize); Page page= esBlogRepository.findDistinctEsBlogByTitleContainingOrSummaryContainingOrContentContaining (title, summary, content, pageRequest); return page.getContent ();}}

Start the project and access it at the front desk:

The foreground result was printed successfully, so our Elasticsearch+Spring Boot integration was successful.

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

Internet Technology

Wechat

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

12
Report