前言
本文是之前做ElasticSearch技術(shù)測試時的筆記宛官,時間比較久遠(yuǎn)了融击,如有錯誤請指正~
安裝
-
官網(wǎng): https://www.elastic.co/downloads/elasticsearch
curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.1.tar.gz
-
5.5.1版本必須是jdk1.8及以上版本蚣驼,不能用root用戶啟動
export JAVA_HOME="/usr/java/jdk1.8.0_111" export JRE_HOME=$JAVA_HOME/jre export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin:/opt/node/bin #如果設(shè)置了系統(tǒng)代理需要將本地地址添加例外 export no_proxy="localhost,127.0.0.1"
-
配置文件
//ES新增機(jī)器很簡單偿警,只需要改成相同的cluster.name就可以自己識別 cluster.name: smile-dev node.name: smile-1 path.data: /home/cdh/es/data path.logs: /home/cdh/es/logs bootstrap.memory_lock: false bootstrap.system_call_filter: false network.host: 0.0.0.0
原理
-
倒排索引
倒排索引兩部分組成:單詞詞典和倒排文件
倒排列表:記載了出現(xiàn)過某個單詞的所有文檔的文檔列表及單詞在該文檔中出現(xiàn)的位置信息
單詞詞典:單詞本身的信息及指向“倒排列表”的指針
分詞:對每個單詞賦予唯一的單詞編號,同時記錄下哪些文檔包含這個單詞
新文檔解析完畢時苛秕,相應(yīng)的詞典結(jié)構(gòu)也就建立起來了 match肌访、term、match_phrase
重新索引:創(chuàng)建新的索引艇劫,把文檔從舊的索引復(fù)制到新的索引
索引主分片數(shù):number_of_shards
主分片的副本數(shù):number_of_replicas
使用REST API
-
查看健康情況
curl -XGET http://localhost:9200/_cat/health?v
-
查看節(jié)點(diǎn)
curl -XGET http://localhost:9200/_cat/nodes?v
-
查看索引列表
curl -XGET http://localhost:9200/_cat/indices?v
-
創(chuàng)建索引
curl -XPUT http://localhost:9200/customer?pretty
-
添加數(shù)據(jù)
curl -XPUT 'localhost:9200/customer/external/1?pretty&pretty' -H 'Content-Type:application/json' -d' {"name": "John Doe"}'
-
查看數(shù)據(jù)
curl -XGET 'localhost:9200/customer/external/1?pretty&pretty'
-
刪除索引
curl -XDELETE 'localhost:9200/index1?pretty&pretty'
-
不指定id吼驶,隨機(jī)生成一個ID
curl -XPOST 'localhost:9200/customer/external?pretty&pretty' -H 'Content-Type: application/json' -d' {"name": "Jane Doe"}'
-
更新文檔
//更新文檔并添加age字段 curl -XPOST 'localhost:9200/customer/external/1/_update?pretty&pretty' -H 'Content-Type: application/json' -d' {"doc": { "name": "Jane Doe", "age": 20 }}' //通過簡單的腳本更新 curl -XPOST 'localhost:9200/customer/external/1/_update?pretty&pretty' -H 'Content-Type: application/json' -d' {"script" : "ctx._source.age += 5"}'
-
刪除文檔
curl -XDELETE 'localhost:9200/customer/external/2?pretty&pretty'
-
批量添加
curl -XPOST 'localhost:9200/customer/external/_bulk?pretty&pretty' -H 'Content-Type: application/json' -d' {"index":{"_id":"1"}} {"name": "John Doe" } {"index":{"_id":"2"}} {"name": "Jane Doe" } '
-
批量更新
curl -XPOST 'localhost:9200/customer/external/_bulk?pretty&pretty' -H 'Content-Type: application/json' -d' {"update":{"_id":"1"}} {"doc": { "name": "John Doe becomes Jane Doe" } } {"delete":{"_id":"2"}} '
-
從文件讀取
curl -H "Content-Type: application/json" -XPOST 'localhost:9200/bank/account/_bulk?pretty&refresh' --data-binary "@accounts.json"
-
檢索
curl -XGET 'localhost:9200/bank/_search?q=*&sort=account_number:asc&pretty&pretty' curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d' { "query": { "match_all": {} }, "sort": [ { "account_number": "asc" } ] } '
異常處理
-
elasticsearch 啟動之后只能在本地訪問
- 防火墻問題,關(guān)閉防火墻或開啟端口訪問權(quán)限
- 修改配置文件店煞,
network.host: 0.0.0.0
-
network.host: 0.0.0.0 之后報錯
錯誤信息[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536] [2]: max number of threads [1024] for user [cdh] is too low, increase to at least [2048] [3]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144] [4]: system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk
解決方法:
error[1]:vim /etc/security/limits.conf #添加或修改以下兩行代碼 * soft nofile 65536 * hard nofile 131072
limits.conf
修改下后不能生效旨剥,貌似是centos6.8之前的版本問題
su - cdh
可以使其生效,或者重啟機(jī)器error[2]
vim /etc/security/limits.d/90-nproc.conf #修改下邊的1024為2048 浅缸,如第二行所示 * soft nproc 1024 * soft nproc 2048
error[3]
vim /etc/sysctl.conf #修改或添加 vm.max_map_count = 262144
error[4]
#修改elasticsearch的配置文件 bootstrap.memory_lock: flase bootstrap.system_call_filter: false