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用户
相关推荐
好赞科技2 小时前
2026年五大精选微信小程序,革新效率体验提升智能生活品质
大数据·微信小程序
Gofarlic_oms12 小时前
Adams许可排队严重?不想买新许可,闲置回收立即可用
java·大数据·服务器·开发语言·人工智能
财经资讯数据_灵砚智能2 小时前
基于全球经济类多源新闻的NLP情感分析与数据可视化(日间)2026年5月8日
大数据·人工智能·python·信息可视化·自然语言处理
乐迪信息2 小时前
乐迪信息:AI防爆摄像机,船舶偏航逆行算法实时告警零漏检
大数据·人工智能·物联网·算法·机器学习·计算机视觉·目标跟踪
FONE_Platform3 小时前
FONE大健康行业全面预算解决方案:重塑全链路敏捷预算体系
大数据·人工智能·区块链·全面预算
jiayi_19993 小时前
[github] 上传本地repo
大数据·elasticsearch·github
科技小花3 小时前
AI原生 vs 云原生:数据治理平台选型如何匹配你的数据中台架构?
大数据·数据库·人工智能·数据治理·数据中台
xcbrand3 小时前
新零售品牌策划公司有哪些
大数据·人工智能·python·零售
跨境小彭3 小时前
Temu 批量下架工具推荐|合规安全,支持 SPU/ID 批量导入
大数据·人工智能·安全·跨境电商·temu