- 公司開展電商業務,之前有開發過類似項目,并沒有涉及首頁搜尋功能,這次鑒于首頁所搜需求變動,不僅僅是搜尋商品,還要搜尋店鋪,直播及主播,現有實作方式是直接搜尋資料庫,為了增加使用者體驗,減小資料庫的壓力,so改用Elasticsearch實作首頁搜尋功能。
Elasticsearch安裝
- 準備elasticsearch安裝包,版本選擇7.6.0; 官網位址 ;
- 使用root使用者建立es使用者
useradd es
然後為es使用者設定密碼
passwd es
- 修改/etc/security/limits.conf檔案 在檔案末尾添加
* hard nofile 65536
* soft nofile 65536
- 修改/etc/security/limits.d/20-nproc.conf檔案
* soft nproc 4096
* hard nproc 4096
root soft nproc unlimited
在/etc/sysctl.conf檔案末尾添加
vm.max_map_count = 2621441
root使用者執行指令
sudo sysctl -p /etc/sysctl.conf
切換使用者
su es
上傳elasticsearch-7.6.0-linux-x86_64.tar.gz檔案至es使用者目錄下
解壓到目前目錄
tar -zxvf elasticsearch-7.6.0-linux-x86_64.tar.gz
修改/home/es/elasticsearch-7.6.0/config/elasticsearch.yml配置檔案
#這裡寫本機ip,
network.host: 192.168.110.191
#
# Set a custom port for HTTP:
#
#http.port: 9200
#跨域配置 網頁直連使用需要該配置,否則不需要
http.cors.enabled: true
http.cors.allow-origin: "*"
安裝
elasticsearch-analysis-ik中文分詞器;在/home/es/elasticsearch-7.6.0/config/目錄下建立analysis-ik目錄
cd /home/es/elasticsearch-7.6.0/config/
mkdir analysis-ik
将下載下傳下來的中文分詞器檔案解壓,并将config目錄下的所有檔案拷貝到/home/es/elasticsearch-7.6.0/config/analysis-ik目錄下
在/home/es/elasticsearch-7.6.0/plugins目錄下建立analysis-ik目錄
cd /home/es/elasticsearch-7.6.0/plugins
mkdir analysis-ik
将分詞器中除config之外的檔案拷貝到/home/es/elasticsearch-7.6.0/plugins/analysis-ik目錄下
啟動elasticsearch服務
cd /home/es/elasticsearch-7.6.0/bin
./elasticsearch -d
浏覽器通路
http://192.168.110.191:9200/出現如下頁面則服務啟動成功
{
"name" : "eshost",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "muEaR2aHT6uT_F1lop84Lg",
"version" : {
"number" : "7.6.0",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "7f634e9f44834fbc12724506cc1da681b0c3b1e3",
"build_date" : "2020-02-06T00:09:00.449973Z",
"build_snapshot" : false,
"lucene_version" : "8.4.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
資料同步工具
Logstash;注意:一定要保證版本一緻
cd /home/es/
wget https://artifacts.elastic.co/downloads/logstash/logstash-7.6.0.zip
unzip logstash-7.6.0.zip
cd logstash-7.6.0
mkdir myconfig
cd myconfig
在此目錄下需要建立mysql.conf檔案以及要執行的sql檔案,以及資料庫驅動jar包,示例(此示例為多表同步):
-rw-rw-r--. 1 es es 23 Jun 22 14:08 commodity.sql
-rw-rw-r--. 1 es es 24 Jun 23 10:35 instructor.sql
-rw-rw-r--. 1 es es 18 Jun 23 10:36 live.sql
-rw-rw-r--. 1 es es 4658 Jun 23 10:50 mysql.conf
-rw-rw-r--. 1 es es 2018353 May 28 11:25 mysql-connector-java-8.0.9-rc.jar
-rw-rw-r--. 1 es es 24 Jun 23 10:36 user_store.sql
sql檔案中的内容為
select * from commodity
mysql.conf檔案中的内容為
input {
stdin {
}
jdbc {
# mysql資料庫連接配接
jdbc_connection_string => "jdbc:mysql://192.168.110.191:3306/platform?allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false"
# mysqly使用者名和密碼
jdbc_user => "xxx"
jdbc_password => "xxxx"
# 驅動配置
jdbc_driver_library => "/home/es/logstash-7.6.0/myconfig/mysql-connector-java-8.0.9-rc.jar"
# 驅動類名
jdbc_driver_class => "com.mysql.cj.jdbc.Driver"
jdbc_paging_enabled => "true"
jdbc_page_size => "50000"
# 執行指定的sql檔案
statement_filepath => "/home/es/logstash-7.6.0/myconfig/commodity.sql"
# 設定監聽 各字段含義 分 時 天 月 年 ,預設全部為*代表含義:每分鐘都更新
schedule => "* * * * *"
# 索引類型
type => "commodity"
}
jdbc {
# mysql資料庫連接配接
jdbc_connection_string => "jdbc:mysql://192.168.110.191:3306/platform?allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false"
# mysqly使用者名和密碼
jdbc_user => "xxxx"
jdbc_password => "xxxx"
# 驅動配置
jdbc_driver_library => "/home/es/logstash-7.6.0/myconfig/mysql-connector-java-8.0.9-rc.jar"
# 驅動類名
jdbc_driver_class => "com.mysql.cj.jdbc.Driver"
jdbc_paging_enabled => "true"
jdbc_page_size => "50000"
# 執行指定的sql檔案
statement_filepath => "/home/es/logstash-7.6.0/myconfig/instructor.sql"
# 設定監聽 各字段含義 分 時 天 月 年 ,預設全部為*代表含義:每分鐘都更新
schedule => "* * * * *"
# 索引類型
type => "instructor"
}
jdbc {
# mysql資料庫連接配接
jdbc_connection_string => "jdbc:mysql://192.168.110.191:3306/platform?allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false"
# mysqly使用者名和密碼
jdbc_user => "xxxx"
jdbc_password => "xxxx"
# 驅動配置
jdbc_driver_library => "/home/es/logstash-7.6.0/myconfig/mysql-connector-java-8.0.9-rc.jar"
# 驅動類名
jdbc_driver_class => "com.mysql.cj.jdbc.Driver"
jdbc_paging_enabled => "true"
jdbc_page_size => "50000"
# 執行指定的sql檔案
statement_filepath => "/home/es/logstash-7.6.0/myconfig/live.sql"
# 設定監聽 各字段含義 分 時 天 月 年 ,預設全部為*代表含義:每分鐘都更新
schedule => "* * * * *"
# 索引類型
type => "live"
}
jdbc {
# mysql資料庫連接配接
jdbc_connection_string => "jdbc:mysql://192.168.110.191:3306/platform?allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false"
# mysqly使用者名和密碼
jdbc_user => "xxxx"
jdbc_password => "xxxx"
# 驅動配置
jdbc_driver_library => "/home/es/logstash-7.6.0/myconfig/mysql-connector-java-8.0.9-rc.jar"
# 驅動類名
jdbc_driver_class => "com.mysql.cj.jdbc.Driver"
jdbc_paging_enabled => "true"
jdbc_page_size => "50000"
# 執行指定的sql檔案
statement_filepath => "/home/es/logstash-7.6.0/myconfig/user_store.sql"
# 設定監聽 各字段含義 分 時 天 月 年 ,預設全部為*代表含義:每分鐘都更新
schedule => "* * * * *"
# 索引類型
type => "user_store"
}
}
filter {
json {
source => "message"
remove_field => ["message"]
}
}
output {
if [type]=="user_store" {
elasticsearch {
#ESIP位址與端口
hosts => "192.168.110.191:9200"
#ES索引名稱(自己定義的)
index => "user_store"
#自增ID編号
#document_id => "%{id}"
}
}
if [type]=="live" {
elasticsearch {
#ESIP位址與端口
hosts => "192.168.110.191:9200"
#ES索引名稱(自己定義的)
index => "live"
#自增ID編号
#document_id => "%{id}"
}
}
if [type]=="instructor" {
elasticsearch {
#ESIP位址與端口
hosts => "192.168.110.191:9200"
#ES索引名稱(自己定義的)
index => "instructor"
#自增ID編号
#document_id => "%{id}"
}
}
if [type]=="commodity" {
elasticsearch {
#ESIP位址與端口
hosts => "192.168.110.191:9200"
#ES索引名稱(自己定義的)
index => "commodity"
#自增ID編号
#document_id => "%{id}"
}
}
}
啟動Logstash
cd /home/es/logstash-7.6.0/bin
./logstash -f ../myconfig/mysql.conf &
完