elasticsearch的文档管理

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
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/922605.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

IT服务团队建设与管理

在 IT 服务团队中,需要明确各种角色。例如系统管理员负责服务器和网络设备的维护与管理;软件工程师专注于软件的开发、测试和维护;运维工程师则保障系统的稳定运行,包括监控、故障排除等。通过清晰地定义每个角色的职责&#xff0…

初学 flutter 问题记录

windows搭建flutter运行环境 一、运行 flutter doctor遇到的问题 Xcmdline-tools component is missingRun path/to/sdkmanager --install "cmdline-tools;latest"See https://developer.android.com/studio/command-line for more details.1)cmdline-to…

神经网络(系统性学习二):单层神经网络(感知机)

此前篇章: 神经网络中常用的激活函数 神经网络(系统性学习一):入门篇 单层神经网络(又叫感知机) 单层网络是最简单的全连接神经网络,它仅有输入层和输出层,没有隐藏层。即&#x…

H.265流媒体播放器EasyPlayer.js播放器提示MSE不支持H.265解码可能的原因

随着人工智能和机器学习技术的应用,流媒体播放器将变得更加智能,能够根据用户行为和偏好提供个性化的内容推荐。总体而言,流媒体播放器的未来发展将更加注重技术创新和用户互动,以适应不断变化的市场需求和技术进步。 提示MSE不支…

MySQL原理简介—6.简单的生产优化案例

大纲 1.MySQL日志的顺序写和数据文件的随机读指标 2.Linux存储系统软件层原理及IO调度优化原理 3.数据库服务器使用的RAID存储架构介绍 4.数据库Too many connections故障定位 1.MySQL日志的顺序写和数据文件的随机读指标 (1)磁盘随机读操作 (2)磁盘顺序写操作 (1)磁盘随…

svn 崩溃、 cleanup失败 怎么办

在使用svn的过程中,可能出现整个svn崩溃, 例如cleanup 失败的情况,类似于 这时可以下载本贴资源文件并解压。 或者直接访问网站 SQLite Download Page 进行下载 解压后得到 sqlite3.exe 放到发生问题的svn根目录的.svn路径下 右键呼出pow…

前后端分离,解决vue+axios跨域和proxyTable不生效等问题

看到我这篇文章前可能你以前看过很多类似的文章。至少我是这样的,因为一直没有很好的解决问题。 正文 当我们通过webstorm等IDE开发工具启动项目的时候,通过命令控制台可以观察到启动项目的命令 如下: webpack-dev-server --inline --prog…

在win10环境部署opengauss数据库(包含各种可能遇到的问题解决)

适用于windows环境下通过docker desktop实现opengauss部署,请审题。 文章目录 前言一、部署适合deskdocker的环境二、安装opengauss数据库1.配置docker镜像源2.拉取镜像源 总结 前言 注意事项:后面docker拉取镜像源最好电脑有科学上网工具如果没有科学上…

Java开发经验——Spring Test 常见错误

摘要 本文详细介绍了Java开发中Spring Test的常见错误和解决方案。文章首先概述了Spring中进行单元测试的多种方法,包括使用JUnit和Spring Boot Test进行集成测试,以及Mockito进行单元测试。接着,文章分析了Spring资源文件扫描不到的问题&am…

2024年亚太地区数学建模大赛D题-探索量子加速人工智能的前沿领域

量子计算在解决复杂问题和处理大规模数据集方面具有巨大的潜力,远远超过了经典计算机的能力。当与人工智能(AI)集成时,量子计算可以带来革命性的突破。它的并行处理能力能够在更短的时间内解决更复杂的问题,这对优化和…

基于 RBF 神经网络整定的 PID 控制

基于 RBF 神经网络整定的 PID 控制 是结合了传统 PID 控制和 RBF(径向基函数)神经网络的自适应控制方法。在这种方法中,RBF 神经网络用于自适应地调整 PID 控制器的增益(比例增益 KpK_pKp​,积分增益 KiK_iKi​ 和微分…

空间注意力网络的性能优化与多维评估

在本文中,首先分析空间注意力网络(Spatial Attention Neural Network)在五个不同数据集上的训练结果。这些数据集包括Daily_and_Sports_Activities、WISDM、UCI-HAR、PAMAP2和OPPORTUNITY。通过对比这些结果,我们可以深入理解空间…

Linux——1_系统的延迟任务及定时任务

系统的延迟任务及定时任务 在系统中我们的维护工作大多数时在服务器行对闲置时进行 我们需要用延迟任务来解决自动进行的一次性的维护 延迟任务时一次性的,不会重复执行 当延迟任务产生输出后,这些输出会以邮件的形式发送给延迟任务发起者 在RHEL9中…

【数据结构】—— 线索二叉树

引入 我们现在提倡节约型杜会, 一切都应该节约为本。对待我们的程序当然也不例外,能不浪费的时间或空间,都应该考虑节省。我们再观察团下图的二叉树(链式存储结构),会发现指针域并不是都充分的利用了,有许…

NVR管理平台EasyNVR多个NVR同时管理:全方位安防监控视频融合云平台方案

EasyNVR是基于端-边-云一体化架构的安防监控视频融合云平台,具有简单轻量的部署方式与多样的功能,支持多种协议(如GB28181、RTSP、Onvif、RTMP)和设备类型(IPC、NVR等),提供视频直播、录像、回放…

虚幻引擎---初识篇

一、学习途径 虚幻引擎官方文档:https://dev.epicgames.com/documentation/zh-cn/unreal-engine/unreal-engine-5-5-documentation虚幻引擎在线学习平台:https://dev.epicgames.com/community/unreal-engine/learning哔哩哔哩:https://www.b…

汽车HiL测试:利用TS-GNSS模拟器掌握硬件性能的仿真艺术

一、汽车HiL测试的概念 硬件在环(Hardware-in-the-Loop,简称HiL)仿真测试,是模型基于设计(Model-Based Design,简称MBD)验证流程中的一个关键环节。该步骤至关重要,因为它整合了实际…

C++编程库与框架实战——sqlite3数据库

一,SQLite数据库简介 SQLite是可以实现类似于关系型数据库中各种操作的事务性SQL数据库引擎。 SQLite可以为应用程序提供存储于本地的嵌入式数据库,帮助应用程序实现轻量级的数据存储。 SQLite是一个库文件,并不是单独的进程,它可以静态或动态链接到C++应用程序中,然后…

STM32F10x 定时器

使用定时器实现:B5 E5的开关 添加相关的.h路径文件 添加相关的.c配置文件 led.h文件 用于声明LED函数 #ifndef __LED_H //没有定义__LED_H #define __LED_H //就定义__LED_H #define LED1_ON GPIO_ResetBits(GPIOB,GPIO_Pin_5) #defi…

PyQt6+pyqtgraph折线图绘制显示

1、实现效果 2、环境: 确认已经安装pyqtgraph的模块,如果没有安装,使用命令安装: pip install pyqtgraph 3、代码实现: 绘制折线函数: import sys import random from PySide6.QtWidgets import QAppl…