elasticsearch的文档管理

ops/2024/11/28 11:27:31/

2 json数据入门

json数据类型
基础数据类型
  • 字符串,只要用双引号包裹的都是字符串类型。“嘻嘻”,“文勇”,“2024”

  • 数字型,2024,3.1415926

  • 布尔型,true 和 false

  • 空值,null

高级数据类型
  • 数组,

    • 扁平化写法:[“文勇”,“zhiyong”,true,2024,null]

    • 漂亮输出写法:

      ["文勇","zhiyong",true,2024,null
      ]
      
  • 对象:

    • 扁平化写法:{“name”: “韩雯童”,“hobby”: [“睡觉”,“美女”,“上课”]}

    • 漂亮输出:

      {"name": "韩雯童","hobby": ["睡觉","美女","上课"]
      }
      
json的高级数据类型嵌套
{"school": "zhiyong18","class": "zhiyong","student": [{"name": "马蔷","hobby": ["打台球","看美女"]},{"name": "孙县伟","hobby": ["打游戏","唱跳rap","篮球"]}]
}
{"school": "zhiyong18",  // "school" 是一个字符串"class": "zhiyong",     // "class" 是一个字符串"student": [            // "student" 是一个数组{"name": "马蔷",      // 第一个学生的名字"hobby": [          // 第一个学生的兴趣爱好是一个数组"打台球",          // 第一个兴趣爱好"看美女"           // 第二个兴趣爱好]},{"name": "孙县伟",    // 第二个学生的名字"hobby": [          // 第二个学生的兴趣爱好是一个数组"打游戏",          // 第一个兴趣爱好"唱跳rap",         // 第二个兴趣爱好"篮球"             // 第三个兴趣爱好]}]
}

文档的管理

文档的创建
文档基本创建

1.文档的创建(推荐的方式),提示:若文档不存在则默认会创建一条索引

POST 10.0.0.92:9200/first-document/doc{"name": "韩雯童","hobby": ["睡觉","美女","上课"]
}

返回结果为201

{"_index": "first-document","_type": "doc","_id": "h4EVK5MBQU0FP-mFMNTI","_version": 1,"result": "created","_shards": {"total": 2,"successful": 1,"failed": 0},"_seq_no": 0,"_primary_term": 1
}
往索引写入数据时指定文档id

说明:建议不要指定文档ID,在不知道文档ID的情况下可能导致ID冲突,

POST 10.0.0.92:9200/first-document/doc/202407101700002{"school": "wzy666","class": "wzyclass","student": [{"name": "马蔷","hobby": ["打台球","看美女"]},{"name": "孙县伟","hobby": ["打游戏","唱跳rap","篮球"]}]
}
文档的批量创建

批量创建文档的好处:

  • 减少网络传输,直接减少TCP握手次数

1.批量创建3条文档,注意:json数据最后一行必须是空行

POST 10.0.0.91:9200/_bulk{ "index" : { "_index" : "first-document"} }
{"name": "韩小童","hobby": ["睡觉","美女","上课"]}
{ "index" : { "_index" : "first-document"} }
{"name": "王鹏鹏","hobby": ["钓鱼","摸泥鳅","打扑克"]}
{ "index" : { "_index" : "first-document"} }
{"name": "黄峰锋","hobby": ["cosplay二次元","喝枸杞","吃羊腰"]}
文档内容的查看
查看所有的文档

方式:GET 10.0.0.91:9200/索引名称/_search

输出:

{"took": 8,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 4,"relation": "eq"},"max_score": 1.0,"hits": [{"_index": "first-document","_type": "doc","_id": "h4EVK5MBQU0FP-mFMNTI","_score": 1.0,"_source": {"name": "韩雯童","hobby": ["睡觉","美女","上课"]}},{"_index": "first-document","_type": "doc","_id": "202407101700001","_score": 1.0,"_source": {"name": "二狗","hobby": ["吃喝","钓鱼","划水"]}},{"_index": "first-document","_type": "doc","_id": "iYEoK5MBQU0FP-mFJNSF","_score": 1.0,"_source": {"name": "马超","hobby": ["骑马","打猎","逛公园"]}},{"_index": "first-document","_type": "doc","_id": "123456789123","_score": 1.0,"_source": {"name": "1马超","hobby": ["1马","1猎","1公园"]}}]}
}
查看指定的文档

