一、安裝elasticsearch
1,下載下傳elasticsearch
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.1.tar.gz
2,下載下傳完成後解壓,并且移動到 /usr/local 目錄下
tar -zxvf elasticsearch-6.2.1.tar.gz
mv elasticsearch-6.2.1 /usr/local/
cd /usr/local/elasticsearch-6.2.1/
注意,elasticsearch是不能使用root使用者啟動的,是以需要建立新的使用者群組,使用指定使用者啟動
3,建立使用者群組
# 以root使用者來 groupadd 添加一個使用者組
groupadd elsearch
# 添加一個使用者,-g是在使用者組下 -p是密碼
useradd elsearch -g elsearch -p elsearch
# 修改elasticsearch-6.2.1 目錄的所屬組合使用者為 elsearch
chown -R elsearch:elsearch /usr/local/elasticsearch-6.2.1/
4,修改 elasticsearch.yml 配置檔案
vim /usr/local/elasticsearch-6.2.1/config/elasticsearch.yml
找到 #network.host: 192.168.0.1 行,将注釋打開,改成自己需要通路的機器ip,或者改成0.0.0.0允許所有ip通路
修改前
修改後
并且在檔案最後面直接配置,允許跨域配置,安裝可視化插件會用到
http.cors.enabled: true
http.cors.allow-origin: "*"
5,使用elsearch身份啟動es
runuser -u elsearch /usr/local/elasticsearch-6.2.1/bin/elasticsearch &
備注:在後面添加 & 符号表示背景啟動
ElasticSearch啟動錯誤處理方法:
[2019-09-27T14:43:43,591][INFO ][o.e.b.BootstrapChecks ] [Gt7IdM2] bound or publishing to a non-loopback address, enforcing bootstrap checks
ERROR: [2] bootstrap checks failed
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
[2]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
1,修改 /etc/security/limits.conf 在最後面追加參數
vim /etc/security/limits.conf
最後追加配置
* soft nofile 65536
* hard nofile 65536
* soft nproc 4096
* hard nproc 4096
2,修改 /etc/sysctl.conf 檔案 在最後追加參數
vim /etc/sysctl.conf
最後追加配置
vm.max_map_count=655360
儲存後執行指令
sysctl -p
啟動完成後在浏覽器輸入 ip:9200 看到輸出參數就表示es安裝成功了
備注:如果使用的華為雲伺服器修改後可能還是報錯,這是華為雲伺服器造成的坑,具體檢視原文文檔連接配接位址解決:
[1]: max file descriptors [65535] for elasticsearch process is too low, increase to at least [65536]
華為雲伺服器修改配置後還是出現以上問題參照下面連結解決:
https://www.huaweicloud.com/kunpeng/software/es.html
二、安裝可視化界面插件elasticsearch-head-master
1,安裝elasticsearch-head-master 依賴的node環境
下載下傳
wget https://npm.taobao.org/mirrors/node/latest-v4.x/node-v4.4.7-linux-x64.tar.gz
解壓,并移動到 /usr/local 目錄下
tar -zxvf node-v4.4.7-linux-x64.tar.gz
mv node-v4.4.7-linux-x64 /usr/local/
配置node環境變量
vim /etc/profile
在最後面追加配置
NODE_HOME=/usr/local/node-v4.4.7-linux-x64
NODE_PATH=$NODE_HOME/lib/node_modules
PATH=$NODE_HOME/bin:$NODE_PATH:$PATH
儲存後執行指令
source /etc/profile
2,下載下傳elasticsearch-head-master安裝包
wget https://github.com/mobz/elasticsearch-head/archive/master.zip
解壓并移動到 /usr/local目錄下
unzip master.zip
mv elasticsearch-head-master/ /usr/local/
備注:下載下傳較慢可以使用指令将鏡像設定成阿裡鏡像
npm config set registry http://registry.npm.taobao.org
進入到 elasticsearch-head-master 目錄,因為elasticsearch-head-master是通過grunt啟動的,是以需要安裝grunt插件
cd /usr/local/elasticsearch-head-master/
npm install -g grunt-cli
npm install
3,啟動elasticsearch-head-master
需要在head的目錄下運作
cd /usr/local/elasticsearch-head-master/
grunt server &
啟動成功後在浏覽器輸入 ip:9100就可以通路到頁面了
es各個搜尋條件含義:
match 搜尋key會被分詞,搜尋的filed中的值是分了詞,然後再比對
term 精确比對輸入的參數(不使用分析器)
wildcard 沒用過
prefix 字首比對
fuzzy 模糊比對
range 範圍比對 比對數值類型,比如說對于使用者樣本搜尋,年齡範圍在20~30之間的,就用range。
query_string 查詢text類型的字段
text 文本類型,文本類型可以設定分詞器屬性
missing 是否存在該字段值 和exists反義
三、使用 elasticsearch-head 界面工具建立一個索引庫與文檔
1,建立索引庫
索引欄點選建立索引,輸入索引名稱,點選OK,分片數和副本數預設就可以了
建立完成後就可以在清單看到剛剛建立的索引庫了
2, 再複合查詢裡面建立索引庫的文檔資訊
question_dev/doc/_mapping
question_dev:表示索引庫名稱
doc:文檔名稱
_mapping:關鍵字,表示索引庫文檔映射操作操作
完成後送出,再傳回資料浏覽,就能夠看到剛剛建立的索引庫文檔字段了
文檔建立資料:
{
"properties": {
"question_id": {
"type": "integer"
},
"title": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"answer": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"type_ids": {
"type": "text"
},
"create_time": {
"format": "yyyy-MM-dd HH:mm:ss",
"type": "date"
}
}
}
3,向文檔裡面添加資料
添加完成後再次傳回資料浏覽就能夠檢視到剛剛添加的資料了
資料添加json
{
"question_id": 1,
"title": "String 是最基本的資料類型嗎?",
"answer": "<p>不是。Java中的基本資料類型隻有8個:byte、short、int、long、float、double、char、boolean;</p><p>除了基本類型(primitive type)和枚舉類型(enumeration type),剩下的都是引用類型(reference type)。</p>",
"type_ids": "1",
"create_time": "2019-03-25 16:20:50"
}
4,搜尋文檔,按照标題搜尋
question_dev/doc/_search?q=title:今天你吃了嗎
question_dev/doc/_search?q=title:資料類型