您好,登錄后才能下訂單哦!
這篇文章主要講解了“springboot2.1.6集成elasticsearch6.4.3如何實現全文搜索”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“springboot2.1.6集成elasticsearch6.4.3如何實現全文搜索”吧!
由于項目需要elasticsearch做全文搜索,其基本介紹如下:
簡介:ElasticSearch是一個基于Lucene的搜索服務器。它提供了一個分布式多用戶能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java開發的,并作為Apache許可條款下的開放源碼發布,是當前流行的企業級搜索引擎。設計用于云計算中,能夠達到實時搜索,穩定,可靠,快速,安裝使用方便。官方客戶端在Java、.NET(C#)、PHP、Python、Apache Groovy、Ruby和許多其他語言中都是可用的。根據DB-Engines的排名顯示,Elasticsearch是最受歡迎的企業搜索引擎,其次是Apache Solr,也是基于Lucene。
springboot整合elasticsearch常有方式主要有三種:
1.Java API 基于TCP和ES通信,官方已經明確表示在ES 7.0版本中將棄用TransportClient客戶端,且在8.0版本中完全移除它,所以不提倡。 2.REST Client 上面的方式1是基于TCP和ES通信的(而且TransPort將來會被拋棄……),官方也給出了基于HTTP的客戶端REST Client(推薦使用),官方給出來的REST Client有Java Low Level REST Client和Java Hight Level REST Client兩個,前者兼容所有版本的ES,后者是基于前者開發出來的,只暴露了部分API. 3.spring-data-elasticsearch 除了上述方式,Spring也提供了本身基于SpringData實現的一套方案spring-data-elasticsearch
spring-data-elasticsearch集成Es這種方式,封裝了比較常見的es操作,和JPA操作數據庫一樣便捷,只需要繼承 ElasticsearchRepository就可以實現常見的es操作了。
public interface UserESRepository extends ElasticsearchRepository<UserBean, Long> {}
在測試的過程中,鼓搗了兩天多都好無進展,始終報如下錯誤:
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}]
看了很多博客,基本上說是端口和cluster-name 不對應的造成的,可是改了之后還是報錯,后來才知道版本不對應造成的。
依賴文件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' }
配置文件application.yml:
spring: data: elasticsearch: cluster-name: EStest cluster-nodes: 127.0.0.1:9300 server: port: 8080
到官網:https://www.elastic.co/cn/downloads/past-releases/官網
可以下載歷史版本,最新版本為(7.2.0),不建議使用最新的
我使用的版本:
springboot版本 | Elasticsearch版本 |
---|---|
2.1.6 | 6.4.3 |
下載后文件目錄:
進入config文件夾后,修改 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 # # ----------------------------------- 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: 9200 # # 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 # # For more information, consult the gateway module documentation. # # ---------------------------------- Various ----------------------------------- # # Require explicit names when deleting indices: # #action.destructive_requires_name: true http.cors.enabled: true http.cors.allow-origin: "*" node.master: true node.data: true
提示: 這里的cluster.name: EStest 要與application.yml中的cluster-name: EStest保持一致
啟動 elasticsearch 腳本,雙擊即可啟動
在瀏覽器中輸入http://localhost:9200/ ,出現如下就說明啟動成功:
<1>創建實體:
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實體 *@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 + '\'' + '}'; } }
<2> 創建操作數據的Repository
package com.example.demo; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; /* *@Description: 數據倉庫 *@ClassName: BlogRepository *@Author: zzq *@Date: 2019/7/19 17:48 *@Version: 1.0 */ public interface BlogRepository extends ElasticsearchRepository<BlogModel, String> { }
<3>創建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: 2019/7/19 17:49 *@Version: 1.0 */ @RestController @RequestMapping("/blog") @Repository public 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<BlogModel> list = (List<BlogModel>) blogRepository.findAll(); list.forEach(blogModel -> { title += blogModel.toString() + "\n"; }); return title; } }
<4>啟動入口
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ElasticsearchApplication { public static void main(String[] args) { SpringApplication.run(ElasticsearchApplication.class, args); } }
查詢:
d
感謝各位的閱讀,以上就是“springboot2.1.6集成elasticsearch6.4.3如何實現全文搜索”的內容了,經過本文的學習后,相信大家對springboot2.1.6集成elasticsearch6.4.3如何實現全文搜索這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。