Elasticsearch从入门到精通
一、准备es环境1.1 准备java环境[rootlocalhost home]# java -versionopenjdk version17.0.192026-04-21 OpenJDK Runtime Environment Temurin-17.0.1910(build 17.0.1910)OpenJDK 64-Bit Server VM Temurin-17.0.1910(build 17.0.1910,mixed mode,sharing)1.2准备docker环境[rootlocalhost home]# docker -vDocker version 1.13.1,build 7d71120/1.13.1[rootlocalhost home]# systemctl status docker● docker.service-Docker Application Container Engine Loaded: loaded(/usr/lib/systemd/system/docker.service;enabled;vendor preset: disabled)Drop-In:/usr/lib/systemd/system/docker.service.d └─flannel.conf Active: active(running)since 一 2026-07-06 22:36:35 GMT;10min ago Docs: http://docs.docker.comProcess: 2858 ExecStartPost/usr/sbin/iptables-P FORWARD ACCEPT(codeexited,status0/SUCCESS)Main PID: 2651(dockerd-current)Memory: 339.5M CGroup:/system.slice/docker.service ├─2651/usr/bin/dockerd-current--add-runtimedocker-runc/usr/libexec/docker/docker-runc-current--default-runtimedocker-runc--exec-opt...└─2684/usr/bin/docker-containerd-current-l unix:///var/run/docker/libcontainerd/docker-containerd.sock--metrics-interval0--start-time...7月 06 22:36:34 localhost.localdomain dockerd-current[2651]: time2026-07-06T22:36:34.727168587Zlevelinfo msgDefault bridge (docker0) is...dress7月 06 22:36:34 localhost.localdomain dockerd-current[2651]: time2026-07-06T22:36:34.781338123Zlevelinfo msgLoading containers: done.7月 06 22:36:34 localhost.localdomain dockerd-current[2651]: time2026-07-06T22:36:34.993557184Zlevelinfo msgDaemon has completed initialization7月 06 22:36:34 localhost.localdomain dockerd-current[2651]: time2026-07-06T22:36:34.993585831Zlevelinfo msgDocker daemoncommit7d71...1.13.1 7月 06 22:36:35 localhost.localdomain dockerd-current[2651]: time2026-07-06T22:36:35.029542279Z levelinfo msgAPI listen on/var/run/docker.sock 7月 06 22:36:35 localhost.localdomain systemd[1]: Started Docker Application Container Engine. 7月 06 22:36:51 localhost.localdomain dockerd-current[2651]: time2026-07-06T22:36:51.988907540Z levelerror msgAttempting next endpoint f...tials 7月 06 22:36:52 localhost.localdomain dockerd-current[2651]: time2026-07-06T22:36:52.536542193Z levelerror msgNot continuing with pull a...found 7月 06 22:37:03 localhost.localdomain dockerd-current[2651]: time2026-07-06T22:37:03.698694337Z levelerror msgAttempting next endpoint f...tials 7月 06 22:37:04 localhost.localdomain dockerd-current[2651]: time2026-07-06T22:37:04.075951037Z levelerror msgNot continuing with pull a...found Hint: Some lines were ellipsized,use-l to show in full.二、部署es2.1下载es镜像docker pull docker.elastic.co/elasticsearch/elasticsearch:7.17.272.2启动镜像持久化启动推荐数据不丢失 bash# 1. 创建数据目录mkdir-p/mydata/es7/data# 2. 启动容器挂载数据目录docker run-d \--name es7 \-p 9200:9200 \-p 9300:9300 \-ediscovery.typesingle-node\-eES_JAVA_OPTS-Xms512m -Xmx512m\-v/mydata/es7/data:/usr/share/elasticsearch/data\ docker.elastic.co/elasticsearch/elasticsearch:7.17.27验证是否启动成功 bash# 查看容器状态dockerps|grep es7# 测试ES是否可访问curl http://localhost:9200# 查看启动日志如果有问题docker logs es7 如果 curl 返回了版本信息就说明ES已经正常运行了。sudo sysctl-w vm.max_map_count262144 docker restart es7roote82214f8f545:/usr/share/elasticsearch# curl http://0.0.0.0:9200curl http://localhost:9200{name:e82214f8f545,cluster_name:docker-cluster,cluster_uuid:hjznxttiRDO5Rb_9vSo4Kg,version:{number:7.17.27,build_flavor:default,build_type:docker,build_hash:0f88dde84795b30ca0d2c0c4796643ec5938aeb5,build_date:2025-01-09T14:09:01.578835424Z,build_snapshot: false,lucene_version:8.11.3,minimum_wire_compatibility_version:6.8.0,minimum_index_compatibility_version:6.0.0-beta1},tagline:You Know, for Search}roote82214f8f545:/usr/share/elasticsearch#三、创建索引写入数据roote82214f8f545:/usr/share/elasticsearch# curl -X GET http://localhost:9200/test/_search?pretty{took: 14,timed_out: false,_shards:{total: 1,successful: 1,skipped: 0,failed: 0},hits:{total:{value: 4,relation:eq},max_score: 1.0,hits:[{_index:test,_type:_doc,_id:1,_score: 1.0,_source:{id:001,name:张三,age: 25,create_time:2026-07-06T10:00:00}},{_index:test,_type:_doc,_id:2,_score: 1.0,_source:{id:002,name:李四,age: 30,create_time:2026-07-06T10:05:00}},{_index:test,_type:_doc,_id:3,_score: 1.0,_source:{id:003,name:王五,age: 28,create_time:2026-07-06T10:10:00}},{_index:test,_type:_doc,_id:4,_score: 1.0,_source:{id:004,name:赵六,age: 35,create_time:2026-07-06T10:15:00}}]}}roote82214f8f545:/usr/share/elasticsearch#四、使用reindex备份索引1.创建备份目标索引test_backup bash curl-X PUThttp://localhost:9200/test_backup-HContent-Type: application/json-d{settings:{number_of_shards:1,number_of_replicas:1},mappings:{properties:{id:{type:keyword},name:{type:text},age:{type:integer},create_time:{type:date}}}}2.执行 Reindex将 test 的数据复制到 test_backup bash curl-X POSThttp://localhost:9200/_reindex-HContent-Type: application/json-d{source:{index:test},dest:{index:test_backup}} 返回结果会显示 json{took:50,total:4,updated:0,created:4,failures:[]}3.验证备份是否成功 bash# 查询备份索引数据curl-X GEThttp://localhost:9200/test_backup/_search?pretty# 对比两个索引的文档数量curl-X GEThttp://localhost:9200/_cat/count/test?vcurl-X GEThttp://localhost:9200/_cat/count/test_backup?v五、常用运维命令汇总# 查看所有索引curl-X GEThttp://localhost:9200/_cat/indices?v# 查看索引映射curl-X GEThttp://localhost:9200/test/_mapping?pretty# 查看索引设置curl-X GEThttp://localhost:9200/test/_settings?pretty# 删除索引谨慎操作curl-X DELETEhttp://localhost:9200/test_backup# 清空索引数据保留结构和设置curl-X POSThttp://localhost:9200/test/_delete_by_query?conflictsproceed-HContent-Type: application/json-d { query: {match_all: {}} }# 1. 创建索引curl-X PUThttp://localhost:9200/test-HContent-Type: application/json-d{settings:{number_of_shards:1,number_of_replicas:1},mappings:{properties:{id:{type:keyword},name:{type:text},age:{type:integer},create_time:{type:date}}}}# 2. 写入数据curl-X POSThttp://localhost:9200/test/_doc/1-HContent-Type: application/json-d{id:001,name:张三,age:25,create_time:2026-07-06T10:00:00}curl-X POSThttp://localhost:9200/test/_doc/2-HContent-Type: application/json-d{id:002,name:李四,age:30,create_time:2026-07-06T10:05:00}curl-X POSThttp://localhost:9200/test/_doc/3-HContent-Type: application/json-d{id:003,name:王五,age:28,create_time:2026-07-06T10:10:00}# 3. 创建备份索引curl-X PUThttp://localhost:9200/test_backup-HContent-Type: application/json-d{settings:{number_of_shards:1,number_of_replicas:1},mappings:{properties:{id:{type:keyword},name:{type:text},age:{type:integer},create_time:{type:date}}}}# 4. 执行reindex备份curl-X POSThttp://localhost:9200/_reindex-HContent-Type: application/json-d{source:{index:test},dest:{index:test_backup}}# 5. 验证结果echo test索引文档数 curl-shttp://localhost:9200/_cat/count/test?v \echo test_backup索引文档数 curl-shttp://localhost:9200/_cat/count/test_backup?v \echo test_backup数据样例 curl-shttp://localhost:9200/test_backup/_search?size2pretty