安装 Elastic Search
Quick start guide - Elasticsearch 8.14
下载和安装
Installing Elasticsearch
Download and install archive for Linux
Run Elasticsearch from the command line
下载压缩包:
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.17.3-linux-x86_64.tar.gz
校验 SHA512:(可选)
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.17.3-linux-x86_64.tar.gz.sha512
shasum -a 512 -c elasticsearch-8.17.3-linux-x86_64.tar.gz.sha512
校验正确应当输出:elasticsearch-8.17.3-linux-x86_64.tar.gz: OK
解压缩到 HOME 目录,其文件名默认为 elasticsearch-8.17.3
:
tar -xzf elasticsearch-8.17.3-linux-x86_64.tar.gz -C ~
启动
cd ~/elasticsearch-8.17.3
./bin/elasticsearch
首次启动 Elasticsearch 时,默认情况下会启用并配置安全功能。以下安全配置会自动发生:
- 启用身份验证和授权,并为内置超级用户
elastic
生成密码。- elastic 用户的密码和 Kibana 的注册令牌会在终端输出。
- 为传输层和 HTTP 层生成 TLS 的证书和密钥,并使用这些密钥和证书启用和配置 TLS。
- 为 Kibana 生成注册令牌,有效期为 30 分钟。
命令行输出形如:
✅ Elasticsearch security features have been automatically configured!
✅ Authentication is enabled and cluster connections are encrypted.
ℹ️ Password for the elastic user (reset with `bin/elasticsearch-reset-password -u elastic`):
********************
ℹ️ HTTP CA certificate SHA-256 fingerprint:
****************************************************************
ℹ️ Configure Kibana to use this cluster:
• Run Kibana and click the configuration link in the terminal when Kibana starts.
• Copy the following enrollment token and paste it into Kibana in your browser (valid for the next 30 minutes):
********************************************************************************
ℹ️ Configure other nodes to join this cluster:
• On this node:
⁃ Create an enrollment token with `bin/elasticsearch-create-enrollment-token -s node`.
⁃ Uncomment the transport.host setting at the end of config/elasticsearch.yml.
⁃ Restart Elasticsearch.
• On other nodes:
⁃ Start Elasticsearch with `bin/elasticsearch --enrollment-token <token>`, using the enrollment token that you generated.
ElasticSearch 默认运行端口为 9200
。
添加环境变量
将下列内容添加到 .bashrc
或 .zshrc
:
export ES_HOME=~/elasticsearch-8.17.3
export ELASTIC_PASSWORD="<your_password>"
export PATH=$ES_HOME/bin:$PATH
重新加载配置:
zsh
检查 Elasticsearch 是否运行
curl --cacert $ES_HOME/config/certs/http_ca.crt -u elastic:$ELASTIC_PASSWORD https://localhost:9200
输出形如:
{
"name" : "<your_hostname>",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "**********************",
"version" : {
"number" : "8.17.3",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "****************************************",
"build_date" : "2024-06-10T23:35:17.114581191Z",
"build_snapshot" : false,
"lucene_version" : "9.10.0",
"minimum_wire_compatibility_version" : "7.17.0",
"minimum_index_compatibility_version" : "7.0.0"
},
"tagline" : "You Know, for Search"
}
重置密码
elasticsearch-reset-password -u elastic
在集群中注册新节点
首先在运行 Elasticsearch 的终端中执行:
elasticsearch-create-enrollment-token -s node
然后在新节点的目录下执行:
elasticsearch --enrollment-token <enrollment-token>
这里的 <enrollment-token>
是上一步生成的注册令牌。 Elasticsearch 会自动在 config/certs
目录下生成证书和密钥。
命令行选项:
elasticsearch-create-enrollment-token --help
Creates enrollment tokens for elasticsearch nodes and kibana instances
Option (* = required) Description
--------------------- -----------
-E <KeyValuePair> Configure a setting
-f, --force Use this option to force execution of the command
against a cluster that is currently unhealthy.
-h, --help Show help
* -s, --scope The scope of this enrollment token, can be either "node"
or "kibana"
--url the URL where the elasticsearch node listens for
connections.
-v, --verbose Show verbose output
复制证书到其他目录
cp $ES_HOME/config/certs/http_ca.crt ~/repos/bili-scraper/configs/elastic_ca.crt
安装 Python client
pip install elasticsearch --upgrade
Python Client Examples
Python Client Helpers
安装插件
安装 Smart Chinese analysis 插件
elasticsearch-plugin install analysis-smartcn
Smart Chinese analysis plugin | Elasticsearch Plugins and Integrations [8.14] | Elastic
安装 IK 分词插件
elasticsearch-plugin install https://get.infini.cloud/elasticsearch/analysis-ik/8.17.3
注意:analysis-ik
插件需要与 Elasticsearch 版本匹配。
默认的 stopword.dic
文件中包含了一些停用词(stop word),分词的时候会被过滤。如果不想过滤这些词,可以将该 dic 文件清空:
cd $ES_HOME/config/analysis-ik
cp stopword.dic stopword.dic.bak && rm stopword.dic && touch stopword.dic
重启 Elasticsearch 以使清空配置的操作生效。
infinilabs/analysis-ik: 🚌 The IK Analysis plugin integrates Lucene IK analyzer into Elasticsearch and OpenSearch, support customized dictionary.
Elasticsearch 中文分词器-阿里云开发者社区
安装 pinyin 插件
elasticsearch-plugin install https://get.infini.cloud/elasticsearch/analysis-pinyin/8.17.3
注意:analysis-pinyin
插件需要与 Elasticsearch 版本匹配。
infinilabs/analysis-pinyin: 🛵 This Pinyin Analysis plugin is used to do conversion between Chinese characters and Pinyin.
安装 stconvert 插件
elasticsearch-plugin install https://get.infini.cloud/elasticsearch/analysis-stconvert/8.17.3
注意:analysis-stconvert
插件需要与 Elasticsearch 版本匹配。
infinilabs/analysis-stconvert: 中文简繁體互相转换.
重启 Elasticsearch 以使插件生效
cd ~/elasticsearch-8.17.3/
./bin/elasticsearch
或者:(可执行程序已经添加到环境变量)
elasticsearch
查看已安装插件
elasticsearch-plugin list
输出形如:
analysis-ik
analysis-pinyin
analysis-smartcn
analysis-stconvert
升级 ElasticSearch
Elastic Installation and Upgrade Guide [8.17] | Elastic
- 备份或清空数据
- 关停所有数据库读写任务
- 关停所有 ElasticSearch 和 Kibana 的服务
- 下载新版本:(例如
8.14.1
->8.17.3
)
- 由于上面的过程都是在独立的目录下进行,所以可以直接下载新版本并解压缩
- 只需要把所有步骤中的
8.14.1
替换为8.17.3
即可
- 升级 Kibana,详见:升级 Kibana