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用户
相关推荐
珠海西格电力1 小时前
零碳园区数据应用的具体场景有哪些?
大数据·人工智能·算法·架构·能源
Ganttable1 小时前
项目基线项目使用指南
大数据
阿乔外贸日记2 小时前
意大利进口主力产品及主要合作供应国
大数据·人工智能·物联网·搜索引擎·云计算
TTBIGDATA2 小时前
【Ambari Plus】15.Livy 安装
大数据·运维·hadoop·ambari·hdp·cdh·bigtop
YangYang9YangYan2 小时前
2026仓库文员学数据分析的价值
大数据
EMBA寰球网3 小时前
互动展厅设计核心逻辑、实施路径与落地实施要点专业解析:名瑞展览展陈行业实践深度洞察
大数据·人工智能
lin9902123 小时前
内容矩阵批量分发实战
大数据·人工智能·矩阵
我登哥MVP4 小时前
Hadoop成长史-从Nutch子项目到大数据生态王者
java·大数据·hadoop·分布式·云原生·云计算
dunge20264 小时前
# GPT 与 Codex:软件正在从“静态程序”演化为“持续推理系统”
大数据·gpt
2301_780303904 小时前
DevSecOps建设之自动化集成与部署 Jenkins教程和使用案例
运维·自动化·jenkins