不带索引的全部查看

方式:GET 10.0.0.91:9200/索引名称/文档ID

GET 10.0.0.93:9200/first-document/doc/123456789123

输出结果:

{"_index": "first-document","_type": "doc","_id": "123456789123","_version": 1,"_seq_no": 3,"_primary_term": 1,"found": true,"_source": {"name": "1马超","hobby": ["1马","1猎","1公园"]}
}
DSL语句模糊查询数据

需求:查找包含 的数据

POST 10.0.0.93:9200/first-document/_search{"query": {"match": {"name": "韩"}}
}

查询结果:

{"took": 47,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 1,"relation": "eq"},"max_score": 1.1129161,"hits": [{"_index": "first-document","_type": "doc","_id": "h4EVK5MBQU0FP-mFMNTI","_score": 1.1129161,"_source": {"name": "韩雯童","hobby": ["睡觉","美女","上课"]}}]}
}
文档的批量查看

根据索引名称和多个文档ID查询数据

POST 10.0.0.93:9200/_mget{"docs": [{"_index": "first-document","_id": "202407101700001"},{"_index": "first-document","_id": "dxxbK5MBDakapW2yA3oo"}]
}

输出结果:

{"docs": [{"_index": "first-document","_type": "doc","_id": "202407101700001","_version": 1,"_seq_no": 1,"_primary_term": 1,"found": true,"_source": {"name": "二狗","hobby": ["吃喝","钓鱼","划水"]}},{"_index": "first-document","_type": "doc","_id": "dxxbK5MBDakapW2yA3oo","_version": 1,"_seq_no": 8,"_primary_term": 1,"found": true,"_source": {"name": "黄峰锋","hobby": ["cosplay二次元","喝枸杞","吃羊腰"]}}]
}
文档内容的修改
文档的单个修改

1.把文档id为123456789中的name有1马超改为一马超

POST 10.0.0.93:9200/zhiyong18-zhiyong/doc/202407101700001/_update{"doc":{"name" : "一马超"}
}

输出结果:

{"_index": "first-document","_type": "doc","_id": "123456789123","_version": 2,"result": "updated","_shards": {"total": 2,"successful": 2,"failed": 0},"_seq_no": 4,"_primary_term": 1
}

2.查询数据验证是否修改

GET 10.0.0.93:9200/first-document/doc/123456789123
{"_index": "first-document","_type": "doc","_id": "123456789123","_version": 2,"_seq_no": 4,"_primary_term": 1,"found": true,"_source": {"name": "一马超","hobby": ["1马","1猎","1公园"]}
}
文档的批量修改

一次操作2个文档。换行提示

POST 10.0.0.93:9200/_bulk{ "update" : { "_index" : "first-document","_id" : "202407101700001"} }
{ "doc" : {"age" : 10, "hobby": ["洗脚脚MoreUpdate","按摩Moreupdate","上二楼Moreupdate"]} }
{ "update" : { "_index" : "first-document","_id" : "dxxbK5MBDakapW2yA3oo"} }
{ "doc" : {"age" : 12, "hobby": ["批量修改后的hobby","批量修改后的age","MoreUpdate"]} }
文档的删除
单条文档的删除

1.删除文档first-document中id号为123456789123的文档

DELETE 10.0.0.93:9200/first-document/doc/123456789123

执行结果:

{"_index": "first-document","_type": "doc","_id": "123456789123","_version": 3,"result": "deleted","_shards": {"total": 2,"successful": 2,"failed": 0},"_seq_no": 5,"_primary_term": 1
}

2.再次查看这条文档就查不到了

查询结果:

{"_index": "first-document","_type": "doc","_id": "123456789123","found": false
}

批量删除

POST 10.0.0.93:9200/_bulk

{ "delete" : { "_index" : "classroom06", "_id" : "tn0XnZABmNKdkfhcgdw7" } }
{ "delete" : { "_index" : "classroom06", "_id" : "tH0XnZABmNKdkfhcgdw6" } }
# 空行
多个文档的删除

根据2个文档ID,删除这2个文档。注意底部换行

