Quick Reference 中的 Elasticsearch 操作实战:从安装启动到索引、Mapping 与文档查询的完整速查 Quick Reference 中的 Elasticsearch 操作实战从安装启动到索引、Mapping 与文档查询的完整速查【免费下载链接】reference面向开发者的技术速查清单Cheat Sheets集合整理常见技术、工具与开发流程帮助快速查阅关键信息提高开发效率。项目地址: https://gitcode.com/GitHub_Trending/referen/reference本文基于速查清单仓库Quick Reference中的 Elasticsearch 备忘清单 展开系统梳理 Elasticsearch 的下载安装与启动验证、HTTP 请求基础语法、索引/Mapping/文档的增删改查全套操作以及 DSL 与 curl 两种等价写法。读完本文你可以脱离 GUI 客户端仅用curl命令行完成索引创建、字段映射维护、条件/范围/分页查询和按条件批量删除等日常运维与开发任务并在遇到细节差异时对照本仓库清单原文快速核对。Elasticsearch 是什么Elasticsearch 是一个基于 Lucene 库构建的搜索引擎提供分布式、多租户的全文搜索能力通过 HTTP Web 接口操作使用无模式schema-free的 JSON 文档。本清单收录于 Quick Reference 仓库 的「数据库」分类下与 MySQL、PostgreSQL、Redis 等并列其品牌标识见 assets/elasticsearch.svg。清单原文给出的定位非常务实它是围绕「命令行可复制即用」整理的一份备忘所有操作均给出 DSLHTTP 语法与curl两种形态因此本文也遵循这一结构——先给出 DSL再给出等价的 curl 命令方便直接粘贴执行。下载安装${VERSION}占位符与三大平台清单中所有下载链接都使用${VERSION}作为版本占位符执行前必须替换为你要安装的具体版本号。原文特别提示官方包有的功能只能试用完整功能需要付费请仔细阅读官网文档。这是使用 Elastic 官方发行版的重要前提不同订阅层级Basic/Default 等决定了哪些功能如安全、监控、部分 X-Pack 能力可用。Windowshttps://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${VERSION}-windows-x86_64.zipLinuxLinux 侧清单给出的流程是「下载 → 下载校验文件 → 校验 → 解压 → 进入目录」校验步骤可以防止下载不完整或包被篡改$ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${VERSION}-linux-x86_64.tar.gz $ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${VERSION}-linux-x86_64.tar.gz.sha512 $ shasum -a 512 -c elasticsearch-${VERSION}-linux-x86_64.tar.gz.sha512 $ tar -xzf elasticsearch-${VERSION}-linux-x86_64.tar.gz $ cd elasticsearch-${VERSION}/macOSmacOS 侧等价流程用curl下载并把.sha512校验文件通过管道直接交给shasum校验$ curl -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${VERSION}-darwin-x86_64.tar.gz $ curl https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${VERSION}-darwin-x86_64.tar.gz.sha512 | shasum -a 512 -c - $ tar -xzf elasticsearch-${VERSION}-darwin-x86_64.tar.gz $ cd elasticsearch-${VERSION}/启动与启动验证清单将启动流程拆成三步启动进程、设置密码、验证连通性。启动 Elasticsearch在解压目录内执行$ ./bin/elasticsearch设置密码用于后续带认证的请求export ELASTIC_PASSWORDyour_password测试是否启动成功——注意请求走的是https并通过--cacert指定证书、-u携带elastic内置用户凭证curl --cacert $ES_HOME/config/certs/http_ca.crt -u elastic:$ELASTIC_PASSWORD https://localhost:9200启动成功后返回的 JSON 样例如下name、cluster_uuid、build_hash、build_date等字段会随实例不同而变化{ name : Cp8oag6, cluster_name : elasticsearch, cluster_uuid : AT69_T_DTp-1qgIJlatQqA, version : { number : ${VERSION}, build_type : tar, build_hash : f27399d, build_flavor : default, build_date : 2016-03-30T09:51:41.449Z, build_snapshot : false, lucene_version : 9.10.0, minimum_wire_compatibility_version : 1.2.3, minimum_index_compatibility_version : 1.2.3 }, tagline : You Know, for Search }从返回结构中可以看出几个关键字段cluster_name/cluster_uuid标识集群version.build_type表明这是 tar 包安装与前面「解压运行」的安装方式呼应lucene_version反映其底层的 Lucene 版本minimum_wire_compatibility_version与minimum_index_compatibility_version则说明该节点可与哪些旧版本节点/索引互通对做版本升级或集群滚动更新时有参考价值。Elasticsearch 与 RDBMS 的对比清单中给出一张概念映射表帮助你用关系型数据库的思维快速对齐 Elasticsearch 的数据模型RDBMSElasticsearch数据库 (database)索引 (index)表 (table)类型 (type)行 (row)文档 (document)列 (column)字段 (field)表结构映射 (mapping)索引全文索引SQL查询 DSLSELECT * FROM tablenameGET http://...UPDATE table SETPUT http://...DELETEDELETE http://...需要留意的是表中「类型 (type)」对应 RDBMS 的「表」是历史概念现代版本的 Elasticsearch 每个索引内实际只支持单一文档类型映射直接挂在索引级本文后续所有示例均采用索引级写法如PUT /user_info的mappings.properties。HTTP 基础语法规则Elasticsearch 的所有操作本质都是 HTTP 请求清单给出了统一模板$ curl -XVERB PROTOCOL://HOST:PORT/PATH?QUERY_STRING -d BODY各部分含义VERBHTTP 方法可选GET、POST、PUT、HEAD、DELETEPROTOCOLhttp或https只有在 Elasticsearch 前面有 https 代理的时候可用HOST集群中任何一个节点的主机名本地即localhostPORTElasticsearch HTTP 服务端口默认为9200PATHAPI 路径例如_count返回集群中文档数量路径可包含多个组件例如_cluster/stats、_nodes/stats/jvmQUERY_STRING可选查询参数例如?pretty使返回的 JSON 更美观易读BODYJSON 格式请求主体如果请求需要的话。从 curl 备忘清单 中也能看到这套语法在实战中的延伸写法例如把_cluster/nodes之类的响应通过python -m json.tool做格式化与本清单的?pretty参数殊途同归。索引操作创建索引统一的 API 前缀http://localhost:9200/DSL 写法以user_info索引为例1 主分片、1 副本并给出完整字段映射PUT /user_info { settings: { number_of_replicas: 1, number_of_shards: 1 }, mappings: { properties: { id: { type: long, index: true }, username: { type: keyword, index: true }, nickname: { type: keyword, index: true }, password: { type: keyword, index: false }, age: { type: integer, index: true }, info: { type: text, index: true }, remark: { type: text, index: true } } } }等价 curl 命令curl -XPUT http://localhost:9200/user_info -H Content-Type: application/json -d{ settings: { number_of_replicas: 1, number_of_shards: 1 }, mappings: { properties: { id: { type: long, index: true }, username: { type: keyword, index: true }, nickname: { type: keyword, index: true }, password: { type: keyword, index: false }, age: { type: integer, index: true }, info: { type: text, index: true }, remark: { type: text, index: true } } } }参数说明settings索引级设置number_of_shards每个索引的主分片数。索引一旦创建后无法修改此配置因此建索引前应根据数据量预估number_of_replicas每个主分片的副本数可随时修改用于在写入吞吐与查询可用性/吞吐之间权衡mappings索引映射定义properties字段定义键为字段名值中type指定字段类型。从示例映射本身可以读出几处设计取舍值得注意username/nickname用keyword不分词、适合精确匹配与聚合info/remark用text分词、适合全文检索而password设为index: false——即该字段可被存储与返回但不建全文索引避免密码值被分词进倒排索引属于合理的敏感字段处理方式。清单同时注明「其他参数很多请参考官网资料」即本清单只固化最常用的核心参数。删除索引DELETE /user_infocurl -XDELETE http://localhost:9200/user_info判断索引是否存在用HEAD请求返回头而不返回主体适合脚本判断状态码# 查看索引是否存在 HEAD /user_info# 查看索引是否存在 curl -XHEAD http://localhost:9200/user_info开启 / 关闭索引关闭索引常用于维护、迁移或临时停止写入的场景开启则恢复服务POST /user_info/_opencurl -XPOST http://localhost:9200/user_info/_openPOST /user_info/_closecurl -XPOST http://localhost:9200/user_info/_close索引别名别名让「逻辑名」与「物理索引名」解耦是滚动更新索引建新索引 → 建别名 → 原子切换的常用手段。添加别名POST /user_info/_alias/user1curl -XPOST http://localhost:9200/user_info/_alias/user1删除别名DELETE /user_info/_alias/user1curl -XDELETE http://localhost:9200/user_info/_alias/user1查看别名DSL 写的是GET /_alias/user1curl -XGET http://localhost:9200/_alias/useraa核对提示清单原文中「查看别名」的 DSL 与 curl 示例里的别名不一致DSL 为user1curl 为useraa实际执行时请统一为当前真实存在的别名。Mapping 操作类似修改数据库中列的操作查看 mappingGET /user_info/_mappingcurl -XGET http://localhost:9200/user_info/_mapping新增 mapping给已有索引追加新字段sexPUT /user_info/_mapping { properties:{ sex:{ type:keyword } } }curl -XPUT http://localhost:9200/user_info/_mapping -H Content-Type: application/json -d{ properties:{ sex:{ type:keyword } } }注意字段映射只能增加不能更改或删除已有字段类型变更通常需要重建索引并迁移数据。这条约束在原文中以显式「注意」标出是 Mapping 维护中最重要的规则之一。文档操作添加文档指定_doc/1即写入文档 ID 为1的文档POST /user_info/_doc/1 { id:1, username:username, password:123456, nickname:nickname, age:18, info:一些个人相关的介绍, remark:备注信息, sex:男 }curl -XPOST http://localhost:9200/user_info/_doc/1 -H Content-Type: application/json -d{ id:1, username:username, password:123456, nickname:nickname, age:18, info:一些个人相关的介绍, remark:备注信息, sex:男 }查询索引下所有文档类似select * from user_info;GET /user_info/_search { query: { match_all: {} } }curl -XGET http://localhost:9200/user_info/_search -H Content-Type: application/json -d{ query: { match_all: {} } }通过 id 查询文档类似select * from user_info where id 1;GET /user_info/_doc/1curl -XGET http://localhost:9200/user_info/_doc/1模糊查找全文匹配类似select * from user_info where info like %人%;。match会对text字段分词后匹配GET /user_info/_search { query: { match: { info: 人 } } }通过条件查询文档term 精确匹配类似select * from user_info where username username;。term针对keyword类型做不分词精确匹配GET /user_info/_search { query: { bool: { must: [ { term: { username: username } } ] } } }curl -XGET http://localhost:9200/user_info/_search -H Content-Type: application/json -d{ query: { bool: { must: [ { term: { username: username } } ] } } }范围查找类似select * from user_info where age between 18 and 30;注意gt/lt为开区间严格大于/小于GET /user_info/_search { query: { range: { age: { gt: 18, lt: 30 } } } }curl -XGET http://localhost:9200/user_info/_search -H Content-Type: application/json -d{ query: { range: { age: { gt: 18, lt: 30 } } } }and 查询bool.must类似select * from user_info where age 18 and sex 男;。bool查询的must子句要求所有条件都满足GET /user_info/_search { query: { bool: { must: [ { range: { age: { gt: 18 } } }, { term: { sex: 男 } } ] } } }curl -XGET http://localhost:9200/user_info/_search -H Content-Type: application/json -d{ query: { bool: { must: [ { range: { age: { gt: 17 } } }, { term: { sex: 男 } } ] } } }核对提示原文此处 DSL 与 curl 的阈值不一致DSL 为gt: 18curl 为gt: 17执行时请以 DSL 为准或按需统一。or 查询bool.should类似select * from user_info where age 18 or sex 男;。should表达「满足其一」GET /user_info/_search { query: { bool: { should: [ { range: { age: { gt: 18 } } }, { term: { sex: 男 } } ] } } }curl -XGET http://localhost:9200/user_info/_search -H Content-Type: application/json -d{ query: { bool: { should: [ { range: { age: { gt: 18 } } }, { term: { sex: 男 } } ] } } }limit 查找控制返回条数类似select * from user_info limit 10;用size控制返回条数GET /user_info/_search { size: 10, query: { match_all: {} } }curl -XGET http://localhost:9200/user_info/_search -H Content-Type: application/json -d{ size: 1, query: { match_all: {} } }原文 DSL 为size: 10curl 示例为size: 1两者语义相同按实际需要取值。limit offset 查找分页类似select * from user_info limit 0,10;用fromsize组合实现偏移分页GET /user_info/_search { size: 2, from: 1, query: { match_all: {} } }curl -XGET http://localhost:9200/user_info/_search -H Content-Type: application/json -d{ size: 2, from: 1, query: { match_all: {} } }参数说明原文参数解释与示例数值不同这里按原文语义整理size想要返回的结果数量原文示例语义为 10 条from从结果集第几条开始返回偏移从 0 开始例如from: 1即跳过第 1 条、从第 2 条开始query: {match_all: {}}无特定条件、仅取分页结果时的匹配所有文档查询。实践提醒from size深度分页在大数据量下开销较高生产环境深度翻页可考虑滚动或搜索上下文方案此处清单聚焦的是基础分页语义。删除文档删除指定 id类似delete from user_info where id 3;# 删除文档 DELETE /user_info/_doc/3# 删除文档 curl -XDELETE http://localhost:9200/user_info/_doc/3按条件删除delete_by_query类似delete from user_info where age 18;。这是清单「删除文档」部分的关键补充Elasticsearch 不支持直接写DELETE WHERE条件删除统一走_delete_by_queryAPIPOST /user_info/_delete_by_query { query: { range: { age: { gt: 18 } } } }curl -XPOST http://localhost:9200/user_info/_delete_by_query -H Content-Type: application/json -d{query:{range:{age:{gt:18}}}}_delete_by_query复用的是与_search相同的查询 DSL因此前面学过的bool、range、term、match等组合都可以直接套用于条件删除这也是清单以「查询 DSL 即删除条件」方式组织删除章节的原因。小结与延伸本文完整继承了 docs/elasticsearch.md 的结构概念定位 → 三平台下载安装含${VERSION}占位符与 sha512 校验→ 启动与带认证验证 → RDBMS 对照表 → HTTP 语法模板 → 索引/别名/Mapping/文档全量操作 → 条件删除。使用时记住三条主线一切皆 HTTPVERB host:9200/path?pretty美化输出HEAD探测存在性不可变项要前置规划number_of_shards建后不可改、mapping 只能增不能删改这两点决定了索引设计的评审必须发生在创建之前查询 DSL 与删除 DSL 同源_delete_by_query与_search共用查询语法学会一套查询即可覆盖「查 条件删」两大场景。如需更完整的参数与 API 细节清单原文明确建议参考 Elasticsearch 官方文档同时本仓库 README 的「数据库」分类还收录了 MySQL、MongoDB、Redis、Neo4j、SQLite 等姊妹速查表配合 curl 清单一起使用可以快速搭建一套纯命令行的数据存储运维手册。【免费下载链接】reference面向开发者的技术速查清单Cheat Sheets集合整理常见技术、工具与开发流程帮助快速查阅关键信息提高开发效率。项目地址: https://gitcode.com/GitHub_Trending/referen/reference创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考