ElasticSearch入門(mén)篇
一训挡、安裝部署
1.1 版本選擇
http://www.reibang.com/p/c5c3e834c028
1.2 單機(jī)版
-
下載解壓
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.6.1.tar.gz tar -zxvf elasticsearch-6.6.1.tar.gz -C /opt/es/
-
ES不能在ROOT賬戶下啟動(dòng)腥放,必須創(chuàng)建子賬戶
useradd es 創(chuàng)建用戶 passwd es 設(shè)置密碼 chown -R es:root /opt/es/ 給文件指定所屬用戶和用戶組 chmod -R 770 /opt/es/ 修改文件夾的權(quán)限 su es 切換到es賬戶,su是switch user的縮寫(xiě)
-
修改配置vim elasticsearch.yml
####### 若要外界能訪問(wèn)到虛擬機(jī)上的ES,必須要配置network.host ####### network.host: 192.168.114.100 ####### ES綁定端口 ####### http.port: 9200
-
./elasticsearch -d 后臺(tái)啟動(dòng)
./elasticsearch -h 了解啟動(dòng)腳本可用選項(xiàng) ps -ef | grep elastic 查找ES進(jìn)程 kill -9 進(jìn)程號(hào) 殺掉ES進(jìn)程 啟動(dòng)日志在 elasticsearch-6.6.1/logs/elasticsearch.log curl 192.168.114.100:9200 啟動(dòng)成功
- 安裝ES-Head
(1)安裝NodeJs
由于elasticsaerch-head是由node.js編寫(xiě)的,Linux需確保安裝了node6以上版本 .
到 Node.js 官網(wǎng)(https://nodejs.org/en/download/) 選擇要下載的編譯版本Linux Binaries (x64)
1涯塔、切換到安裝路徑
cd /usr/local/src
wget https://nodejs.org/dist/v10.13.0/node-v10.13.0-linux-x64.tar.xz
2找爱、解壓縮文件包
xz -d node-v10.13.0-linux-x64.tar.xz
tar -xvf node-v10.13.0-linux-x64.tar
3、node 環(huán)境配置
vim /etc/profile
在最下面加入
# node
export NODE_HOME=/usr/local/src/node-v10.13.0-linux-x64
export PATH=$PATH:$NODE_HOME/bin
export NODE_PATH=$NODE_HOME/lib/node_modules
source /etc/profile // 使配置文件生效
4掘殴、查看 Node.js 是否安裝成功
node -v
?
(2)安裝elasticsaerch-head
github官網(wǎng) https://github.com/mobz/elasticsearch-head赚瘦,下載mobz開(kāi)頭的elasticsaerch-head
解壓后進(jìn)入elasticsaerch-head-master,
npm install 安裝結(jié)束出現(xiàn)npm ERR!不用管
npm run start 啟動(dòng)在9100
(3) 連接ES-head控制臺(tái)
在elasticsearch.yml中加入下面的配置才能鏈接到控制臺(tái)
http.cors.enabled: true http.cors.allow-origin: "*"
然后重啟嘗試連接
1.3 分布式部署
在另外兩臺(tái)機(jī)子上奏寨,拷貝es包起意、config下elasticsearch.yml與jvm.options,賦予權(quán)限病瞳,解決啟動(dòng)報(bào)錯(cuò)揽咕。
修改配置如下
##################### master配置 #######################
network.host: 192.168.114.100
http.port: 9200
http.cors.enabled: true
http.cors.allow-origin: "*"
cluster.name: gas
node.name: master
node.master: true
discovery.zen.ping.unicast.hosts: ["192.168.114.100","192.168.114.110","192.168.114.120"]
##################### slave1配置 #######################
network.host: 192.168.114.110
http.port: 9200
http.cors.enabled: true
http.cors.allow-origin: "*"
cluster.name: gas
node.name: slave1
discovery.zen.ping.unicast.hosts: ["192.168.114.100","192.168.114.110","192.168.114.120"]
##################### slave2配置 #######################
network.host: 192.168.114.120
http.port: 9200
http.cors.enabled: true
http.cors.allow-origin: "*"
cluster.name: gas
node.name: slave2
discovery.zen.ping.unicast.hosts: ["192.168.114.100","192.168.114.110","192.168.114.120"]
若是偽分布式,則指定不同的http.port套菜,discovery.zen.ping.unicast.hosts只配置一項(xiàng)IP即可亲善。依次用子賬戶啟動(dòng)三臺(tái)ES。
二逗柴、基本API操作
3.1 創(chuàng)建索引
|創(chuàng)建結(jié)構(gòu)化索引
PUT http://192.168.114.100:9200/person
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"person": {
"properties": {
"name": {
"type": "text"
},
"age": {
"type": "integer"
},
"birthday": {
"type": "date",
"format": "yyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
"country": {
"type": "keyword"
}
}
}
}
}
注:
text:會(huì)分詞蛹头,然后進(jìn)行索引
支持模糊、精確查詢
不支持聚合
keyword:不進(jìn)行分詞戏溺,直接索引
支持模糊渣蜗、精確查詢
支持聚合
常用的參數(shù)類型定義&賦值demo
類型 | 參數(shù)定義 | 賦值 |
---|---|---|
text | "name":{"type":"text"} | "name": "zhangsan" |
keyword | "tags":{"type":"keyword"} | "tags": "abc" |
date | "date":{"type": "date"} | "date":"2015-01-01T12:10:30Z" |
long | "age":{"type":"long"} | "age" :28 |
double | "score":{"type":"double"} | "score":98.8 |
boolean | "isgirl": { "type": "boolean" } | "isgirl" :true |
ip | "ip_addr":{"type":"ip"} | "ip_addr": "192.168.1.1" |
geo_point | "location": {"type":"geo_point"} | "location":{"lat":40.12,"lon":-71.34} |
3.2 文檔操作
【插入文檔】
PUT http://192.168.114.100:9200/person/person/1
{
"name" : "張飛",
"age" : 20,
"birthday" : "1998-02-02",
"country" : "大漢"
}
或者 POST http://192.168.114.100:9200/person/person
{
"name" : "劉備",
"age" : 24,
"birthday" : "1994-02-02",
"country" : "大漢"
}
POST不用指定文檔id,會(huì)自行生成唯一字符串
【修改文檔】
可以直接PUT指定對(duì)應(yīng)的文檔ID替換那條數(shù)據(jù)旷祸,
也可以
POST http://192.168.114.100:9200/person/person/1/_update
{
"doc":{
"name":"關(guān)二爺"
}
}
【刪除文檔】
DELETE http://192.168.114.100:9200/person/person/1
刪除索引袍睡,同時(shí)也會(huì)把所有文檔刪除
DELETE http://192.168.114.100:9200/person
3.3 查詢數(shù)據(jù)
【簡(jiǎn)單查詢】
GET http://192.168.114.100:9200/person/person/1
{
"_index": "person",
"_type": "person",
"_id": "1",
"_version": 3, // ---該條文檔修改的次數(shù)
"_seq_no": 2,
"_primary_term": 1,
"found": true,
"_source": {
"name": "關(guān)二爺",
"age": 20,
"birthday": "1998-02-01",
"country": "大漢"
}
}
【條件查詢】
POST http://192.168.114.100:9200/person/person/_search
{
"query":{
"match_all":{}
}
}
{
"took": 10, //--- 查詢花費(fèi)時(shí)間ms
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": { //--- hits命中的數(shù)據(jù)
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "person",
"_type": "person",
"_id": "5Tk8ImkB6d7OxUUbp0wf",
"_score": 1,
"_source": {
"name": "劉備",
"age": 24,
"birthday": "1994-02-02",
"country": "大漢"
}
},
{
"_index": "person",
"_type": "person",
"_id": "1",
"_score": 1,
"_source": {
"name": "關(guān)二爺",
"age": 20,
"birthday": "1998-02-01",
"country": "大漢"
}
}
]
}
}
【排序分頁(yè)】
默認(rèn)排序規(guī)則是按_score降序,若查詢指定排序規(guī)則后肋僧,則返回的數(shù)據(jù)中_score變?yōu)閚ull
POST http://192.168.114.100:9200/person/person/_search
{
"query": {
"match": {
"country": "東吳" // 包含關(guān)鍵詞
}
},
"sort": [{
"birthday": {
"order": "desc" // 按生日降序
}
}],
"from": 0, // 從第一條開(kāi)始
"size": 10 // 取10條
}
聚合查詢會(huì)專列一篇博客深入講解