Elasticsearch是一个开源的分布式搜索和分析引擎,基于Apache Lucene构建。它提供了一个分布式、多租户能力的全文搜索引擎,具有HTTP Web接口和无模式JSON文档。
{
"query": {
"bool": {
"must": [
{ "match": { "title": "search" }},
{ "match": { "content": "elasticsearch" }}
],
"filter": [
{ "term": { "status": "published" }}
]
}
}
}
{
"aggs": {
"group_by_status": {
"terms": {
"field": "status"
}
}
}
}
下载Elasticsearch
解压安装包
# 解压到指定目录
C:\elasticsearch-8.17.4
配置环境变量
ES_HOME
环境变量%ES_HOME%\bin
添加到PATH
修改配置文件
# config/elasticsearch.yml
cluster.name: my-application
node.name: node-1
network.host: 0.0.0.0
http.port: 9200
discovery.type: single-node
启动服务
# 命令行启动
bin\elasticsearch.bat
# 作为Windows服务安装
bin\elasticsearch-service.bat install
bin\elasticsearch-service.bat start
# 导入Elasticsearch GPG密钥
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg
# 添加Elasticsearch源
echo "deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://artifacts.elastic.co/packages/8.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-8.x.list
# 更新包列表并安装
sudo apt update
sudo apt install elasticsearch
# 配置服务
sudo systemctl daemon-reload
sudo systemctl enable elasticsearch
sudo systemctl start elasticsearch
# 导入Elasticsearch GPG密钥
sudo rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
# 添加Elasticsearch源
sudo tee /etc/yum.repos.d/elasticsearch.repo << EOF
[elasticsearch]
name=Elasticsearch repository for 8.x packages
baseurl=https://artifacts.elastic.co/packages/8.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
EOF
# 安装Elasticsearch
sudo yum install elasticsearch
# 配置服务
sudo systemctl daemon-reload
sudo systemctl enable elasticsearch
sudo systemctl start elasticsearch
# 安装Homebrew(如果未安装)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# 安装Elasticsearch
brew tap elastic/tap
brew install elastic/tap/elasticsearch-full
# 启动服务
brew services start elasticsearch-full
# 检查服务状态
curl -X GET "localhost:9200"
# 预期输出
{
"name" : "node-1",
"cluster_name" : "my-application",
"cluster_uuid" : "xxxxx",
"version" : {
"number" : "8.17.4",
"build_flavor" : "default",
"build_type" : "zip",
"build_hash" : "xxxxx",
"build_date" : "2024-03-20T15:39:59.811110136Z",
"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"
}
# 生成密码
bin/elasticsearch-reset-password -u elastic
# 配置TLS证书
bin/elasticsearch-certutil ca
bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12
# 配置安全设置
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
# config/jvm.options
-Xms1g
-Xmx1g
# Linux/macOS
sudo chown -R elasticsearch:elasticsearch /path/to/elasticsearch
IK分词器
# 安装IK分词器
bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v8.17.4/elasticsearch-analysis-ik-8.17.4.zip
拼音分词器
# 安装拼音分词器
bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-pinyin/releases/download/v8.17.4/elasticsearch-analysis-pinyin-8.17.4.zip
SQL插件
# 安装SQL插件
bin/elasticsearch-plugin install https://github.com/elastic/elasticsearch-sql/releases/download/8.17.4/elasticsearch-sql-8.17.4.zip
# IK分词器配置
index:
analysis:
analyzer:
ik_smart:
type: ik
use_smart: true
ik_max_word:
type: ik
use_smart: false
# config/jvm.options
-Xms4g
-Xmx4g
-XX:+UseG1GC
-XX:MaxGCPauseMillis=200
-XX:InitiatingHeapOccupancyPercent=35
分片设置
PUT /my_index
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1,
"refresh_interval": "30s"
}
}
映射优化
PUT /my_index/_mapping
{
"properties": {
"title": {
"type": "text",
"analyzer": "ik_max_word",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"price": {
"type": "scaled_float",
"scaling_factor": 100
}
}
}
使用过滤器缓存
{
"query": {
"bool": {
"filter": [
{ "term": { "status": "active" }},
{ "range": { "price": { "gte": 100 }}}
]
}
}
}
分页优化
{
"from": 0,
"size": 10,
"sort": [
{ "_score": "desc" },
{ "timestamp": "desc" }
]
}
集群健康
GET _cluster/health
GET _cluster/stats
节点状态
GET _nodes/stats
GET _nodes/hot_threads
索引统计
GET _stats
GET _cat/indices?v
Elasticsearch Curator
# 安装
pip install elasticsearch-curator
# 使用示例
curator --config config.yml action.yml
Elasticsearch Exporter
# 安装Prometheus Exporter
docker run -d --name elasticsearch-exporter \
-p 9114:9114 \
justwatch/elasticsearch_exporter \
--es.uri=http://elasticsearch:9200
创建快照仓库
PUT _snapshot/my_backup
{
"type": "fs",
"settings": {
"location": "/mnt/backups/elasticsearch"
}
}
创建快照
PUT _snapshot/my_backup/snapshot_1
{
"indices": "index1,index2",
"ignore_unavailable": true,
"include_global_state": false
}
恢复快照
POST _snapshot/my_backup/snapshot_1/_restore
{
"indices": "index1,index2",
"rename_pattern": "index(.+)",
"rename_replacement": "restored_index$1"
}
# elasticsearch.yml
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.http.ssl.enabled: true
# 创建角色
POST _security/role/my_role
{
"indices": [
{
"names": ["my_index"],
"privileges": ["read", "write"]
}
]
}
# 创建用户
POST _security/user/my_user
{
"password": "changeme",
"roles": ["my_role"]
}
# elasticsearch.yml
xpack.security.audit.enabled: true
xpack.security.audit.logfile.events.include: authentication_failed,access_denied
慢查询分析
GET _search
{
"profile": true,
"query": {
"match": {
"message": "search term"
}
}
}
内存使用分析
GET _nodes/stats/jvm
GET _cat/nodes?v&h=name,heap.percent,ram.percent
分片分配问题
GET _cluster/allocation/explain
GET _cat/shards?v
节点加入问题
GET _cluster/state?filter_path=metadata.cluster_uuid
GET _nodes?filter_path=nodes.*.name
滚动升级
# 停止节点
systemctl stop elasticsearch
# 升级软件包
apt-get update && apt-get install elasticsearch
# 启动节点
systemctl start elasticsearch
全集群升级
# 停止所有节点
# 升级所有节点
# 启动所有节点