自学内容网 自学内容网

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
}

原文地址:https://blog.csdn.net/qq_73797346/article/details/144001442

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!