學習筆記 | ElasticSearch學習筆記(基礎)
前言
本文是之前做ElasticSearch技術測試時的筆記,時間比較久遠了,如有錯誤請指正~
安裝
-
官網: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 #如果設定了系統代理需要將本地地址新增例外 export no_proxy="localhost,127.0.0.1"
-
配置檔案
//ES新增機器很簡單,只需要改成相同的cluster.name就可以自己識別 cluster.name: es-sts node.name: sts-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
原理
-
倒排索引
倒排索引兩部分組成:單詞詞典和倒排檔案
倒排列表:記載了出現過某個單詞的所有文件的文件列表及單詞在該文件中出現的位置資訊
單詞詞典:單詞本身的資訊及指向“倒排列表”的指標
分詞:對每個單詞賦予唯一的單詞編號,同時記錄下哪些文件包含這個單詞
新文件解析完畢時,相應的詞典結構也就建立起來了
-
match、term、match_phrase
-
重新索引:建立新的索引,把文件從舊的索引複製到新的索引
-
索引主分片數:number_of_shards
主分片的副本數:number_of_replicas
使用REST API
-
檢視健康情況
curl -XGET http://localhost:9200/_cat/health?v
-
檢視節點
curl -XGET http://localhost:9200/_cat/nodes?v
-
檢視索引列表
curl -XGET http://localhost:9200/_cat/indices?v
-
建立索引
curl -XPUT http://localhost:9200/customer?pretty
-
新增資料
curl -XPUT 'localhost:9200/customer/external/1?pretty&pretty' -H 'Content-Type:application/json' -d' {"name": "John Doe"}'
-
檢視資料
curl -XGET 'localhost:9200/customer/external/1?pretty&pretty'
-
刪除索引
curl -XDELETE 'localhost:9200/index1?pretty&pretty'
-
不指定id,隨機生成一個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 啟動之後只能在本地訪問
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
可以使其生效,或者重啟機器error[2]
vim /etc/security/limits.d/90-nproc.conf #修改下邊的1024為2048 ,如第二行所示 *softnproc1024 *softnproc2048
error[3]
vim /etc/sysctl.conf #修改或新增 vm.max_map_count = 262144
error[4]
#修改elasticsearch的配置檔案 bootstrap.memory_lock: flase bootstrap.system_call_filter: false