引言:上一节我们学习了ES的数据类型,有同学反馈说,里面的语句看不懂。今天,TeHero就为大家讲解ES索引和文档的CURD的操作。掌握了基本操作才能更好的系统学习,让我们开始吧!

1、索引的CURD

1)新增

# 创建索引名为 tehero_index 的索引
PUT /tehero_index?pretty
{
# 索引设置"settings": {"index": {"number_of_shards": 1, # 分片数量设置为1,默认为5"number_of_replicas": 1 # 副本数量设置为1,默认为1}},
# 映射配置"mappings": {"_doc": { # 类型名,强烈建议设置为 _doc"dynamic": false, # 动态映射配置
# 字段属性配置"properties": {"id": {"type": "integer"  # 表示字段id,类型为integer},"name": {"type": "text","analyzer": "ik_max_word", # 存储时的分词器"search_analyzer": "ik_smart"  # 查询时的分词器},"createAt": {"type": "date"}}}}
}

注:dynamic:是动态映射的开关,有3种状态:true 动态添加新的字段--缺省;推荐使用)false 忽略新的字段,不会添加字段映射,但是会存在于_source中;(strict 如果遇到新字段抛出异常;

# 返回值如下:
{"acknowledged": true, # 是否在集群中成功创建了索引"shards_acknowledged": true,"index": "tehero_index"
}

2)查询