POST 10.0.0.92:9200/_bulk{ "delete" : { "_index" : "first-document", "_id" : "202407101700001" } }
{ "delete" : { "_index" : "first-document", "_id" : "dxxbK5MBDakapW2yA3oo" } }
DSL语句简单上手

1.写入30条关于笔记本的数据

POST 10.0.0.91:9200/_bulk{ "index" : { "_index" : "notebook" } }
{ "brand": "火影", "price": 3999, "color": "黑色", "memory": "16G", "storage": "512G" }
{ "index" : { "_index" : "notebook" } }
{ "brand": "机械革命", "price": 4999, "color": "白色", "memory": "32G", "storage": "1T" }
{ "index" : { "_index" : "notebook" } }
{ "brand": "华硕", "price": 8999, "color": "灰色", "memory": "64G", "storage": "512G" }
{ "index" : { "_index" : "notebook" } }
{ "brand": "惠普", "price": 3999, "color": "白色", "memory": "16G", "storage": "1T" }
{ "index" : { "_index" : "notebook" } }
{ "brand": "机械革命", "price": 8999, "color": "黑色", "memory": "32G", "storage": "1T" }
{ "index" : { "_index" : "notebook" } }
{ "brand": "火影", "price": 4999, "color": "灰色", "memory": "16G", "storage": "512G" }
{ "index" : { "_index" : "notebook" } }
{ "brand": "华硕", "price": 3999, "color": "黑色", "memory": "64G", "storage": "1T" }
{ "index" : { "_index" : "notebook" } }
{ "brand": "惠普", "price": 4999, "color": "白色", "memory": "32G", "storage": "512G" }
{ "index" : { "_index" : "notebook" } }
{ "brand": "机械革命", "price": 3999, "color": "黑色", "memory": "16G", "storage": "512G" }
{ "index" : { "_index" : "notebook" } }
{ "brand": "火影", "price": 8999, "color": "白色", "memory": "64G", "storage": "1T" }
{ "index" : { "_index" : "notebook" } }
{ "brand": "华硕", "price": 4999, "color": "灰色", "memory": "32G", "storage": "512G" }
{ "index" : { "_index" : "notebook" } }
{ "brand": "惠普", "price": 3999, "color": "灰色", "memory": "16G", "storage": "1T" }
{ "index" : { "_index" : "notebook" } }
{ "brand": "机械革命", "price": 8999, "color": "黑色", "memory": "64G", "storage": "512G" }
{ "index" : { "_index" : "notebook" } }
{ "brand": "火影", "price": 4999, "color": "白色", "memory": "32G", "storage": "512G" }
{ "index" : { "_index" : "notebook" } }
{ "brand": "华硕", "price": 3999, "color": "灰色", "memory": "16G", "storage": "512G" }
{ "index" : { "_index" : "notebook" } }
{ "brand": "惠普", "price": 4999, "color": "黑色", "memory": "32G", "storage": "1T" }
{ "index" : { "_index" : "notebook" } }
{ "brand": "机械革命", "price": 3999, "color": "灰色", "memory": "16G", "storage": "512G" }
{ "index" : { "_index" : "notebook" } }
{ "brand": "火影", "price": 8999, "color": "黑色", "memory": "64G", "storage": "1T" }
{ "index" : { "_index" : "notebook" } }
{ "brand": "华硕", "price": 4999, "color": "白色", "memory": "32G", "storage": "512G" }
{ "index" : { "_index" : "notebook" } }
{ "brand": "惠普", "price": 8999, "color": "灰色", "memory": "64G", "storage": "512G" }
{ "index" : { "_index" : "notebook" } }
{ "brand": "机械革命", "price": 3999, "color": "白色", "memory": "16G", "storage": "1T" }
{ "index" : { "_index" : "notebook" } }
{ "brand": "火影", "price": 4999, "color": "黑色", "memory": "32G", "storage": "1T" }
{ "index" : { "_index" : "notebook" } }
{ "brand": "华硕", "price": 8999, "color": "黑色", "memory": "64G", "storage": "1T" }
{ "index" : { "_index" : "notebook" } }
{ "brand": "惠普", "price": 3999, "color": "白色", "memory": "16G", "storage": "512G" }
{ "index" : { "_index" : "notebook" } }
{ "brand": "机械革命", "price": 4999, "color": "灰色", "memory": "32G", "storage": "1T" }
{ "index" : { "_index" : "notebook" } }
{ "brand": "火影", "price": 8999, "color": "白色", "memory": "64G", "storage": "512G" }
{ "index" : { "_index" : "notebook" } }
{ "brand": "华硕", "price": 4999, "color": "黑色", "memory": "32G", "storage": "1T" }
{ "index" : { "_index" : "notebook" } }
{ "brand": "惠普", "price": 8999, "color": "黑色", "memory": "64G", "storage": "512G" }

