当前位置: 首页 > news >正文

elasticsearch学习(五)文档CRUD

目录

  • 上一篇文章
  • 文档CRUD
    • 创建索引
    • 查看索引字段mapping
    • 更新mapping
    • 删除索引
    • 测试索引是否存在
    • 查看索引字段结构
    • 删除索引
  • 文档命令
    • 创建文档
      • Index
      • Create
        • Create与Index的区别
    • 更新文档
    • 删除文档
    • 查询文档
      • 简单查询语法

上一篇文章

elasticsearch学习(四)kibana安装
https://blog.csdn.net/github_36801273/article/details/151076329?spm=1011.2124.3001.6209

文档CRUD

创建索引

PUT /my-index
{"mappings": {"properties": {"testFieldA": {"type": "text","fields": {"keyword": {"type": "keyword","ignore_above": 256}}},"testFieldB": {"type": "text","fields": {"keyword": {"type": "keyword","ignore_above": 256}}}}}
}

mapping的部分不是必须的,可以通过先创建索引,然后写入数据的方式自动生成mapping

查看索引字段mapping

GET /my-index/_mapping{"my-index": {"mappings": {"properties": {"testFieldA": {"type": "text","fields": {"keyword": {"type": "keyword","ignore_above": 256}}},"testFieldB": {"type": "text","fields": {"keyword": {"type": "keyword","ignore_above": 256}}}}}}
}

更新mapping

POST  /my-index/_mapping
{"properties": {"testFieldC": {"type": "text","fields": {"keyword": {"type": "keyword","ignore_above": 256}}},"testFieldB": {"type": "text","fields": {"keyword": {"type": "keyword","ignore_above": 512}}}}
}

只能新增和更新字段,不能删除字段,删除字段用reindex

删除索引

DELETE /my-index

测试索引是否存在

HEAD /my-index

查看索引字段结构

GET /my-index/_mapping

删除索引

DELETE /my-index

文档命令

创建文档

Index

如果ID不存在,则创建新的,否则删除旧的再创建新的,增加版本号

POST /my-index/_doc
{"id": "park_rocky-mountain","title": "Rocky Mountain","description": "Bisected north to south by the Continental Divide, this portion of the Rockies has ecosystems varying from over 150 riparian lakes to montane and subalpine forests to treeless alpine tundra."
}

Create

// 不指定ID(系统自动生成)
POST /my-index/_doc
{"testFieldA":"testA1","testFieldB":"testB1"
}// 指定ID
POST /my-index/_doc/2
{"testFieldA":"testA2","testFieldB":"testB2"
}
Create与Index的区别

create的文档ID如果已存在,会报失败
index的文档ID不存在,会新建,否则会把原来的删除了再增加

更新文档

POST /my-index/_update/2
{"doc":{"testFieldA":"updateTestA2"} 
}

删除文档

DELETE /my-index/_doc/2

查询文档

支持两种方式:
1 url查询,在url路径中使用查询参数
2 用DSL(Query Domain Specific Language)

简单查询语法

api作用
/_search查询集群上所有索引
/{index_name}/_search查询指定索引
/{index_name1},{index_name2},{…}/_search查询多个指定索引
/{index_prex}*/_search查询以{index_prex}为开头的索引
// 指定ID查询
GET /my-index/_doc/2// 查询全量数据
GET /my-index/_search// 查询my开头的索引数据
GET /my*/_search// URL查询
GET /my-index/_search?q=testFieldA:updateTestA2// DSL查询
GET /my-index/_search -H 'Content-Type:application/json' -d
{"query":{"match":{"testFieldA":"updateTestA2"}}
}
http://www.dtcms.com/a/366826.html

相关文章:

  • 前端跨域终极指南:3 种优雅解决方案 + 可运行 Demo
  • App UI 自动化环境搭建指南
  • Java Stream 流式操作举例
  • QT Creator 使用
  • 【一文了解】C#泛型
  • 数据库集成:使用 SQLite 与 Electron
  • 新电脑硬盘如何分区?3个必知技巧避免“空间浪费症”!
  • [技术革命]Harmonizer:仅20MB模型如何实现8K图像_视频的完美和谐化?
  • 鸿蒙:AppStorageV2状态管理和数据共享
  • 泛型的通配符
  • axios请求缓存与重复拦截:“相同请求未完成时,不发起新请求”
  • TDengine TIMETRUNCATE 函数用户使用手册
  • 野火STM32Modbus主机读取寄存器/线圈失败(三)-尝试将存贮事件的地方改成数组(非必要解决方案)(附源码)
  • 腾讯云国际代理:如何在腾讯云GPU服务器上部署私有化大模型?附GPU简介
  • SQLmap 完整使用指南:环境搭建 + 命令详解 + 实操案例
  • 打开 solidworks当前文件 所在的文件夹 python pywin32
  • Effective Python 第10条 - 用赋值表达式减少重复代码
  • 上位机知识篇---conda run
  • KingbaseES一体化架构与多层防护体系如何保障企业级数据库的持续稳定与弹性扩展
  • 关于在自然语言处理深层语义分析中引入公理化体系的可行性、挑战与前沿展望
  • 谁才是企业级开源平台的优选?OpenCSG与Dify、Coze、Langflow、Ollama 的差异化之路
  • 深度学习——ResNet 卷积神经网络
  • 高并发商城 商品为了防止超卖,都做了哪些努力?
  • 2025国赛C题保姆级教程思路分析 NIPT 的时点选择与胎儿的异常判定
  • Spring Cloud Alibaba快速入门01
  • C语言结构体:轻松管理球员数据
  • SpringMVC的异常处理和拦截器
  • 【C语言】深入理解指针(4)
  • nextcyber——常见应用攻击
  • 一个老工程师的“新生”:良策金宝AI,让我重新爱上设计