elasticsearch的文档管理

news2024/11/26 2:19:18

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.coloradmin.cn/o/2247591.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Python 获取微博用户信息及作品(完整版)

在当今的社交媒体时代,微博作为一个热门的社交平台,蕴含着海量的用户信息和丰富多样的内容。今天,我将带大家深入了解一段 Python 代码,它能够帮助我们获取微博用户的基本信息以及下载其微博中的相关素材,比如图片等。…

vue面试题——描述一下vue

目录 1 vue是什么2 Vue的核心特性2.1 数据驱动(MVVM)2.2 组件化2.3 指令系统 3 Vue跟传统开发的区别 1 vue是什么 简单点说,vue就是一个用于创建用户界面的JavaScript框架,同时也是一个创建单页面应用的Web应用框架,Vu…

Large Spatial Model:End-to-end Unposed Images to Semantic 3D 论文解读

目录 一、概述 二、相关工作 1、SfM和可微神经表示 2、端到端的Image-to-3D 三、LSM 1、密集几何预测 2、2D信息特征提取 3、点特征融合 4、可微渲染 5、损失函数 四、实验 一、概述 该论文提出一种大型空间模型(Larget Spatial Model,LSM)…

A045-基于spring boot的个人博客系统的设计与实现

🙊作者简介:在校研究生,拥有计算机专业的研究生开发团队,分享技术代码帮助学生学习,独立完成自己的网站项目。 代码可以查看文章末尾⬇️联系方式获取,记得注明来意哦~🌹 赠送计算机毕业设计600…

VMware17安装之VMware Workstation Pro 16升级到17详细教程

VMware17安装之VMware Workstation Pro 16升级到17详细教程 一、下载安装包二、开始安装三、升级成功 当前使用的是VMware Workstation 16 Pro版本,想用最新的17,但是又不想卸载原来的,所以想尝试下看看能不能直接升级,最终升级成…

nature communications论文 解读

