elasticsearch的文档管理

devtools/2024/11/26 23:15:27/

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/devtools/137243.html

相关文章

彻底理解Redis的过期策略

一.由来 因为现在Redis的使用是非常普遍的,Redis是基于内存的数据存储系统,而内存相比于早些年而言,还是便宜了很多,但是相比硬盘而言还是最很多,因此提高Redis里面的内存利用率是非常重要,而Redis的过期策…

淘宝商品评论爬虫:Java版“窃听风云”

在这个信息爆炸的时代,淘宝评论就像是隐藏在商品页面背后的小秘密,等待着我们去发掘。今天,我们将化身为“数据侦探”,使用Java编写一个爬虫程序,潜入淘宝的评论区,窃取那些珍贵的用户反馈。请注意&#xf…

R语言处理JSON文件

R语言处理JSON文件 引言 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成。它基于JavaScript编程语言的一个子集,但JSON是独立于语言的文本格式&#xff0c…

C 语言面向对象

面向对象的基本特性:封装,继承,多态 1.0 面向过程概念 当我们在编写程序时,通常采用以下步骤: 1. 将问题的解法分解成若干步骤 2. 使用函数分别实现这些步骤 3. 依次调用这些函数 这种编程风格的被称作 面向过程…

Python 网络爬虫操作指南

网络爬虫是自动化获取互联网上信息的一种工具。它广泛应用于数据采集、分析以及实现信息聚合等众多领域。本文将为你提供一个完整的Python网络爬虫操作指南,帮助你从零开始学习并实现简单的网络爬虫。我们将涵盖基本的爬虫概念、Python环境配置、常用库介绍。 上传…

解决登录Google账号遇到手机上Google账号无法验证的问题

文章目录 场景小插曲解决方案总结 场景 Google账号在新的设备上登录的时候,会要求在手机的Google上进行确认验证,而如果没有安装Google play就可能出现像我一样没有任何弹框,无法实现验证 小插曲 去年,我在笔记本上登录了Googl…

Flink Standalone集群模式安装部署全攻略

Flink Standalone集群模式安装部署全攻略 一、引言 Flink作为一款强大的分布式流处理和批处理框架,在大数据领域有着广泛的应用。本文将详细介绍Flink Standalone集群模式的安装部署过程,帮助大家快速搭建起开发测试环境。 二、安装前准备 首先&…

电话机器人的发展历程

电话机器人的发展历程 作者:开源呼叫中心系统 FreeIPCC,Github地址:https://github.com/lihaiya/freeipcc 电话机器人的发展历程可以大致分为以下几个阶段: 一、初级阶段:互动式语音应答(IVR)…