1. 程式人生 > >12章 搜索框架ElasticSearch介紹和整合SpringBoot 4節課

12章 搜索框架ElasticSearch介紹和整合SpringBoot 4節課

article run ads 官方 地址 osi ron ren handle

1、搜索引擎知識和搜索框架elasticsearch基本介紹
簡介:通過京東電商 介紹什麽是搜索引擎,和開源搜索框架ElasticSearch6.x新特性介紹

前言:介紹ES的主要特點和使用場景,新特性講解
mysql:like 模糊,性能問題,

solr:針對企業,Lucene
elasticsearch:針對數據量特別大,PB,TB
純java開發,springboot使用,5.6版本
es升級4->5版本,改動大,但是5版本後,改動不大


elasticSearch主要特點

1、特點:全文檢索,結構化檢索,數據統計、分析,接近實時處理,分布式搜索(可部署數百臺服務器),處理PB級別的數據
搜索糾錯,自動完成
2、使用場景:日誌搜索,數據聚合,數據監控,報表統計分析

3、國內外使用者:維基百科,Stack Overflow,GitHub

新特性講解

1、6.2.x版本基於Lucene 7.x,更快,性能進一步提升,對應的序列化組件,升級到Jackson 2.8
mysql:database table rocord
es : index type(只能存在一個) document

2、推薦使用5.0版本推出的Java REST/HTTP客戶端,依賴少,比Transport使用更方便,在基準測試中,性能並不輸於Transport客戶端,

在5.0到6.0版本中,每次有對應的API更新, 文檔中也說明,推薦使用這種方式進行開發使用,所有可用節點間的負載均衡
在節點故障和特定響應代碼的情況下進行故障轉移,失敗的連接處罰(失敗的節點是否重試取決於失敗的連續次數;失敗的失敗次數越多,客戶端在再次嘗試同一節點之前等待的時間越長)

3、(重要)不再支持一個索引庫裏面多個type,6.x版本已經禁止一個index裏面多個type,所以一個index索引庫只能存在1個type

官方文檔:
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

2、快熟部署ElastcSearch5.6.x
簡介:講解為什麽不用ES6.x版本,及本地快速安裝ElasticSeach和場景問題處理

配置JDK1.8
使用wget 下載elasticsearch安裝包
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.8.tar.gz
解壓
tar -zxvf elasticsearch-5.6.8.tar.gz
官網:https://www.elastic.co/products/elasticsearch

外網訪問配置:
config目錄下面elasticsearch.yml
修改為 network.host: 0.0.0.0


配置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、問題二
[root@iZwz95j86y235aroi85ht0Z 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

  Windows環境使用教程:

  將zip文件解壓進入 elasticsearch-xx\bin 目錄,雙擊執行 elasticsearch.bat,該腳本文件執行 ElasticSearch 安裝程序,稍等片刻,打開瀏覽器,輸入 http://localhost:9200 ,顯式以下畫面,說明ES安裝成功。

  技術分享圖片


3、ElasticSearch5.6.8測試數據準備
簡介: ElasticSearch5.6.x簡單測試
1、步驟 https://www.elastic.co/guide/en/elasticsearch/reference/5.6/index.html
2、使用POSTMAN 工具

基礎
查看集群狀態:localhost:9200/_cat/health?v
查看索引列表:localhost:9200/_cat/indices?v

4、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/

版本說明:SpringBoot整合elasticsearch
https://github.com/spring-projects/spring-data-elasticsearch/wiki/Spring-Data-Elasticsearch---Spring-Boot---version-matrix

1、添加maven依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

2、接口繼承ElasticSearchRepository,裏面有很多默認實現
註意點:
索引名稱記得小寫,類屬性名稱也要小寫
新建實體對象article
加上類註解 @Document(indexName = "blog", type = "article")


3、配置文件:
# ELASTICSEARCH (ElasticsearchProperties)
spring.data.elasticsearch.cluster-name=elasticsearch # Elasticsearch cluster name.
spring.data.elasticsearch.cluster-nodes=localhost:9300 # Comma-separated list of cluster node addresses.
spring.data.elasticsearch.repositories.enabled=true # Whether to enable Elasticsearch repositories.

4、QueryBuilder使用
https://www.elastic.co/guide/en/elasticsearch/client/java-api/1.3/query-dsl-queries.html

//單個匹配,搜索name為jack的文檔
QueryBuilder queryBuilder = QueryBuilders.matchQuery("title", "搜");

4、查看es數據

查看索引信息:http://localhost:9200/_cat/indices?v
查看某個索引庫結構:http://localhost:9200/blog
查看某個對象:http://localhost:9200/blog/article/1

12章 搜索框架ElasticSearch介紹和整合SpringBoot 4節課