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 realize full-text search by integrating springboot2.1.6 with elasticsearch6.4.3

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "springboot2.1.6 integrated elasticsearch6.4.3 how to achieve full-text search", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "springboot2.1.6 integrated elasticsearch6.4.3 how to achieve full-text search" bar!

Since the project requires elasticsearch to do full-text search, the basic introduction is as follows:

Summary: ElasticSearch is a Lucene-based search server. It provides a full-text search engine with distributed multi-user capability, based on RESTful web interface. Elasticsearch is developed in Java and released as open source under the Apache license terms, and is currently a popular enterprise search engine. Designed for cloud computing, can achieve real-time search, stable, reliable, fast, easy to install and use. Official clients are available in Java, .NET (C #), PHP, Python, Apache Groovy, Ruby, and many other languages. According to DB-Engines 's ranking, Elasticsearch is the most popular enterprise search engine, followed by Apache Solr and based on Lucene.

There are three main ways for springboot to integrate elasticsearch:

1.Java API is based on TCP and ES communication, and officials have made it clear that the TransportClient client will be deprecated in ES version 7. 0 and removed completely in version 8. 0, so it is not recommended. Mode 1 above in 2.REST Client is based on communication between TCP and ES (and TransPort will be abandoned in the future.) Officially, the client REST Client based on HTTP is also given (recommended). There are two official REST Client: Java Low Level REST Client and Java Hight Level REST Client. The former is compatible with all versions of ES, while the latter is developed based on the former, which only exposes part of API.3.spring-data-elasticsearch. In addition to the above methods, Spring also provides a set of solutions based on SpringData, spring-data-elasticsearch.

Spring-data-elasticsearch integrates Es, which encapsulates the common es operations, which is as convenient as JPA to operate the database. You only need to inherit ElasticsearchRepository to achieve common es operations.

Public interface UserESRepository extends ElasticsearchRepository {}

In the course of the test, there was no progress for more than two days, and the following errors were always reported:

Caused by: org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: [{# transport#-1} {kgdgqCDKRlm9rjdj2B_s8A} {47.89.250.94} {47.89.250.94:9300}]

After reading a lot of blogs, it is basically said that the port and cluster-name do not correspond to each other, but I still report an error after the change, and later I know that the version does not correspond to each other.

1. Create a springboot project using IDEA

Dependency file build.gradle:

Plugins {id 'org.springframework.boot' version' 2.1.6.RELEASE'id 'java'} apply plugin:' io.spring.dependency-management'group = 'com.example'version =' 0.0.1-SNAPSHOT'sourceCompatibility = '1.8'repositories {mavenCentral ()} dependencies {implementation' org.springframework.boot:spring-boot-starter-data-elasticsearch' implementation 'org.springframework.boot:spring-boot -starter-web' testImplementation 'org.springframework.boot:spring-boot-starter-test'}

Profile application.yml:

Spring: data: elasticsearch: cluster-name: EStest cluster-nodes: 127.0.0.1:9300server: port: 80802. Download elasticsearch

To the official website: https://www.elastic.co/cn/downloads/past-releases/ official website

You can download the historical version, and the latest version is (7.2.0). The latest version is not recommended.

The version I use:

Springboot version Elasticsearch version 2.1.66.4.3

File directory after download:

After entering the config folder, modify elasticsearch.yml:

# = = Elasticsearch Configuration = # # NOTE: Elasticsearch comes with reasonable defaults for most settings.# Before you set out to tweak and tune the configuration, make sure you# understand what are you trying to accomplish and the consequences.## The primary way of configuring a node is via this file. This template lists# the most important settings you may want to configure for a production cluster.## Please consult the documentation for further information on configuration options:# https://www.elastic.co/guide/en/elasticsearch/reference/index.html##-Cluster-- -# # Use a descriptive name for your cluster:#cluster.name: EStest##-Node-- # # Use a descriptive name for the node:#node.name: node-1## Add custom Attributes to the node:##node.attr.rack: R1 path.data #-Paths-# # Path to directory where to store the data (separate multiple locations by comma): # # path.data: / path/to/data # # Path to log files:##path.logs: / path/to/logs##-- Memory-- # # Lock the memory on startup:##bootstrap.memory_lock: true## Make sure that the heap size is Set to about half the memory available# on the system and that the owner of the process is allowed to use this# limit.## Elasticsearch performs poorly when the system is swapping the memory.##-Network-# # Set the bind address to a Specific IP (IPv4 or IPv6): # network.host: 0.0.0.0 Set a custom port for HTTP:#http.port # Set a custom port for HTTP:#http.port: 9200 million # For more information Consult the network module documentation.##-- Discovery-- # # Pass an initial list of hosts to perform discovery when new node is started:# The default list of hosts is ["127.0.0.1" "[: 1]"] # # discovery.zen.ping.unicast.hosts: ["host1", "host2"] # # Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1): # # discovery.zen.minimum_master_nodes:## For more information Consult the zen discovery module documentation.##-- Gateway-- # # Block initial recovery after a full cluster restart until N nodes are started:##gateway.recover_after_nodes: 3 years # For more information Consult the gateway module documentation.##-Various-# # Require explicit names when deleting indices:##action.destructive_requires_name: truehttp.cors.enabled: truehttp.cors.allow-origin: "*" node.master: Truenode.data: true hint: the cluster.name: EStest here should be consistent with the cluster-name: EStest in application.yml

Start the elasticsearch script, double-click to start it

Enter http://localhost:9200/ in the browser, and the following indicates that the startup is successful:

3. Add data to es and implement search

Create an entity:

Package com.example.demo;import com.fasterxml.jackson.annotation.JsonFormat;import lombok.Data;import lombok.experimental.Accessors;import org.springframework.data.annotation.Id;import org.springframework.data.elasticsearch.annotations.Document;import org.springframework.data.elasticsearch.annotations.Field;import org.springframework.data.elasticsearch.annotations.FieldType;import java.io.Serializable / * * @ Description: Blog entity * @ ClassName: BlogModel * @ Author: zzq * @ Date: 2019-7-19 17:47 * @ Version: 1.0 * / @ Data@Accessors (chain = true) @ Document (indexName = "blog", type = "user") public class BlogModel implements Serializable {private static final long serialVersionUID = 1L; @ Id private Long id; @ Field (type = FieldType.Text, analyzer = "ik_max_word") private String title / / @ Field (type = FieldType.Date, format = DateFormat.basic_date) public BlogModel () {} public BlogModel (Long id, String title) {this.id = id; this.title = title;} public Long getId () {return id;} public void setId (Long id) {this.id = id;} public String getTitle () {return title } public void setTitle (String title) {this.title = title;} @ Override public String toString () {return "BlogModel {" + "id='" + id +'\'+ ", title='" + title +'\'+'}';}}

Create a Repository for manipulating data

Package com.example.demo;import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;/* * @ Description: data Warehouse * @ ClassName: BlogRepository * @ Author: zzq * @ Date: 17:48 * @ Version: 2019-7-19 / public interface BlogRepository extends ElasticsearchRepository {}

Create controller

Package com.example.demo;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Repository;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List / * * @ Description: controller * @ ClassName: BlogController * @ Author: zzq * @ Date: 17:49 * @ Version on 2019-7-19 * / @ RestController@RequestMapping ("/ blog") @ Repositorypublic class BlogController {@ Autowired private BlogRepository blogRepository; @ GetMapping ("/ save") public String add () {BlogModel blogModel = new BlogModel (); blogModel.setTitle ("superheros"); blogRepository.save (blogModel); return "ok" } private String title = "; @ GetMapping (" / get ") public String get () {Iterable list = (List) blogRepository.findAll (); list.forEach (blogModel-> {title + = blogModel.toString () +"\ n ";}); return title;}}

Start the entrance

Package com.example.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class ElasticsearchApplication {public static void main (String [] args) {SpringApplication.run (ElasticsearchApplication.class, args);}} 4. Result

Query:

D

Thank you for your reading, the above is the content of "springboot2.1.6 integrated elasticsearch6.4.3 how to achieve full-text search". After the study of this article, I believe you have a deeper understanding of how springboot2.1.6 integrated elasticsearch6.4.3 realizes full-text search, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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