elasticsearch8安装使用 - 简书


本站和网页 http://events.jianshu.io/p/c6ad06b20a18 的作者无关,不对其内容负责。快照谨为网络故障时之索引,不代表被搜索网站的即时页面。

elasticsearch8安装使用 - 简书登录注册写文章首页下载APP会员IT技术elasticsearch8安装使用freeok关注赞赏支持elasticsearch8安装使用1、centos环境下安装
安装包下载
下载elasticsearch8安装包elasticsearch-8.1.0-linux-x86_64.tar.gz,并下载好jdk-17.0.2,解压后目录为/software/elasticsearch-8.1.0和/software/jdk-17.0.2。
ES环境变量配置
export ES_HOME=/software/elasticsearch-8.1.0
export ES_JAVA_HOME=/software/jdk-17.0.2
创建ES专用账号(需要使用此账号来管理ES)
useradd elastic
chown elastic /software/elasticsearch-8.1.0 -R
使用root账号更改打开的文件数限制及最大虚拟内存限制
查看当前用户文件数限制
ulimit -n
vi /etc/security/limits.conf 文件最后添加两行
elastic soft nofile 65536
elastic hard nofile 65536
将elastic用户的文件数限制调整为65536
vi /etc/sysctl.conf 文件最后添加一行
vm.max_map_count=655360
使用sysctl -p使系统配置生效
屏蔽自动下载geopip
vi config/elasticsearch.yml
修改两项host
network.host: 192.168.91.133
discovery.seed_hosts: ["192.168.91.133"]
并在文件最后添加一行
ingest.geoip.downloader.enabled: false
切换到elastic账号启动ES(加上-d参数表示后台启动)
su elastic
cd /software/elasticsearch-8.1.0
./bin/elasticsearch
首次启动后控制台会打印出一些预设置的log信息,以及会在elasticsearch.yml配置文件的的最后面增加SECURITY相关配置,可以不用理会,因为需要使用时可用脚本工具去修改默认生成的一些配置信息;
2、修改默认配置
第一次启动完成后会自动生成一些信息,接下来修改一下默认密码
修改默认密码
bin/elasticsearch-reset-password --username elastic -i
elastic8888
配置ssl为false(首次启动后才会自动生成xpack相关配置项,改为false后重启)
xpack.security.http.ssl:
enabled: false
重新启动后安装结果
curl -u elastic:elastic8888 http://192.168.91.133:9200
"name" : "localhost.localdomain",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "W4dJfYBGTOyJr2S1w2W8Hg",
"version" : {
"number" : "8.1.0",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "3700f7679f7d95e36da0b43762189bab189bc53a",
"build_date" : "2022-03-03T14:20:00.690422633Z",
"build_snapshot" : false,
"lucene_version" : "9.0.0",
"minimum_wire_compatibility_version" : "7.17.0",
"minimum_index_compatibility_version" : "7.0.0"
},
"tagline" : "You Know, for Search"
此时是未开启ssl的情况下可以正常访问,同时也可以开启ssl完成请求
开启ssl访问
打开config/elasticsearch.yml中可以看到配置项xpack.security.http.ssl.keystore.path: certs/http.p12,此http.p12证书为默认生成的,使用脚本工具重新生成一份替换它,生成命令如下
./bin/elasticsearch-certutil http
Generate a CSR? [y/N] N
Use an existing CA? [y/N] N
Do you wish to change any of these options? [y/N] y
CA Name [CN=Elasticsearch HTTP CA] www.minxyz.com
CA password: [<ENTER> for none] 123456
Repeat password to confirm: 123456
For how long should your certificate be valid? [5y] [ENTER]
Generate a certificate per node? [y/N]N
Enter all the hostnames that you need, one per line.
When you are done, press <ENTER> once more to move on to the next step.
www.minxyz.com
You entered the following hostnames.
- www.minxyz.com
Is this correct [Y/n]Y
Key Name: www.minxyz.com
Subject DN: CN=www, DC=minxyz, DC=com
Key Size: 2048
Do you wish to change any of these options? [y/N]N
If you wish to use a blank password, simply press <enter> at the prompt below.
Provide a password for the "http.p12" file: [<ENTER> for none] 123456
Repeat password to confirm: 123456
What filename should be used for the output zip file? [/software/elasticsearch-8.1.0/elasticsearch-ssl-http.zip] [ENTER]
Zip file written to /software/elasticsearch-8.1.0/elasticsearch-ssl-http.zip
解压elasticsearch-ssl-http.zip后,在目录elasticsearch中可以看到新生成的文件http.p12,将其替换掉config/cert/http.p12,由于生成此证书时密码为123456,所以需要重新设置keystore的密码(需注意不是在配置文件上添加密码配置项),命令如下
./bin/elasticsearch-keystore add "xpack.security.http.ssl.keystore.secure_password"
重新打开ssl配置
xpack.security.http.ssl:
enabled: true
配置hosts为192.168.91.133 www.minxyz.com(此域名为任意定义的),重启elasticsearch,此时需要使用https来访问ES,可以先增加-k参数允许不使用证书连接到SSL站点(若浏览器可以强制访问不安全的地址)
curl -k -u elastic:elastic8888 https://www.minxyz.com:9200
"name" : "localhost.localdomain",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "W4dJfYBGTOyJr2S1w2W8Hg",
"version" : {
"number" : "8.1.0",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "3700f7679f7d95e36da0b43762189bab189bc53a",
"build_date" : "2022-03-03T14:20:00.690422633Z",
"build_snapshot" : false,
"lucene_version" : "9.0.0",
"minimum_wire_compatibility_version" : "7.17.0",
"minimum_index_compatibility_version" : "7.0.0"
},
"tagline" : "You Know, for Search"
若需要安全访问,需要增加证书配置,通过以下命令生成http.pem后,使用http.pem证书完成https请求访问
openssl pkcs12 -nodes -in http.p12 -out http.pem
curl --cert ./http.pem --cacert ./http.pem -u elastic:elastic8888 https://www.minxyz.com:9200
在浏览器上需要安全访问,可将http.p12证书安装到根证书目录下之后访问
elasticsearch-ssl
elasticsearch-ssl
3、ES常用操作
由于直接使用curl操作相对较麻烦,临时扩展定义一个ecurl别名,如下(后面即可用ecurl简化操作)
alias ecurl='curl --cert ./http.pem --cacert ./http.pem -u elastic:elastic8888'
查看es版本信息
ecurl https://www.minxyz.com:9200
查看所有索引
ecurl https://www.minxyz.com:9200/_cat/indices?v
新增索引
新增完成后查看索引的状态是yellow,默认有一个副本
ecurl -X PUT https://www.minxyz.com:9200/user-index
修改副本数
非集群状态下修改完副本数后即变为green
ecurl -X PUT -H "content-type:application/json;charset=utf-8" https://www.minxyz.com:9200/user-index/_settings -d "{\"number_of_replicas\": 0}"
查看索引的mapping
ecurl https://www.minxyz.com:9200/user-index/_mapping?pretty
新增索引对应的mapping(其中user-index为索引名)
ecurl -X POST --cert ./http.pem --cacert ./http.pem -u elastic:elastic8888 -H "content-type:application/json;charset=utf-8" https://www.minxyz.com:9200/user-index/_mapping -d \
"{ \
\"properties\": { \
\"name\": { \
\"type\": \"keyword\" \
}, \
\"type\": { \
\"type\": \"integer\" \
}, \
\"content\": { \
\"type\": \"text\" \
} \
} \
}"
向索引里面新增测试数据
ecurl -X POST -H "content-type:application/json;charset=utf-8" https://www.minxyz.com:9200/user-index/_create/1 -d "{\"name\":\"测试姓名\",\"type\":100,\"content\":\"测试永远滴神啊。。。\"}"
根据文档id查找文档
ecurl https://www.minxyz.com:9200/user-index/_doc/1?pretty
ecurl https://www.minxyz.com:9200/user-index/_search?q=_id:1
检索所有文档
ecurl -X POST -H "Content-Type:application/json;charset=UTF-8" "https://www.minxyz.com:9200/user-index/_search?pretty" -d "{\"query\":{\"match_all\":{}}}"
按查询条件查询
ecurl -X POST -H "content-type:application/json;charset=utf-8" https://www.minxyz.com:9200/user-index/_search?pretty -d \
"{ \
\"query\": { \
\"match\": { \
\"content\": \"永远滴神\" \
} \
} \
}"
根据文档id删除文档
ecurl -X DELETE https://www.minxyz.com:9200/user-index/_doc/1?pretty
查看全部索引配置或某个索引配置
ecurl https://www.minxyz.com:9200/_settings/_all?pretty
ecurl https://www.minxyz.com:9200/user-index/_settings?pretty
创建、查看与删除索引别名
创建索引别名
ecurl -X POST -H "content-type:application/json;charset=utf-8" https://www.minxyz.com:9200/_aliases -d'
"actions": [{
"add": {
"index": "test-create-3",
"alias": "article-index-master"
查看索引别名
ecurl https://www.minxyz.com:9200/_aliases?pretty
删除索引别名
ecurl -X POST https://www.minxyz.com:9200/_aliases -H 'Content-Type: application/json' -d'
"actions": [
"remove": {
"index": "test-create-3",
"aliases": "article-index-master"
删除索引
ecurl -X DELETE https://www.minxyz.com:9200/user-index
查看分片信息
ecurl https://www.minxyz.com:9200/_cat/shards?v
查看集群健康状态
ecurl https://www.minxyz.com:9200/_cluster/health?pretty=true
查看副本信息
ecurl -X GET "https://www.minxyz.com:9200/_cluster/allocation/explain?pretty" -H 'Content-Type: application/json' -d'
"index": "user-index",
"shard": 0,
"primary": false,
"current_node": "localhost.localdomain"
查看分片详情
ecurl https://www.minxyz.com:9200/_cat/allocation?v
4、中文分词
安装中文分词插件sudo bin/elasticsearch-plugin install analysis-smartcn,安装成功后重启es
分词分析
ecurl -X POST -H 'Content-Type: application/json' https://www.minxyz.com:9200/_analyze?pretty -d'
"analyzer":"smartcn",
"text":"测试永远滴神啊。。。"
}'
自定义中文词库
安装IK中文分词器
./elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v8.1.0/elasticsearch-analysis-ik-8.1.0.zip
比如要将“永远滴神”定义为一个词,需要在配置IKAnalyzer.cfg.xml中指定<entry key="ext_dict">customer.dic</entry>,然后创建文件customer.dic,并添加一行“永远滴神”,重启后再次访问即可看到分词结果
ecurl -X POST -H 'Content-Type: application/json' https://www.minxyz.com:9200/_analyze?pretty -d'
> {
> "analyzer":"ik_smart",
> "text":"测试永远滴神啊。。。"
> }'
"tokens" : [
"token" : "测试",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
},
"token" : "永远滴神",
"start_offset" : 2,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 1
},
"token" : "啊",
"start_offset" : 6,
"end_offset" : 7,
"type" : "CN_CHAR",
"position" : 2
推荐阅读更多精彩内容linux下安装ElasticSearch一、下载软件二、elasticsearch常见错误2.1. root用户启动elasticsearch报错2.1....KwongRay阅读 289评论 0赞 0ElasticSearch的Windows的安装和简单使用ElasticSearch的基本概念 Elaticsearch,简称为ES, ES是一个开源的高扩展的分布式全文检...doriayy阅读 879评论 0赞 0Elasticsearch 安装与简单使用1.Search Engine 目前主流的开源搜索引擎主要有两个,一个是基于Java的Apache Lucene,...秦汉邮侠阅读 704评论 0赞 1【工作】ElasticSearch技术全脉络概述和摘要 ES已经发展了10年,版本来到7.x,Lucene发展了20年,版本来到8.x。期间的技术迭代与更新真...苏柏亚的星空阅读 872评论 0赞 1“源产控”系列(三)在CentOS 8上使用Elastic Stack: Elasticsea...本篇为慧响技术角“源产控”专题系列第3篇文章。 慧响技术角“源产控”专题,将聚焦开源、国产化、自主可控三个方向的技...zning1994阅读 230评论 0赞 3Linux环境安装Elasticsearch集群高可用及使用Elasticsearch优势 横向可扩展性:只需要增加台服务器,做一点儿配置,启动一下Elasticsearch...AncientJazz阅读 2,854评论 0赞 7elasticsearch7安装并搭建集群,kibana,header&ik分词器插件安装基于系统centos7 ---------------------------------------------...virtual灬zzZ阅读 819评论 0赞 2ElasticSearch的概述和使用1. ElasticSearch概述 Elaticsearch,简称为es, es是一个开源的高扩展的分布式全文检...弹钢琴的崽崽阅读 1,667评论 1赞 2Elasticsearch 默认分词器和中分分词器之间的比较及使用方法转载出处:https://zhuanlan.zhihu.com/p/29183128介绍:ElasticSearc...树上骑个猴阅读 599评论 0赞 2linux下基本软件安装1.下载linux64位安装包 centos官网下载iso镜像 centos出现“FirewallD is not...泥煤的嘎嘎阅读 835评论 0赞 3Elasticsearch集群、Kibana配置及常用REST指令第一部分 集群配置 1 准备环境 首先准备3台机器,例如预装CentOS 7.6,可以分别修改下hostname,...赤色要塞满了阅读 620评论 0赞 115.ElasticSearch介绍及基本使用ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RE...MononokeHime阅读 1,196评论 0赞 0Elasticsearch性能优化欢迎访问我的博客查看原文:http://wangnan.tech 注:文本整理自《ELKstack权威指南》 目录...GhostStories阅读 9,651评论 0赞 7ElasticSearch系列一:安装,使用,搜索此处需补充: 安装cerebro及es集群 1.windows版 1.1安装ElasticSearch(以6.5....suxin1932阅读 983评论 0赞 2Elasticsearch 基础知识及安装异常处理今天要修改原来的代码, 重新安装了ElasticSearch, 在安装过程中发生了一些问题, 刚好在搜索到这篇文章...欢乐的弹跳机阅读 1,970评论 0赞 9Substrate的transaction-payment模块分析Substrate的transaction-payment模块分析 transaction-payment模块提供...建怀阅读 7,549评论 0赞 42019-11-28 17316宿命:用概率思维提高你的胜算 以前的我是风险厌恶者,不喜欢去冒险,但是人生放弃了冒险,也就放弃了无数的可能。 ...yichen大刀阅读 5,402评论 0赞 42019-11-28三数灯谜分析公元:2019年11月28日19时42分农历:二零一九年 十一月 初三日 戌时干支:己亥乙亥己巳甲戌当月节气:立冬...石放阅读 6,467评论 0赞 2零基础也不怕!CAD新手快速入门技巧,后悔没有早知道!想要快速入门CAD,对于零基础的新手来说的确有一定的困难。不过只要你掌握了以下这些CAD快速入门技巧,你就跨进了C...努力的王榆阅读 2,665评论 0赞 32019-11-27昨天考过了阿里规范,心里舒坦了好多,敲代码也犹如神助。早早完成工作回家喽常亚星阅读 2,804评论 0赞 1评论0赞11赞2赞赞赏更多好文