2.根据筛选条件内存32G,查询出最贵的笔记本是哪个

GET 10.0.0.91:9200/notebook/_search{"query": {"match": {"memory": "32G"}},"sort": {"price": {"order": "desc"}},"_source": ["brand","price","color","memory","storage"],"from": "0","size": 1
}
  • query,匹配内存32G
  • sort desc,按价格降降序
  • _source,显示源数据
  • from,截断输出,只显示1个

3.输出结果为

{"took": 9,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 10,"relation": "eq"},"max_score": null,"hits": [{"_index": "notebook","_type": "_doc","_id": "kIGZK5MBQU0FP-mF5NRt","_score": null,"_source": {"memory": "32G","color": "黑色","price": 8999,"storage": "1T","brand": "机械革命"},"sort": [8999]}]}
}

可以发现DSL的查询语句写起来还是比较痛苦的

**补充:**最便宜购物商品的怎么查询?换成升序即可

GET 10.0.0.93:9200/shopping/_search
{"query": {"match": {"group": 1}},"sort": {"price": {"order": "asc"}},"_source": ["title","price","producer","type","group","item"],"from": "0","size": 1
}

http://www.ppmy.cn/ops/137338.html

相关文章

文件的处理(c语言)

首先了解下文件的作用 文件可以把数据直接放在电脑的硬盘上,实现了数据的持久化 什么是文件 文件就是磁盘上的文件。在程序设计中,文件通常有俩种,一种是程序文件,另一种是数据文件(这是从文件功能来分类的&#xff…

设计模式——解释器模式

定义 解释器模式是一种行为设计模式,它给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子。在这种模式中,通常会将一个复杂的表达式(如数学表达式、规则表达…

k8s删除网络组件错误

k8s集群删除calico网络组件重新部署flannel网络组件,再部署pod后出现报错不能分配ip地址 plugin type"calico" failed (add): error getting ClusterInformation: connection is unauthorized: Unauthorized 出现该问题是因为删除网络组件后,网…

工业网络安全 智能电网,SCADA和其他工业控制系统等关键基础设施的网络安全(总结)

1、工业网络的安全势必是未来安全方向必须要做的一个重要的方面 工业网络的概念:简单的说就是控制控制系统的网络,其可以进行基于网络的数字通信。 关键的基础设施:包括直接操作任何系统的设施 了解工业网络的安全势必要了解工业网络使用的通…

数据库中的视图

数据库中的视图 什么是视图创建视图使⽤视图修改数据注意事项 删除视图视图的优点 什么是视图 视图是⼀个虚拟的表,它是基于⼀个或多个基本表或其他视图的查询结果集。视图本⾝不存储数 据,⽽是通过执⾏查询来动态⽣成数据。⽤户可以像操作普通表⼀样使…

Swift——自动引用计数ARC

ARC ARC是swift使用的一种管理应用程序内存的机制,对于C语言我们知道,当我们申请一块空间,通常需要手动释放,不然会造成空间浪费,而有了ARC机制,你无需考虑内存的管理,因为ARC会在类的实例不再…

医学机器学习:数据预处理、超参数调优与模型比较的实用分析

摘要 本文介绍了医学中的机器学习,重点阐述了数据预处理、超参数调优和模型比较的技术。在数据预处理方面,包括数据收集与整理、处理缺失值、特征工程等内容,以确保数据质量和可用性。超参数调优对模型性能至关重要,介绍了多种调…

深度学习中的梯度下降算法:详解与实践

梯度下降算法是深度学习领域最基础也是最重要的优化算法之一。它驱动着从简单的线性回归到复杂的深度神经网络模型的训练和优化。作为深度学习的核心工具,梯度下降提供了调整模型参数的方法,使得预测的结果逐步逼近真实值。本文将从梯度下降的基本原理出…