Elasticsearch 7.x 新手指南

Elasticsearch 7.17 新手指南

一、环境说明

  • 服务器系统:CentOS Linux release 7.4.1708
  • ES版本:Elasticsearch 7.17.23(最终兼容版,不支持8.x)
  • 运行方式:YUM安装、Systemd管理、单机开发模式
  • 访问地址:http://127.0.0.1:9200

二、服务器前置优化

2.1 关闭防火墙、SELinux

bash 复制代码
systemctl stop firewalld && systemctl disable firewalld
setenforce 0
sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

2.2 修改内核虚拟内存

bash 复制代码
echo "vm.max_map_count=262144" >> /etc/sysctl.conf
sysctl -p

2.3 修改文件句柄限制

bash 复制代码
cat >> /etc/security/limits.conf << EOF
* soft nofile 65536
* hard nofile 65536
* soft nproc 4096
* hard nproc 4096
EOF

三、Elasticsearch 安装步骤

3.1 导入官方密钥

bash 复制代码
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch

3.2 编写YUM源

bash 复制代码
vi /etc/yum.repos.d/elasticsearch.repo

写入内容:

ini 复制代码
[elasticsearch]
name=Elasticsearch 7.x Stable
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md

3.3 安装指定稳定版

bash 复制代码
yum clean all
yum install -y elasticsearch-7.17.23

3.4 修改核心配置

bash 复制代码
vi /etc/elasticsearch/elasticsearch.yml

清空原有内容,粘贴以下配置:

yaml 复制代码
cluster.name: es-cluster
node.name: node-1
network.host: 0.0.0.0
http.port: 9200
discovery.type: single-node
xpack.security.enabled: false

3.5 启停命令

bash 复制代码
# 启动
systemctl start elasticsearch
# 开机自启
systemctl enable elasticsearch
# 查看状态
systemctl status elasticsearch
# 重启
systemctl restart elasticsearch
# 停止
systemctl stop elasticsearch
# 重载服务(报错找不到服务执行)
systemctl daemon-reload

3.6 验证是否安装成功

bash 复制代码
curl 127.0.0.1:9200

四、ES核心概念(必背)

Elasticsearch MySQL
索引(index) 数据库
文档(document) 一行数据
字段(field) 列/字段
类型(_doc) 表(7.x统一为_doc)

五、_cat运维监控命令(常用)

口诀:加?v 带表头给人看;不加?v 纯数据给程序

bash 复制代码
# 查看集群健康
curl http://127.0.0.1:9200/_cat/health?v

# 查看所有索引(最常用)
curl http://127.0.0.1:9200/_cat/indices?v

# 查看节点
curl http://127.0.0.1:9200/_cat/nodes?v

# 查看分片
curl http://127.0.0.1:9200/_cat/shards?v

# 查看主节点
curl http://127.0.0.1:9200/_cat/master?v

健康颜色说明:

  • green:全部正常
  • yellow:主分片正常、副本未分配(单机正常)
  • red:数据丢失、故障

六、索引操作(建库、删库)

6.1 创建索引

bash 复制代码
curl -XPUT http://127.0.0.1:9200/test_index

6.2 删除索引

bash 复制代码
curl -XDELETE http://127.0.0.1:9200/test_index

6.3 查看索引结构

bash 复制代码
curl http://127.0.0.1:9200/test_index/_mapping?pretty

七、基础CRUD增删改查

7.1 新增数据(自动ID)

bash 复制代码
curl -XPOST http://127.0.0.1:9200/test_index/_doc \
-H "Content-Type: application/json" \
-d '{
  "name":"张三",
  "age":25,
  "city":"北京"
}'

7.2 指定ID新增/覆盖

bash 复制代码
curl -XPUT http://127.0.0.1:9200/test_index/_doc/1001 \
-H "Content-Type: application/json" \
-d '{
  "name":"李四",
  "age":30,
  "city":"上海"
}'

7.3 根据ID查询

bash 复制代码
curl http://127.0.0.1:9200/test_index/_doc/1001

7.4 删除单条文档

bash 复制代码
curl -XDELETE http://127.0.0.1:9200/test_index/_doc/1001

八、中级常用查询语法

8.1 查询全部(match_all)

bash 复制代码
curl -XPOST http://127.0.0.1:9200/test_index/_search \
-H "Content-Type: application/json" \
-d '{
  "query": {
    "match_all": {}
  }
}'

8.2 模糊分词查询(match)

适合:中文、文本、模糊搜索

bash 复制代码
curl -XPOST http://127.0.0.1:9200/test_index/_search \
-H "Content-Type: application/json" \
-d '{
  "query": {
    "match": {
      "name":"张"
    }
  }
}'

8.3 精准匹配(term)

适合:数字、状态、关键词、城市

bash 复制代码
curl -XPOST http://127.0.0.1:9200/test_index/_search \
-H "Content-Type: application/json" \
-d '{
  "query": {
    "term": {
      "age":25
    }
  }
}'

8.4 分页查询

bash 复制代码
curl -XPOST http://127.0.0.1:9200/test_index/_search \
-H "Content-Type: application/json" \
-d '{
  "query": {"match_all": {}},
  "from":0,
  "size":10
}'

8.5 排序查询

