hosts解析
vim /etc/hosts
10.0.0.10 es1
10.0.0.11 es2
10.0.0.12 es3
部署jdk
mkdir /usr/java
tar xf jdk-8u221-linux-x64.tar.gz -C /usr/java/
ln -s /usr/java/jdk1.8.0_221 /usr/java/jdk
vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/java/jdk
export PATH=$JAVA_HOME/bin:$JAVA_HOME/bin:$PATH
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar
部署elasticsearch
安裝
tar xf elasticsearch-6.8.3.tar.gz -C /opt/
ln -s /opt/elasticsearch-6.8.3 /opt/elasticsearch
創建用戶並授權
useradd -s /bin/bash -M es
chown -R es.es /opt/elasticsearch-6.8.3/
配置文件
vim config/elasticsearch.yml
es1
# 修改
node.name: es1
network.host: 10.0.0.10
es2
node.name: es2
network.host: 10.0.0.11
附錄:
# 修改以下內容
cluster.name: es.od.com
node.name: hdss7-12.host.com
path.data: /data/elasticsearch/data
path.logs: /data/elasticsearch/logs
bootstrap.memory_lock: true
network.host: 10.0.0.10
http.port: 9200
jvm內存設置
vi config/jvm.options
修改以下
-Xms512m
-Xmx512m
文件描述符
cat /etc/security/limits.d/es.conf
es hard nofile 65536
es soft fsize unlimited
es hard memlock unlimited
es soft memlock unlimited
調整內核參數
sysctl -w vm.max_map_count=262144
echo "vm.max_map_count=262144" >> /etc/sysctl.conf
sysctl -p
啓動
su -c "/opt/elasticsearch/bin/elasticsearch -p /tmp/elasticsearch-pid -d" es
集羣配置
es1
egrep -v “#|^$” config/elasticsearch.yml
cluster.name: elasticsearch
node.name: es1
network.host: 10.0.0.10
http.port: 9200
node.master: true
node.data: true
node.ingest: false
network.bind_host: 0.0.0.0
network.publish_host: 10.0.0.10
bootstrap.memory_lock: true
transport.tcp.port: 9300
transport.tcp.compress: true
discovery.zen.ping.unicast.hosts: ["10.0.0.10","10.0.0.11","10.0.0.12"]
discovery.zen.minimum_master_nodes: 1
es2
egrep -v “#|^$” config/elasticsearch.yml
cluster.name: elasticsearch
node.name: es2
network.host: 10.0.0.11
http.port: 9200
node.ingest: false
node.master: false
node.data: true
transport.tcp.port: 9300
network.bind_host: 0.0.0.0
network.publish_host: 10.0.0.11
bootstrap.memory_lock: true
transport.tcp.compress: true
discovery.zen.ping.unicast.hosts: ["10.0.0.10","10.0.0.11","10.0.0.12"]
discovery.zen.minimum_master_nodes: 1
es3
egrep -v “#|^$” config/elasticsearch.yml
cluster.name: elasticsearch
node.name: es3
network.host: 10.0.0.12
http.port: 9200
node.ingest: false
node.master: false
node.data: true
transport.tcp.port: 9300
network.bind_host: 0.0.0.0
network.publish_host: 10.0.0.12
bootstrap.memory_lock: true
transport.tcp.compress: true
discovery.zen.ping.unicast.hosts: ["10.0.0.10","10.0.0.11","10.0.0.12"]
discovery.zen.minimum_master_nodes: 1
檢查集羣狀態
curl -X GET http://localhost:9200/_cat/nodes
10.0.0.12 21 97 0 0.00 0.01 0.05 d - es3
10.0.0.11 21 97 0 0.06 0.04 0.05 d - es2
10.0.0.10 19 88 0 0.00 0.03 0.05 md * es1
倒排索引
- 倒排索引(inverted index),也常被稱爲反向索引、植入檔案或反向檔案,是一種索引方法,被用來存儲在全文搜索下某個單詞在一個文檔或一組文檔中的存儲位置的映射。它是文檔檢索系統中最常用的數據結構
常規的索引建立方式
文檔 —> 關鍵詞的映射過程(正向索引)
倒排反向建立索引
關鍵詞 —> 文檔的映射 把正向索引的結果重新構造成倒排索引
單詞集合
文檔集合
根據單詞集合和文檔集合,得到一張表
插件安裝
bin/elasticsearch-plugin install analysis-icu
Elasticsearch API CURD操作
創建索引
創建索引library
curl -H "Content-Type: application/json" -X PUT 'http://10.0.0.10:9200/library/' -d '{
"settings":{
"index":{
"number_of_shards":3,
"number_of_replicas":1
}
}
}'
查看索引
curl -X GET 'http://10.0.0.10:9200/library/_settings'
curl -X GET 'http://10.0.0.10:9200/library,library2/_settings'
curl -X GET 'http://10.0.0.10:9200/_all/_settings'
創建文檔
/索引/Type/文檔ID
curl -H "Content-Type: application/json" -X PUT 'http://10.0.0.10:9200/library/books/1' -d '{
"title": "Elasticsearch: The Definitive Guide",
"name":{
"first":"Zachary",
"last":"Tong"
},
"publish_date":"2020-02-06",
"price":"49.99"
}'
curl -H "Content-Type: application/json" -X PUT 'http://10.0.0.10:9200/library/books/2' -d '{
"title": "Elasticsearch: The Definitive Guide007",
"name":{
"first":"wu",
"last":"xing"
},
"publish_date":"2020-02-06",
"price":"60.99"
}'
創建文檔不指定ID
curl -H "Content-Type: application/json" -X POST 'http://10.0.0.10:9200/library/books/' -d '{
"title": "Elasticsearch: The Definitive Guide007",
"name":{
"first":"xia",
"last":"zhi"
},
"publish_date":"2017-04-18",
"price":"100.99"
}'
查看文檔
通過ID查看文檔
curl -X GET 'http://10.0.0.10:9200/library/books/1'
通過_source獲取指定的字段
curl -X GET 'http://10.0.0.10:9200/library/books/1?_source=title'
curl -X GET 'http://10.0.0.10:9200/library/books/1?_source=title,price'
curl -X GET 'http://10.0.0.10:9200/library/books/1?_source'
更新文檔
通過ID更新
curl -H "Content-Type: application/json" -X PUT 'http://10.0.0.10:9200/library/books/2' -d '{
"title": "Elasticsearch: The Definitive Guide007",
"name":{
"first":"dong",
"last":"zhi"
},
"publish_date":"2017-04-18",
"price":"89.99"
}'
通過_update API方式單獨更新對應的字段
curl -H "Content-Type: application/json" -X POST 'http://10.0.0.10:9200/library/books/1/_update' -d '{
"doc":{
"price":10
}
}'
curl -H "Content-Type: application/json" -X POST 'http://10.0.0.10:9200/library/books/1/_update' -d '{
"doc":{
"title":"fly"
}
}'
刪除文檔
curl -X DELETE http://10.0.0.10:9200/library/books/1
刪除Type
curl -X DELETE http://10.0.0.10:9200/library/books
刪除索引
curl -X DELETE http://10.0.0.10:9200/library
elasticsearch內置字段及類型
內置字段
_uid, _id, _type, _source, _all, _analyzer, _boost, _parent, _routing, _index, _size, _timestamp, _ttl
字段類型
String, Integer/long, Float/double, Boolean, Null, Date
批量操作
創建兩個索引
curl -H "Content-Type: application/json" -X PUT 'http://10.0.0.10:9200/bank/' -d '{
"settings":{
"index":{
"number_of_shards":3,
"number_of_replicas":1
}
}
}'
curl -H "Content-Type: application/json" -X PUT 'http://10.0.0.10:9200/shakespeare/' -d '{
"settings":{
"index":{
"number_of_shards":3,
"number_of_replicas":1
}
}
}'