前言
本文是之前做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 ,如第二行所示 * 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
网友评论