title: 34.ElasticSearch安装配置 CreateTime: 2019-07-11 00:00:34 UpdateTime: 2019-07-11 00:00:34 CategoryName: web --- --- title: "34.ElasticSearch安装配置" date: 2019-07-11T00:00:34+08:00 draft: false tags: ["web"] categories: ["web"] author: "springrain" --- **下载** -------- 从官网下载:https://www.elastic.co/downloads/elasticsearch 要求JDK8+,CentOS 7+ ![](/public/34/image1.jpeg) ES 6.5.3下载地址: https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.3.tar.gz **修改Linux配置** ----------------- ### 需要修改操作系统的 ulimit 为 1024000 ### ### /etc/security/limits.conf 中增加### ``` * soft nofile 1024000 * hard nofile 1024000 ``` ###sysctl -p 立即生效### ###修改 操作系统网络参数 ### ### /etc/sysctl.conf #### ``` fs.file-max = 1024000 vm.max_map_count=1024000 net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_tw_recycle = 0 net.ipv4.tcp_timestamps = 1 net.ipv4.tcp_fin_timeout = 30 net.ipv4.ip_local_port_range = 10000 65000 net.ipv4.tcp_max_tw_buckets=100000 net.ipv4.tcp_mem = 786432 2097152 3145728 net.ipv4.tcp_rmem = 4096 4096 16777216 net.ipv4.tcp_wmem = 4096 4096 16777216 ``` ###systemctl restart network 重启网络生效### **创建用户** ------------ ElasticSearch处于安全考虑,禁用了root权限,需要创建单独的用户. ```shell groupadd esuser ##添加用户组## useradd esuser -g esuser ##添加用户 useradd 用户名 -g 组名## ##更改elasticsearch文件夹及内部文件的所属用户及组为esuser:esuser## chown -R esuser:esuser es ##chown -R 用户:用户组 目录## ##es为你elasticsearch的目录名称## ##切换到esuser用户再解压## su esuser ##切换账户## tar -zxvf elasticsearch-6.5.3.tar.gz ##注意:不要用root用户,如果已使用root用户解压缩了,先改文件夹所属用户组和用户.## chown -R esuser:esuser es ``` **修改ES内存** -------------- 修改 conf/jvm.option文件,设置最小内存和最大内存,不要超过32G,超过32G JVM会启用内存压缩,影响性能. **配置示例:[elasticsearch.yml](/public/34/elasticsearch.yml)** **启动访问** ------------ ### esuser用户运行ES su esuser 运行 bin/elasticsearch 显示启动,ctrl+c可以终止 bin/elasticsearch -d 后台模式运行 直接运行启动 su esuser -c "/usr/local/es/es1/bin/elasticsearch -d" 关闭 ps -ef |grep es1 查看进程号 kill -9 强制关闭 ### head查看 下载head项目https://github.com/mobz/elasticsearch-head ![](/public/34/image3.jpeg) 下载完成之后,直接解压打开index.html 输入访问的es路径 http://xxx:9201 ![](/public/34/image4.jpeg) **安装IK分词器** ---------------- 下载IK分词器: https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.5.3/elasticsearch-analysis-ik-6.5.3.zip 解压到 ES的 plugins/analysis-ik 目录, unzip elasticsearch-analysis-ik-6.5.3.zip -d es1/plugins/analysis-ik 重启ES,分词插件生效.自定义词库需要在analysis-ik/config/IKAnalyzer.cfg.xml文件中配置. **索引** -------- es支持restful,实现增(POST)删(DELETE)改(PUT)查(GET)操作,可通过CURL进行操作,也可以通过head插件来执. ![](/public/34/image5.jpeg) **[索引JSON例子模板.json](/public/34/索引JSON例子模板.json)** ![](/public/34/image7.jpeg) **别名** -------- 一个别名一般对应一个索引,也可以关联多个索引,索引的意义就是如果索引有较大修改,可以把别名暂时对应到其他索引,不影响业务.别名和索引的用法基本一致,建议每个索引都使用别名. 这里有个例子,将别名azys和索引zys建立关联: ``` curl -XPOST 'http://localhost:9200/_aliases' -d ' { "actions": [{"add": {"index": "zys","alias": "azys" }}] }' ``` 一个别名也可以被移除,比如: ``` curl -XPOST 'http://localhost:9200/_aliases' -d ' { "actions": [{"remove": {"index": "zys", "alias": "azys"}} ] }' ``` 重命名一个别名就是一个简单的remove然后add的操作,也是使用相同的API.这个操作是原子的,无需担心这个别名未能指向一个索引的简短时间: ``` curl -XPOST 'http://localhost:9200/_aliases' -d ' { "actions": [ {"remove": {"index": "zys", "alias": "azys"}}, {"add": {"index":"zys", "alias": "bzys"}} ] }' ``` **mappings** ------------ 创建mappings ![](/public/34/image8.jpeg) **[mapings数据模板.json](/public/34/mapings数据模板.json)** ![](/public/34/image10.jpeg) **测试IK分词** -------------- curl -XPOST http://localhost:9200/zys/_analyze/ -d -H 'Content-Type:application/json' -d '{"analyzer": "ikMax","text": "中华人民共和国"}' ![](/public/34/image11.jpeg) **常用内置关键字** ------------------ _settings:管理指定索引的设置 _mappings:管理指定索引的mappings _doc:老版本移除了mappingType,新版本默认为_doc. _aliases:管理所有索引的别名 _snapshot:管理所有索引的备份快照 _search:查询信息 ~~_default _all~~ 已经废弃