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用户
相关推荐
Elasticsearch20 小时前
如何通过 Claude Code 来写入 CSV 数据到 Elasticsearch
elasticsearch
得物技术2 天前
从埋点需求到规则资产:Hermes Agent 重构得物数仓工作流
大数据·llm·ai编程
久美子2 天前
AI驱动数仓建设的Harness工程实践——本体建模、知识分层与上下文工程
大数据
大树883 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
大志哥1233 天前
ES和Logstash日志链路系统上线后遭遇切片爆炸(解决)
大数据·elasticsearch
果丁智能3 天前
物联网智能锁赋能集中式住宿:身份核验与远程权限管控的全链路技术实践
大数据·人工智能·物联网·智能家居
ApacheSeaTunnel3 天前
实战演示 | 基于 Apache SeaTunnel 与 Apache DolphinScheduler 实现 MySQL 到 Doris 离线定时增量同步
大数据·mysql·开源·doris·数据集成·seatunnel·数据同步
weixin_397574093 天前
PDF复杂表格的1:1还原引擎:跨页表格自动拼接技术实战
大数据·人工智能·pdf
TableRow3 天前
参数化搜索的实现原理:从多维索引到查询优化
elasticsearch·全文检索
极光代码工作室3 天前
基于数据仓库的电商数据分析平台
大数据·hadoop·python·spark·数据可视化