03-Elasticsearch-基本语法
查询[ES]
查询ES信息
GET /
查询集群健康状态
GET /_cluster/health
增删改索引
创建索引并指定主分片和副本数
PUT /my_doc { "settings": { "number_of_shards": 1, "number_of_replicas": 0 } }
创建索引并指定映射
PUT /index_mappings { "mappings": { "properties": { # 字段名称 "realname":{ # 字段类型 "type": "text", # 是否被索引, 默认为true "index": true }, "username":{ "type": "keyword", "index": false } } } }
修改索引Mappings
不支持
增加索引Mappings字段
POST /index_mappings/_mapping { "properties": { "id": { "type": "long" }, "age": { "type": "integer" } } }
自动设置Mappings
新增index
PUT /my_doc { "settings": { "number_of_shards": 1, "number_of_replicas": 0 } }
新增索引记录
# 可以指定ID, 也可以不指定 POST /my_doc/_doc/ { "id": 1001, "name": "imooc-1", "desc": "imoocisverygood,慕课网非常牛!", "create_date": "2019-12-24" }
新增记录有如果index没有mapping就会自动映射
修改索引记录
# 增量修改 POST /my_doc/_update/Fxu1NYMBTxHwTTCGbOwO { "doc":{ "name":"我是慕课网2" } } # 全量修改 PUT /my_doc/_doc/Fxu1NYMBTxHwTTCGbOwO { "name":"我是delete" }
基于乐观锁修改记录
# 查询数据 GET /my_doc/_doc/zBu3NYMBTxHwTTCGLewF # 返回结果 { "_index" : "my_doc", "_type" : "_doc", "_id" : "zBu3NYMBTxHwTTCGLewF", "_version" : 1, "_seq_no" : 2, "_primary_term" : 1, "found" : true, "_source" : { "id" : 1003, "name" : "imooc-3", "desc" : "imooc is niubility, 慕课网很好很强大!", "create_date" : "2019-12-26" } } # 基于版本修改 正确的版本 POST /my_doc/_update/zBu3NYMBTxHwTTCGLewF?if_seq_no=2&if_primary_term=1 { "doc":{ "name":"imooc-31" } } # 返回结果 修改成功 版本升级 { "_index" : "my_doc", "_type" : "_doc", "_id" : "zBu3NYMBTxHwTTCGLewF", "_version" : 2, "result" : "updated", "_shards" : { "total" : 1, "successful" : 1, "failed" : 0 }, "_seq_no" : 13, "_primary_term" : 2 } # 基于版本修改 错误的版本 POST /my_doc/_update/zBu3NYMBTxHwTTCGLewF?if_seq_no=2&if_primary_term=2 { "doc":{ "name":"imooc-31" } } # 返回结果 修改失败 版本不一致 { "error": { "root_cause": [ { "type": "version_conflict_engine_exception", "reason": "[zBu3NYMBTxHwTTCGLewF]: version conflict, required seqNo [2], primary term [2]. current document has seqNo [13] and primary term [2]", "index_uuid": "OQtxdWrTQRCVsOZFKLrPqA", "shard": "0", "index": "my_doc" } ], "type": "version_conflict_engine_exception", "reason": "[zBu3NYMBTxHwTTCGLewF]: version conflict, required seqNo [2], primary term [2]. current document has seqNo [13] and primary term [2]", "index_uuid": "OQtxdWrTQRCVsOZFKLrPqA", "shard": "0", "index": "my_doc" }, "status": 409 }
删除索引记录
# 根据_id删除记录, 只是逻辑删除, 后续ES会自动清理
DELETE /my_doc/_doc/{_id}
删除索引
DELETE /索引名称
字段类型汇总
查询索引
索引管理里面查看
查询索引信息
GET /索引名称
查询索引列表
# 不携带列头 GET /_cat/indices # 携带列头 GET /_cat/indices?v
索引字段分词
GET /index_mappings/_analyze { "field": "realname", "text": ["imooc is very good!"] }
在增加索引时, 字段index
为TRUE时才会被分词
根据ID查询记录
# 根据_id查询记录
GET /my_doc/_doc/{_id}
查询全部记录
GET /my_doc/_search
查询记录是否存在
HEAD /my_doc/_doc/{_id}
分类:
ES
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)