springboot2.x簡單詳細教程--搜尋框架ElasticSearch介紹和整合(第十二章)
阿新 • • 發佈:2018-11-24
一、搜尋引擎知識和搜尋框架elasticsearch(es)基本介紹
1. 簡介:通過京東電商 介紹什麼是搜尋引擎,和開源搜尋框架ElasticSearch6.x新特性介紹
前言:介紹ES的主要特點和使用場景,新特性講解
mysql:like 模糊,效能問題,(資料量大起來查詢太慢)
Lucene是apache搜尋框架
solr:針對企業,資料量G級別
elasticsearch:針對資料量特別大,PB,TB
elasticsearch 使用場景:1.GitHub搜尋 2.維基百科 3.Stack Overflow 搜尋
注意: solr 和elasticsearch底層都是基於Lucene
elasticsearch是 純java開發,springboot使用,5.6版本的elasticsearch即可
因為 es升級4->5版本,改動大, 但是5版本後,改動不大
優點:不用擔心效能問題
2. elasticSearch主要特點
1)、特點:全文檢索,結構化檢索,資料統計、分析,接近實時處理,分散式搜尋(可部署數百臺伺服器),處理PB級別的資料
搜尋糾錯,自動完成
2)、使用場景:日誌搜尋,資料聚合,資料監控,報表統計分析
3)、國內外使用者:維基百科,Stack Overflow,GitHub
新特性講解
1、6.2.x版本基於Lucene 7.x,更快,效能進一步提升,對應的序列化元件,升級到Jackson 2.8
2、推薦使用5.0版本推出的Java REST/HTTP客戶端,依賴少,比Transport使用更方便,在基準測試中,效能並不輸於Transport客戶端,
在5.0到6.0版本中,每次有對應的API更新, 文件中也說明,推薦使用這種方式進行開發使用,所有可用節點間的負載均衡
在節點故障和特定響應程式碼的情況下進行故障轉移,失敗的連線處罰(失敗的節點是否重試取決於失敗的連續次數;失敗的失敗次數越多,客戶端在再次嘗試同一節點之前等待的時間越長)
3、(重要)不再支援一個索引庫裡面多個type,6.x版本已經禁止一個index裡面多個type,所以一個index索引庫只能存在1個type
怎麼理解index,type?
資料庫: mysql: database(庫) table(表) rocord(欄位):一個庫對應多個表,每個表有多個欄位
elasticSearch: index (相當於庫) type(相當於表只能存在一個) document
官方文件:
1、6.0更新特性
https://www.elastic.co/guide/en/elasticsearch/reference/6.0/release-notes-6.0.0.html#breaking-java-6.0.0
2、6.1更新特性
https://www.elastic.co/guide/en/elasticsearch/reference/6.1/release-notes-6.1.0.html
二、快熟部署ElastcSearch5.6.x
1.簡介:講解為什麼不用ES6.x版本,及本地快速安裝ElasticSeach和場景問題處理
配置JDK1.8
2. linux伺服器去部署
使用wget 下載elasticsearch安裝包
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.8.tar.gz
解壓
tar -zxvf elasticsearch-5.6.8.tar.gz
3.下載elasticsearch
官網:https://www.elastic.co/products/elasticsearch
1)
2)
3)
4)我這裡選擇5.6.8
安裝啟動在linux系統:官網
4.外網訪問配置:
config目錄下面elasticsearch.yml
修改為 network.host: 0.0.0.0
5.配置es出現相關問題處理(阿里雲、騰訊雲,亞馬遜雲安裝問題集合):
1、問題一
Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000c5330000, 986513408, 0) failed; error='Cannot allocate memory' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 986513408 bytes for committing reserved memory.
# An error report file with more information is saved as:
# /usr/local/software/temp/elasticsearch-6.2.2/hs_err_pid1912.log
解決:記憶體不夠,購買阿里雲的機器可以動態增加記憶體
2、問題二
[[email protected] bin]# ./elasticsearch
[2018-02-22T20:14:04,870][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [] uncaught exception in thread [main]
org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:125) ~[elasticsearch-6.2.2.jar:6.2.2]
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:112) ~[elasticsearch-6.2.2.jar:6.2.2]
at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-6.2.2.jar:6.2.2]
at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:124) ~[elasticsearch-cli-6.2.2.jar:6.2.2]
解決:用非root使用者
新增使用者:useradd -m 使用者名稱 然後設定密碼 passwd 使用者名稱
3、問題三
./elasticsearch
Exception in thread "main" java.nio.file.AccessDeniedException: /usr/local/software/temp/elasticsearch-6.2.2/config/jvm.options
解決:許可權不夠 chmod 777 -R 當前es目錄
常見配置問題資料:https://www.jianshu.com/p/c5d6ec0f35e0
三、ElasticSearch5.6.8測試資料準備
簡介: ElasticSearch5.6.x簡單測試
1、步驟 https://www.elastic.co/guide/en/elasticsearch/reference/5.6/index.html
2、使用POSTMAN 工具
啟動ElasticSearch5.6 在bin下
命令: ./elasticsearch
檢視叢集狀態:localhost:9200/_cat/health?v
檢視索引列表:localhost:9200/_cat/indices?v
注意目前沒有建立索引
1)建立索引
再訪問 索引customer有了
2)索引新增type
四、SpringBoot2.x整合elasticsearch5.6.x
簡介:SpringBoot2.x整合elasticSearch5.6.8實戰
Spring Data Elasticsearch文件地址
https://docs.spring.io/spring-data/elasticsearch/docs/3.0.6.RELEASE/reference/html/
Spring Data是整合框架的中介軟體
版本說明:SpringBoot整合elasticsearch
https://github.com/spring-projects/spring-data-elasticsearch/wiki/Spring-Data-Elasticsearch---Spring-Boot---version-matrix
springboot版本最好和ES版本對應,否則可能容易出現相容問題
1、新增maven依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
2、介面繼承ElasticSearchRepository,裡面有很多預設實現
注意點:
索引名稱記得小寫,類屬性名稱也要小寫
新建實體物件article
加上類註解 @Document(indexName = "blog", type = "article")
1)建立一個實體類,將資料存到elasticsearch伺服器
2)介面繼承ElasticSearchRepository
3)Repository原始碼
4)@Component 原始碼
5)ElasticsearchRepository<Article, Long>原始碼
6)實現介面後配置
7)寫controller
檢視es資料
8) 檢視索引資訊:http://localhost:9200/_cat/indices?v
啟動es服務,啟動工程,先訪問路徑http://localhost:8080/api/v1/article就是儲存資料到es再用postman檢視索引
注意:@Document(indexName = "blog", type = "article")
沒有indexName 和type儲存執行後自動建立
9) 檢視某個索引庫結構:http://localhost:9200/blog(9200是es伺服器IP)
10) 檢視某個物件:http://localhost:9200/blog/article/2
3、QueryBuilder使用(es裡面的搜尋API)
https://www.elastic.co/guide/en/elasticsearch/client/java-api/1.3/query-dsl-queries.html
//單個匹配,搜尋name為jack的文件
QueryBuilder queryBuilder = QueryBuilders.matchQuery("title", "搜");
比如title是:springboot整合elasticsearch,這個是新版本 2018
啟動工程後,也可以用postman訪問
注意
補充:多個匹配查詢
https://www.elastic.co/guide/en/elasticsearch/client/java-api/1.3/query-dsl-queries.html