GET /tehero_index  # 索引名,可以同时检索多个索引或所有索引
如:GET /*    GET /tehero_index,other_indexGET /_cat/indices?v  #查看所有 index

结果:

{"tehero_index": {"aliases": {},"mappings": {"_doc": {"dynamic": "false","properties": {"createAt": {"type": "date"},"id": {"type": "integer"},"name": {"type": "text","analyzer": "ik_max_word","search_analyzer": "ik_smart"}}}},"settings": {"index": {"creation_date": "1589271136921","number_of_shards": "1","number_of_replicas": "1","uuid": "xueDIxeUQnGBQTms65wA6Q","version": {"created": "6050499"},"provided_name": "tehero_index"}}}
}

3)修改

ES提供了一系列对index修改的语句,包括副本数量的修改、新增字段、refresh_interval值的修改、索引分析器的修改(后面重点讲解)、别名的修改(关于别名,TeHero后面会专门讲解,这是一个在实践中非常有用的操作)。

先学习常用的语法:

# 修改副本数
PUT /tehero_index/_settings
{"index" : {"number_of_replicas" : 2}
}# 修改分片刷新时间,默认为1s
PUT /tehero_index/_settings
{"index" : {"refresh_interval" : "2s"}
}# 新增字段 age
PUT /tehero_index/_mapping/_doc
{"properties": {"age": {"type": "integer"}}
}

更新完后,我们再次查看索引配置:

GET /tehero_index
结果:
{"tehero_index": {"aliases": {},"mappings": {"_doc": {"dynamic": "false","properties": {"age": {"type": "integer"},"createAt": {"type": "date"},"id": {"type": "integer"},"name": {"type": "text","analyzer": "ik_max_word","search_analyzer": "ik_smart"}}}},"settings": {"index": {"refresh_interval": "2s","number_of_shards": "1","provided_name": "tehero_index","creation_date": "1589271136921","number_of_replicas": "2","uuid": "xueDIxeUQnGBQTms65wA6Q","version": {"created": "6050499"}}}}
}
已经修改成功

4)删除

# 删除索引
DELETE /tehero_index
# 验证索引是否存在
HEAD tehero_index
返回:404 - Not Found

2、文档的CURD

1)新增

# 新增单条数据,并指定es的id 为 1
PUT /tehero_index/_doc/1?pretty
{"name": "Te Hero"
}
# 新增单条数据,使用ES自动生成id
POST /tehero_index/_doc?pretty
{"name": "Te Hero2"
}# 使用 op_type 属性,强制执行某种操作
PUT tehero_index/_doc/1?op_type=create
{"name": "Te Hero3"
}
注意:op_type=create强制执行时,若id已存在,ES会报“version_conflict_engine_exception”。
op_type 属性在实践中同步数据时是有用的,后面讲解数据库与ES的数据同步问题时,TeHero再为大家详细讲解。

我们查询数据,看下效果:GET /tehero_index/_doc/_search

{"took": 1,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": 2,"max_score": 1,"hits": [{"_index": "tehero_index","_type": "_doc","_id": "1","_score": 1,"_source": {"name": "Te Hero"}},{"_index": "tehero_index","_type": "_doc","_id": "P7-FCHIBJxE1TMY0WNGN","_score": 1,"_source": {"name": "Te Hero2"}}]}
}

2)修改

# 根据id,修改单条数据
(ps:修改语句和新增语句相同,可以理解为根据ID,存在则更新;不存在则新增)
PUT /tehero_index/_doc/1?pretty
{"name": "Te Hero-update"
}# 根据查询条件id=10,修改name="更新后的name"
(版本冲突而不会导致_update_by_query 中止)
POST tehero_index/_update_by_query
{"script": {"source": "ctx._source.name = params.name","lang": "painless","params":{"name":"更新后的name"}},"query": {"term": {"id": "10"}}
}

关于文档的更新,Update By Query API,对于该API的使用,TeHero将其归类为进阶知识,后续章节将为大家更深入的讲解。

3)查询

# 1、根据id,获取单个数据
GET /tehero_index/_doc/1
结果:
{"_index": "tehero_index","_type": "_doc","_id": "1","_version": 5,"found": true,"_source": {"name": "Te Hero-update","age": 18}
}# 2、获取索引下的所有数据
GET /tehero_index/_doc/_search
结果:
{"took": 1,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": 3,"max_score": 1,"hits": [{"_index": "tehero_index","_type": "_doc","_id": "P7-FCHIBJxE1TMY0WNGN","_score": 1,"_source": {"name": "Te Hero2"}},{"_index": "tehero_index","_type": "_doc","_id": "_update","_score": 1,"_source": {"name": "Te Hero3"}},{"_index": "tehero_index","_type": "_doc","_id": "1","_score": 1,"_source": {"name": "Te Hero-update","age": 18}}]}
}# 3、条件查询(下一节详细介绍)
GET /tehero_index/_doc/_search
{"query": {"match": {"name": "2"}}
}
结果:
{"took": 1,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": 1,"max_score": 0.9808292,"hits": [{"_index": "tehero_index","_type": "_doc","_id": "P7-FCHIBJxE1TMY0WNGN","_score": 0.9808292,"_source": {"name": "Te Hero2"}}]}
}

4)删除

# 1、根据id,删除单个数据
DELETE /tehero_index/_doc/1# 2、delete by query
POST tehero_index/_delete_by_query
{"query": { "match": {"name": "2"}}
}

3、批量操作 Bulk API

# 批量操作
POST _bulk
{ "index" : { "_index" : "tehero_test1", "_type" : "_doc", "_id" : "1" } }
{ "this_is_field1" : "this_is_index_value" }
{ "delete" : { "_index" : "tehero_test1", "_type" : "_doc", "_id" : "2" } }
{ "create" : { "_index" : "tehero_test1", "_type" : "_doc", "_id" : "3" } }
{ "this_is_field3" : "this_is_create_value" }
{ "update" : {"_id" : "1", "_type" : "_doc", "_index" : "tehero_test1"} }
{ "doc" : {"this_is_field2" : "this_is_update_value"} }# 查询所有数据
GET /tehero_test1/_doc/_search
结果:
{"took": 33,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 2,"max_score": 1,"hits": [{"_index": "tehero_test1","_type": "_doc","_id": "1","_score": 1,"_source": {"this_is_field1": "this_is_index_value","this_is_field2": "this_is_update_value"}},{"_index": "tehero_test1","_type": "_doc","_id": "3","_score": 1,"_source": {"this_is_field3": "this_is_create_value"}}]}
}

注:POST _bulk 都做了哪些操作呢?
1、若索引“tehero_test1”不存在,则创建一个名为“tehero_test1”的 index,同时若id = 1 的文档存在,则更新;不存在则插入一条 id=1 的文档;
2、删除 id=2 的文档;
3、插入 id=3 的文档;若文档已存在,则报异常;
4、更新 id = 1 的文档。

ps:批量操作在实践中使用是比较多的,因为减少了IO,提高了效率!

下节预告:倒排序索引是什么?

最后附上小编自己学习总结的ElasticSearch知识脑图,供大家参考:

elasticsearch 文档_ElasticSearch系列04:索引与文档的CURD相关推荐

  1. php elasticsearch 获取索引所有文档_Elasticsearch客户端主要方法的使用规则

    安装 1.在 composer.json 文件中引入 elasticsearch-php: { "require": { "elasticsearch/elasticse ...

  2. ElasticSearch入门系列(三)文档,索引,搜索和聚合

    一.文档 在实际使用中的对象往往拥有复杂的数据结构 Elasticsearch是面向文档的,这意味着他可以存储整个对象或文档,然而他不仅仅是存储,还会索引每个文档的内容使之可以被搜索,在Elastic ...

  3. Elasticsearch基本操作:索引、文档、搜索

    1.索引 在 Elasticsearch 中开始为数据建立索引之前要做的第一步操作是创建--我们的数据主要容器.这里的索引类似于 SQL 中的数据库概念.它是类型(相当于 SQL 中的表)和文档(相当 ...

  4. ElasticSearch什么是文档?索引一个文档

    什么是文档? 程序中大多的实体或对象能够被序列化为包含键值对的JSON对象,键(key)是字段(field)或属性(property)的名字,值(value)可以是字符串.数字.布尔类型.另一个对象. ...

  5. elasticSearch -- (文档,类型,索引)

    问题:大规模数据如何检索 当系统数据量达到10亿,100亿级别的时候,我们系统该如何去解决这种问题. 数据库选择-mysql, sybase,oracle,mongodb,hbase- 单点故障如何解 ...

  6. ElasticSearch中的集群、节点、索引、文档、类型是什么?

    群集:一个或多个节点(服务器)的集合,它们共同保存您的整个数据,并提供跨所有节点的联合索引和搜索功能.群集由唯一名称标识,默认情况下为"elasticsearch".此名称很重要, ...

  7. elasticsearch基础1——索引、文档

    用于复习快速回顾. 目录 1.初识弹性搜索elasticsearch 1.1.了解ES 1.1.1.elasticsearch的作用 1.1.2.ELK弹性栈 1.1.3.elasticsearch和 ...

  8. Elasticsearch 7.X索引、文档基本操作

    ElasticSearch是基于Lucene框架的全文搜索引擎,是文档型数据库,索引(Index)定义了文档的逻辑存储和字段类型,文档类型是文档的集合,文档以索引定义的逻辑存储模型. ElasticS ...

  9. ElasticSearch查询篇索引映射文档数据准备

    elasticsearch查询篇索引映射文档数据准备 我们后面要讲elasticsearch查询,先来准备下索引,映射以及文档: 我们先用Head插件建立索引film,然后建立映射 POST http ...

最新文章

  1. shell快速将同一名称的不同类型文件自动划分到一个文件中
  2. String和常量池
  3. autojs 按下状态_AutoJs4.1.0实战教程---最后惊喜的一篇
  4. 谈谈软件的开发及成长历程
  5. Linux学习-Linux 主机上的用户讯息传递
  6. “一切即代码”究竟意味着什么?
  7. java肝癌晚期_生信分析43.肿瘤浸润免疫与肝癌(HCCDB+oncomine)
  8. python爬淘宝评论源代码_一篇文章教会你用Python爬取淘宝评论数据(写在记事本)...
  9. 计算机病毒是指______.,计算机病毒是指
  10. 通过温度和湿度计算露点函数
  11. 小米路由器3 mysql_小米路由器3 opkg安装
  12. 自动化运维工具inception+archer
  13. 产品的设计与分析---用户体验五要素
  14. vc6.0程序界面实现XP风格方法
  15. E1/CE1/T1/PRI/BRI知识介绍和配置
  16. 操作系统期末考试简答题汇总(全、附带答案)
  17. A7139 无线通信驱动(STM32) 添加FIFO扩展模式,能够发送超大数据包
  18. Linux-字符界面操作基础
  19. proteus8.0安装教程
  20. 动物模型/ 心血管疾病模型 / 啮齿动物高血压模型-自发性高血压大鼠(SHR)

热门文章

  1. 盾神与砝码称重java_[蓝桥杯][算法提高VIP]盾神与砝码称重-题解(Java代码)
  2. 转正答辩ppt_如何顺利完成转正答辩?
  3. python全局变量的声明和使用_python自学篇(第三章:函数)
  4. sql视图 权限_SQLmysql用户权限管理
  5. su组件在什么窗口_草图大师SketchUp(SU)快捷键
  6. java coroutine类_Coroutines和Rxjava异步编程对比
  7. Java中六种List集合循环遍历取值
  8. mybatis 遍历数组_Mybatis中别名、插件与数据源配置
  9. ai音响怎么连接网络_KTV音响设备怎么连接?点歌机怎么连接?学习下
  10. 如何通过MongoDB自带的Explain功能提高检索性能?