题目《Transfer learning with graph neural networks for improved molecular property prediction in the multi-fidelity setting》 这篇文章主要讨论了如何在多保真数据环境(multi-fidelity setting)下,利用图神经网络(GNNs&…

接口上传视频和oss直传视频到阿里云组件

接口视频上传 <template><div class"component-upload-video"><el-uploadclass"avatar-uploader":action"uploadImgUrl":on-progress"uploadVideoProcess":on-success"handleUploadSuccess":limit"lim…

深度学习图像视觉 RKNN Toolkit2 部署 RK3588S边缘端 过程全记录

深度学习图像视觉 RKNN Toolkit2 部署 RK3588S边缘端 过程全记录 认识RKNN Toolkit2 工程文件学习路线&#xff1a; Anaconda Miniconda安装.condarc 文件配置镜像源自定义conda虚拟环境路径创建Conda虚拟环境 本地训练环境本地转换环境安装 RKNN-Toolkit2&#xff1a;添加 lin…

07-SpringCloud-Gateway新一代网关

一、概述 1、Gateway介绍 官网&#xff1a;https://spring.io/projects/spring-cloud-gateway Spring Cloud Gateway组件的核心是一系列的过滤器&#xff0c;通过这些过滤器可以将客户端发送的请求转发(路由)到对应的微服务。 Spring Cloud Gateway是加在整个微服务最前沿的防…

美创科技入选2024数字政府解决方案提供商TOP100!

11月19日&#xff0c;国内专业咨询机构DBC德本咨询发布“2024数字政府解决方案提供商TOP100”榜单。美创科技凭借在政府数据安全领域多年的项目经验、技术优势与创新能力&#xff0c;入选收录。 作为专业数据安全产品与服务提供商&#xff0c;美创科技一直致力于为政府、金融、…

Java编程,配置mongoUri连接mongodb时,需对特殊字符进行转义

一、背景 java程序连接mongo有两种方式&#xff1a; 用户名和密码方式uri方式 1、用户名和密码 以用户数据库为例&#xff0c;注意看它的密码 spring:data:mongodb:host: 192.168.10.17database: db_user_serviceport: 3717username: user_servicepassword: user_service3…

MySQL底层概述—1.InnoDB内存结构

大纲 1.InnoDB引擎架构 2.Buffer Pool 3.Page管理机制之Page页分类 4.Page管理机制之Page页管理 5.Change Buffer 6.Log Buffer 1.InnoDB引擎架构 (1)InnoDB引擎架构图 (2)InnoDB内存结构 (1)InnoDB引擎架构图 下面是InnoDB引擎架构图&#xff0c;主要分为内存结构和磁…

【Github】如何使用Git将本地项目上传到Github

【Github】如何使用Git将本地项目上传到Github 写在最前面1. 注册Github账号2. 安装Git工具配置用户名和邮箱仅为当前项目配置&#xff08;可选&#xff09; 3. 创建Github仓库4. 获取仓库地址5. 本地操作&#xff08;1&#xff09;进入项目文件夹&#xff08;2&#xff09;克隆…

大事件管理系统项目总结(上)

文章目录 大事件管理系统项目总结&#xff08;上&#xff09;Pinia - 配置仓库统一管理Vue3路由配置Vue3导航拦截 大事件管理系统项目总结&#xff08;上&#xff09; Pinia - 配置仓库统一管理 使用pinia多层文件夹嵌套时&#xff0c;导入某个文件的路径会很长&#xff0c;容…

鸿蒙征文|鸿蒙心路旅程:始于杭研所集训营,升华于横店

始于杭研所 在2024年7月&#xff0c;我踏上了一段全新的旅程&#xff0c;前往风景如画的杭州&#xff0c;参加华为杭研所举办的鲲鹏&昇腾集训营。这是一个专门为开发者设计的培训项目&#xff0c;中途深入学习HarmonyOS相关技术。对于我这样一个对技术充满热情的学生来说&…

flowable流程图详细绘制教程

文章目录 前言一、flowable是什么&#xff1f;回答下之前的问题 二、flowable-modeler使用1. 使用步骤2.开始绘制弄一个请假的流程 三 加载该流程总结 前言 flowable有些晦涩难懂的东西&#xff1a; 我最开始接触的时候,还是用的activity,当时觉得好复杂,那么这次经过我自己在…

【Linux 篇】Docker 的容器之海与镜像之岛:于 Linux 系统内探索容器化的奇妙航行

文章目录&#xff1a; 【Linux 篇】Docker 的容器之海与镜像之岛&#xff1a;于 Linux 系统内探索容器化的奇妙航行前言安装docker-centos7 【Linux 篇】Docker 的容器之海与镜像之岛&#xff1a;于 Linux 系统内探索容器化的奇妙航行 &#x1f4ac;欢迎交流&#xff1a;在学习…

linux从0到1——shell编程9

声明&#xff01; 学习视频来自B站up主 **泷羽sec** 有兴趣的师傅可以关注一下&#xff0c;如涉及侵权马上删除文章&#xff0c;笔记只是方便各位师傅的学习和探讨&#xff0c;文章所提到的网站以及内容&#xff0c;只做学习交流&#xff0c;其他均与本人以及泷羽sec团队无关&a…

go项目中比较好的实践方案

工作两年来&#xff0c;我并未遇到太大的挑战&#xff0c;也没有特别值得夸耀的项目。尽管如此&#xff0c;在日常的杂项工作中&#xff0c;我积累了不少心得&#xff0c;许多实践方法也在思考中逐渐得到优化。因此&#xff0c;我在这里记录下这些心得。 转发与封装 这个需求…

Maven的安装——给Idea配置Maven

一、什么是Maven? Maven是一个开源的项目管理工具&#xff0c;它主要用于Java项目的构建、依赖管理和项目生命周期管理。 二、准备环境 maven安装之前&#xff0c;我们要先安装jdk&#xff0c;确保你已经安装了jdk环境。可以通过【win】【r】打开任务管理器&#xff0c;输入…