bash 复制代码
curl -XPOST http://127.0.0.1:9200/test_index/_search \
-H "Content-Type: application/json" \
-d '{
  "query": {"match_all": {}},
  "sort": [
    {"age": {"order":"desc"}}
  ]
}'

8.6 多条件并且(must)

bash 复制代码
curl -XPOST http://127.0.0.1:9200/test_index/_search \
-H "Content-Type: application/json" \
-d '{
  "query": {
    "bool": {
      "must": [
        {"term":{"age":30}},
        {"term":{"city":"上海"}}
      ]
    }
  }
}'

九、高级查询语法

9.1 范围查询 range

gt大于、lt小于、gte大于等于、lte小于等于

bash 复制代码
curl -XPOST http://127.0.0.1:9200/test_index/_search \
-H "Content-Type: application/json" \
-d '{
  "query": {
    "range": {
      "age":{
        "gte":20,
        "lte":40
      }
    }
  }
}'

9.2 短语精准匹配 match_phrase

不拆分分词,必须连续匹配

bash 复制代码
curl -XPOST http://127.0.0.1:9200/test_index/_search \
-H "Content-Type: application/json" \
-d '{
  "query": {
    "match_phrase": {
      "city":"北京朝阳"
    }
  }
}'

9.3 指定返回字段 _source

bash 复制代码
curl -XPOST http://127.0.0.1:9200/test_index/_search \
-H "Content-Type: application/json" \
-d '{
  "_source": ["name","age"],
  "query": {"match_all": {}}
}'

9.4 关键词高亮 highlight

bash 复制代码
curl -XPOST http://127.0.0.1:9200/test_index/_search \
-H "Content-Type: application/json" \
-d '{
  "query": {
    "match": {
      "name":"张"
    }
  },
  "highlight":{
    "fields":{"name":{}}
  }
}'

9.5 或条件 should

bash 复制代码
curl -XPOST http://127.0.0.1:9200/test_index/_search \
-H "Content-Type: application/json" \
-d '{
  "query": {
    "bool": {
      "should": [
        {"term":{"city":"上海"}},
        {"term":{"city":"北京"}}
      ]
    }
  }
}'

9.6 排除条件 must_not

bash 复制代码
curl -XPOST http://127.0.0.1:9200/test_index/_search \
-H "Content-Type: application/json" \
-d '{
  "query": {
    "bool": {
      "must_not": [
        {"term":{"city":"北京"}}
      ]
    }
  }
}'

9.7 过滤查询 filter(生产推荐)

不计算评分、性能更高

bash 复制代码
curl -XPOST http://127.0.0.1:9200/test_index/_search \
-H "Content-Type: application/json" \
-d '{
  "query": {
    "bool": {
      "filter": [
        {"range":{"age":{"gte":25}}}
      ]
    }
  }
}'

9.8 聚合分组 aggs

类比 group by,size:0 不显示原始数据

bash 复制代码
curl -XPOST http://127.0.0.1:9200/test_index/_search \
-H "Content-Type: application/json" \
-d '{
  "size":0,
  "aggs":{
    "city_group":{
      "terms":{"field":"city.keyword"}
    }
  }
}'

十、重要知识点

10.1 match 和 term 区别

  • match:分词、模糊、适合中文文本
  • term:精准、不分词、适合数字/固定关键词

10.2 bool 三大关键字

  • must = AND 同时满足
  • should = OR 满足其一
  • must_not = 不等于、排除

10.3 filter 和 query 区别

  • query:计算相关性评分,适合搜索
  • filter:不打分、缓存、速度快,适合筛选

十一、踩坑注意事项

  • CentOS7.4 不能安装ES8.x,最高只能 7.17.23
  • 不要浏览器打开ES地址,会URL解析报错,全部命令在Linux终端执行
  • ?v前面不能加空格,必须写成 ?v
  • 单机ES yellow黄色状态属于正常,不用处理
  • ES禁止root启动,系统自动创建elasticsearch用户
相关推荐
狒狒热知识8 小时前
AI赋能下企业新闻内容优化178软文网赋能权威资讯形成持续积累效应
大数据
盘古信息IMS9 小时前
盘古信息IMS V6 8.0重磅发布:以薪火AI数智平台点燃离散制造数智化引擎
大数据·人工智能·制造
论文小助手W68510 小时前
【ACM出版,EI检索】2026年人工智能与智慧城市国际学术会议(IC-AISC 2026)
大数据·人工智能·全文检索·智慧城市·交通物流
盖小雅10 小时前
自动化排班如何破解劳动法合规难题:从规则冲突到可追溯的排班表
大数据·运维·机器学习·自动化
Bechamz11 小时前
大数据开发学习Day43
大数据·学习
五度易链-区域产业数字化管理平台11 小时前
大数据驱动智慧招商:五度易链园区数字化解决方案
大数据
心疼你的一切12 小时前
高效内容生产:如何实现规模化创作
大数据·人工智能·ai·ai编程·ai写作
imbackneverdie13 小时前
深耕医学科研智能化十年,MedPeer打造新一代AI生物医学科研操作系统
大数据·人工智能·ai·信息可视化·数据分析·aigc·科研
Xuantong_9014 小时前
玄同科技亮相2026金砖新工业革命展览会,智启全球合作新篇
大数据·人工智能
机器学习之心15 小时前
上海原油期货收益率研究数据集说明
大数据·人工智能·上海原油期货收益率