Elasticsearch 介紹及基礎安裝
阿新 • • 發佈:2020-08-10
目錄
Elasticsearch 介紹及基礎安裝
搜尋引擎式資料庫
一、生活中的資料
1.生活中的資料分類
1)結構化資料
行資料,以二維表的形式展示的資料
2)非結構化資料
沒有具體結構,視訊,文字,音樂檔案
3)半結構化資料
xml表格辦公軟體,HTML
2.搜尋的種類
1)結構化資料搜尋
結構化資料有固定結構,我們會給他建立關係,生成二維表查詢
2)非結構化資料搜尋
1.順序掃描
2.全文搜尋
二、Elasticsearch介紹
1.什麼Elasticsearch?
是一個高度可擴充套件的開源全文搜尋和分析引擎,它可實現資料的實時全文搜尋搜尋、支援分散式可實現高可用、提供API介面,可以處理大規模日誌資料,比如Nginx、Tomcat、系統日誌等功能。 Elasticsearch是基於lucene開發而來的 #Elasticsearch不能完全代替資料庫,而且es不能配置密碼
2.ES和資料庫結構對比
mysql | ES |
---|---|
庫(database) | 索引(index) |
表(table) | 型別(type) |
列(欄位) | 項 |
真實資料行 | 文件(doc) |
3.ES原理
1.儲存資料時進行全文檢索
2.全文檢索後建立倒排索引
4.全文檢索
將一個數據的內容轉化拆分
1.分詞
2.找到關鍵詞
3.搜尋索引
4.匹配,命中,計算命中率
5.根據命中率進行排序
基於java環境,基於Lucene之上包裝一層外殼,Lucene是一個java的搜尋引擎庫,操作非常繁瑣
5.倒排索引
java 是世界上最好的語言 php 是世界上最好的語言 python 是世界上最好的語言
分詞後的值(詞條) | 文件1 | 文件2 | 文件3 |
---|---|---|---|
java | 命中 | ||
是 | 命中 | 命中 | 命中 |
世界上 | 命中 | 命中 | 命中 |
最好的 | 命中 | 命中 | 命中 |
語言 | 命中 | 命中 | 命中 |
php | 命中 | ||
python | 命中 |
6.倒排索引術語
1.詞條:索引最小的儲存單位,拆分一組詞之後,每一個字或者詞
2.詞典:詞條儲存的地方,一般在記憶體中
3.倒排表:記錄多個詞命中的次數和順序
4.倒排檔案:儲存倒排表的地方,一般在磁碟中
倒排索引介紹:https://www.cnblogs.com/ajianbeyourself/p/11280247.html
7.ES功能
1.分散式儲存
2.全文搜尋,結構化檢索,資料分析
全文搜尋:select * from table;
結構化檢索:select * from table where id > 1;
資料分析:select count(*) from table;
8.使用場景
1.大量資料儲存
2.搜尋資料
3.分析資料(ELK)
4.搜尋高亮顯示
# 使用場景
1.搜尋: 電商,百科,app搜尋
2.高亮顯示: github
3.分析和資料探勘: ELK
9.ES特點
1.高效能,天然分散式
2.對運維友好,不需要會java語言,開箱即用
3.功能豐富
三、部署ES
0.安裝包地址參考
# jdk安裝包參考:
https://www.oracle.com/technetwork/java/javase/downloads/index.html
# es安裝包參考
訪問 Elasticsearch 官網 https://www.elastic.co/cn/downloads/elasticsearch 下載安裝包
1.伺服器時間同步
[root@db01 ~]# yum install -y ntpdate
[root@db01 ~]# ntpdate time1.aliyun.com
2.安裝java環境
#上傳
[root@db01 ~]# rz jdk-8u181-linux-x64.rpm
#安裝
[root@db01 ~]# rpm -ivh jdk-8u181-linux-x64.rpm
3.安裝ES
# 1.上傳或下載包
[root@db01 ~]# rz elasticsearch-6.6.0.rpm
下載地址:https://www.elastic.co/downloads/elasticsearch
# 2.安裝
[root@db01 ~]# rpm -ivh elasticsearch-6.6.0.rpm
# 3.根據提示繼續操作
[root@db01 ~]# systemctl daemon-reload
[root@db01 ~]# systemctl enable elasticsearch.service
Created symlink from /etc/systemd/system/multi-user.target.wants/elasticsearch.service to /usr/lib/systemd/system/elasticsearch.service.
[root@db01 ~]# systemctl start elasticsearch.service
# 4.啟動失敗,檢視日誌
[2020-08-10T10:38:56,170][ERROR][o.e.b.Bootstrap ] [node-1] node validation exception
[1] bootstrap checks failed
[1]: memory locking requested for elasticsearch process but memory is not locked
#說明記憶體未鎖定
# 5.配置啟動檔案中記憶體鎖
[root@db01 ~]# vim /usr/lib/systemd/system/elasticsearch.service
[Service]
... ...
LimitMEMLOCK=infinity # 新增此行在service中
# 6.再次啟動後驗證
[root@db01 ~]# netstat -lntp
tcp6 0 0 127.0.0.1:9200 :::* LISTEN 20040/java
tcp6 0 0 127.0.0.1:9300 :::* LISTEN 20040/java
# 8.本地curl訪問
[root@db01 ~]# curl 127.0.0.1:9200
{
"name" : "FIddisT",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "m8Y9neWHRxat7V1tVijMxA",
"version" : {
"number" : "6.6.0",
"build_flavor" : "default",
"build_type" : "rpm",
"build_hash" : "a9861f4",
"build_date" : "2019-01-24T11:27:09.439740Z",
"build_snapshot" : false,
"lucene_version" : "7.6.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
4.ES相關配置檔案
[root@db01 ~]# rpm -qc elasticsearch
/etc/elasticsearch/elasticsearch.yml #ES主配置檔案
/etc/elasticsearch/jvm.options #jvm虛擬記憶體配置
/etc/elasticsearch/log4j2.properties #日誌配置
/etc/elasticsearch/role_mapping.yml #規則配置
/etc/elasticsearch/roles.yml
/etc/elasticsearch/users
/etc/elasticsearch/users_roles
/etc/init.d/elasticsearch #啟動指令碼
/etc/sysconfig/elasticsearch #系統配置
/usr/lib/sysctl.d/elasticsearch.conf #引數配置
/usr/lib/systemd/system/elasticsearch.service #啟動程式
5.配置ES
[root@db01 ~]# vim /etc/elasticsearch/elasticsearch.yml
#叢集名稱
#cluster.name: my-application
#節點名稱
node.name: node-1
#指定資料目錄
path.data: /service/es/data
#指定日誌目錄
path.logs: /service/es/logs
#開啟記憶體鎖
bootstrap.memory_lock: true
#ES監聽地址
network.host: 10.0.0.51 127.0.0.1
#ES埠
http.port: 9200
#叢集的地址
#discovery.zen.ping.unicast.hosts: ["host1", "host2"]
#叢集投票切換
#discovery.zen.minimum_master_nodes:
#總配置(針對單節點環境)
[root@db01 ~]# grep "^[a-z]" /etc/elasticsearch/elasticsearch.yml
node.name: node-1
path.data: /service/es/data
path.logs: /service/es/logs
bootstrap.memory_lock: true
network.host: 10.0.0.51,127.0.0.1
http.port: 9200
6.根據配置檔案建立目錄
#建立資料目錄和日誌目錄
[root@db01 ~]# mkdir /service/es/{data,logs} -p
#授權
[root@db01 ~]# chown -R elasticsearch.elasticsearch /service/es/
7.重啟ES
#重啟ES
[root@db01 ~]# systemctl restart elasticsearch.service
8.驗證
#瀏覽器訪問 http://10.0.0.51:9200/
{
#節點名稱
"name" : "node-1",
#叢集名稱
"cluster_name" : "elasticsearch",
#叢集的uuid
"cluster_uuid" : "KCRhZiS2QWSADsuDwwKC9g",
#版本資訊
"version" : {
"number" : "6.6.0",
"build_flavor" : "default",
"build_type" : "rpm",
"build_hash" : "a9861f4",
"build_date" : "2019-01-24T11:27:09.439740Z",
"build_snapshot" : false,
"lucene_version" : "7.6.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
四、跟ES進行互動
1.curl命令的方式
1)特點
1.使用不方便,容易出錯,命令複雜
2.不需要要安裝任何服務,只需要curl命令
#ES不能完全代替資料庫
1.ES的庫不可修改,表修改屬性困難,容易出錯
2.ES沒有使用者驗證和許可權控制
2)使用方式
#建立索引(庫)
[root@db01 ~]# curl -XPUT '10.0.0.51:9200/student?pretty'
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "student"
}
#新增資料
[root@db01 ~]# curl -XPUT '10.0.0.51:9200/student/user/1?pretty' -H 'Content-Type: application/json' -d '{"name": "lhd","sex":"man","age":"18","about":"good good study","interests":["chinese","english"]}'
{
"_index" : "student",
"_type" : "user",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
#檢視資料
[root@db01 ~]# curl -GET '10.0.0.51:9200/student/user/1?pretty'
{
"_index" : "student",
"_type" : "user",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "lhd",
"sex" : "man",
"age" : "18",
"about" : "good good study",
"interests" : [
"chinese",
"english"
]
}
}
2.使用head外掛的方式
外掛是為了完成不同的功能,官方提供了一些外掛但大部分是收費的,另外也有一些開發愛好者提供的外掛,
可以實現對elasticsearch叢集的狀態監控與管理配置等功能,我們現在要安裝的是Elasticsearch的head外掛,此外掛提供elasticsearch的web介面功能。
安裝Elasticsearch的head外掛時,要安裝npm,npm的全稱是Node Package Manager,是隨同NodeJS一起安裝的包管理和分發工具,
它很方便讓JavaScript開發者下載、安裝、上傳以及管理已經安裝的包。
在Elasticsearch 5.x版本以後不再支援直接安裝head外掛,而是需要通過啟動一個服務方式。
Github地址:https://github.com/mobz/elasticsearch-head
1)特點
1.檢視資料簡單,操作簡單
2.需要安裝nodejs環境,安裝費時
2)安裝外掛方式一:
#安裝npm(只需要在一個節點安裝即可,如果前端還有nginx做反向代理可以每個節點都裝)
[root@elkstack01 ~]# yum install -y npm
#進入下載head外掛程式碼目錄
[root@elkstack01 src]# cd /usr/local/
#從GitHub上克隆程式碼到本地
[root@elkstack01 local]# git clone git://github.com/mobz/elasticsearch-head.git
#克隆完成後,進入elasticsearch外掛目錄
[root@elkstack01 local]# cd elasticsearch-head/
#清除快取
[root@elkstack01 elasticsearch-head]# npm cache clean -f
#使用npm安裝n模組(不同的專案js指令碼所需的node版本可能不同,所以就需要node版本管理工具)
[root@elkstack01 elasticsearch-head]# npm install -g n
#安裝最新版本n模組
[root@elkstack01 elasticsearch-head]# n stable
#生成grunt
[root@elkstack01 elasticsearch-head]# npm install grunt -save
#確認生成grunt檔案
[root@elkstack01 elasticsearch-head]# ll node_modules/grunt
#執行安裝grunt
[root@elkstack01 elasticsearch-head]# npm install
#後臺啟動head外掛(切記,必須在外掛目錄下執行啟動命令)
[root@elkstack01 elasticsearch-head]# npm run start &
#驗證埠是否啟動成功
[root@elkstack01 elasticsearch-head]# netstat -lntup
tcp 0 0 0.0.0.0:9100 0.0.0.0:* LISTEN 11293/grunt
#啟動成功後,修改elasticsearch配置檔案
[root@elkstack01 elasticsearch-head]# vim /etc/elasticsearch/elasticsearch.yml
#新增如下兩行,開啟跨域訪問支援(新增在配置檔案最後即可)
http.cors.enabled: true
http.cors.allow-origin: "*"
#重啟elasticsearch
[root@elkstack01 elasticsearch-head]# /etc/init.d/elasticsearch restart
3)安裝外掛方式二:
1.在電腦上解壓es-head-0.1.4_0.crx.zip,解壓到一個目錄
2.谷歌瀏覽器,右上角,三個點或者三個槓
3.更多工具--擴充套件程式
4.右上角開啟開發者模式
5.載入已解壓的擴充套件程式,選擇解壓問價你的目錄
6.右上角有個放大鏡或者拼圖,點選進去