elasticSearch核心概念的介绍(三):文档的增删改查

文档的增删改查

在上一章介绍了映射的使用,有兴趣的朋友可以参考一下
elasticSearch核心概念的介绍(二):映射的介绍和使用
这一章我们来简单介绍 一下 文档的使用,在es中文档也就是指的数据,如同mysql中的表数据。

  • 新增文档

    • 请求

      curl -X PUT "http://172.25.45.150:9200/nba/_doc/1" -H 'Content-Type:application/json' -d '
      {
          "name":"哈登",
          "team_name":"火箭",
          "position":"得分后卫",
          "play_year":"10",
          "jerse_no":"13"
      }
      '
      

      http://172.25.45.150:9200/nba/_doc/1 1:为id 我们可以自行指定,我们也可以不指定id进行创建

      http://172.25.45.150:9200/nba/_doc 不指定id的话 为POST请求

    • 响应

      {
          "_index": "nba",
          "_type": "_doc",
          "_id": "1",
          "_version": 1,
          "result": "created",
          "_shards": {
              "total": 2,
              "successful": 1,
              "failed": 0
          },
          "_seq_no": 0,
          "_primary_term": 1
      }
      
  • 获取文档信息

    • 请求

      curl -X GET "http://172.25.45.150:9200/nba/_doc/1" 
      
    • 响应

      {
          "_index": "nba",
          "_type": "_doc",
          "_id": "1",
          "_version": 1,
          "_seq_no": 0,
          "_primary_term": 1,
          "found": true,
          "_source": {
              "name": "哈登",
              "team_name": "火箭",
              "position": "得分后卫",
              "play_year": "10",
              "jerse_no": "13"
          }
      }
      
  • 自动创建索引

    • 查看auto_create_index 开关状态

      • 请求
      curl -X GET "http://172.25.45.150:9200/_cluster/settings"
      
      • 响应:
      {
          "persistent": {
              "action": {
                  "destructive_requires_name": "true"
              }
          },
          "transient": {}
      }
      
    • 当索引不存在并且auto_create_index为true的时候,新增文档的时候如果不存在该索引就会自动创建索引

    • 修改auto_create_index状态

      • 请求

        curl -X PUT "http://172.25.45.150:9200/_cluster/settings" -H 'Content-Type:application/json' -d '
        	{
        		"persistent":{
        			"action.auto_create_index":false
        		}
        	}
        '
        
      • 响应

        {
            "acknowledged": true,
            "persistent": {
                "action": {
                    "auto_create_index": "false"
                }
            },
            "transient": {}
        }
        

        修改之后再次进行 curl -X GET “http://172.25.45.150:9200/_cluster/settings” 会发现多了一条auto_create_index=false的字段。

  • 当auto_create_index=false时新增一个不存在索引的文档。

    • 请求

      curl -X PUT "http://172.25.45.150:9200/wnba/_doc/1" -H 'Content-Type:application/json' -d '
      
      {
          "name":"杨超越",
          "team_name":"梦之队",
          "position":"组织后卫",
          "play_year":"0",
          "jerse_no":"18"
      }
      
      '
      
    • 响应

      {
          "error": {
              "root_cause": [
                  {
                      "type": "index_not_found_exception",
                      "reason": "no such index [wnba]",
                      "resource.type": "index_expression",
                      "resource.id": "wnba",
                      "index_uuid": "_na_",
                      "index": "wnba"
                  }
              ],
              "type": "index_not_found_exception",
              "reason": "no such index [wnba]",
              "resource.type": "index_expression",
              "resource.id": "wnba",
              "index_uuid": "_na_",
              "index": "wnba"
          },
          "status": 404
      }
      

      将auto_create_index设置为true,我们再来试试创建文档

    • 响应

      {
          "_index": "wnba",
          "_type": "_doc",
          "_id": "1",
          "_version": 1,
          "result": "created",
          "_shards": {
              "total": 2,
              "successful": 1,
              "failed": 0
          },
          "_seq_no": 0,
          "_primary_term": 1
      }
      

      会发现wnba的索引会帮我们创建,同时mapping也会根据文档信息帮我们自动创建 ,我们来试一下

    • 请求

      curl -X GET "http://172.25.45.150:9200/wnba/_mapping"
      
    • 响应

      {
          "wnba": {
              "mappings": {
                  "properties": {
                      "jerse_no": {
                          "type": "text",
                          "fields": {
                              "keyword": {
                                  "type": "keyword",
                                  "ignore_above": 256
                              }
                          }
                      },
                      "name": {
                          "type": "text",
                          "fields": {
                              "keyword": {
                                  "type": "keyword",
                                  "ignore_above": 256
                              }
                          }
                      },
                      "play_year": {
                          "type": "text",
                          "fields": {
                              "keyword": {
                                  "type": "keyword",
                                  "ignore_above": 256
                              }
                          }
                      },
                      "position": {
                          "type": "text",
                          "fields": {
                              "keyword": {
                                  "type": "keyword",
                                  "ignore_above": 256
                              }
                          }
                      },
                      "team_name": {
                          "type": "text",
                          "fields": {
                              "keyword": {
                                  "type": "keyword",
                                  "ignore_above": 256
                              }
                          }
                      }
                  }
              }
          }
      }
      
  • 查看多个文档

    • 请求一

      curl -X POST "http://172.25.45.150:9200/_mget" -H 'Content-Type:application/json' -d '
      {
          "docs":[
              {
                  "_index":"nba",
                  "_type":"_doc",
                  "_id":"1"
              },
              {
                  "_index":"nba",
                  "_type":"_doc",
                  "_id":"2"
              }
          ]
      }
      '
      
    • 请求二

      curl -X POST "http://172.25.45.150:9200/nba/_mget" -H 'Content-Type:application/json' -d '
      {
          "docs":[
              {
                  "_type":"_doc",
                  "_id":"1"
              },
              { 
                  "_type":"_doc",
                  "_id":"2"
              }
          ]
      }
      '
      
    • 请求三

      curl -X POST "http://172.25.45.150:9200/nba/_doc/_mget" -H 'Content-Type:application/json' -d '
      {
          "docs":[
              {
                  "_id":"1"
              },
              {  
                  "_id":"2"
              }
          ]
      }
      '
      
    • 请求四

      curl -X POST "http://172.25.45.150:9200/nba/_doc/_mget" -H 'Content-Type:application/json' -d '
      {
          "ids":["1","2"]
      }
      '
      
    • 响应

      {
          "docs": [
              {
                  "_index": "nba",
                  "_type": "_doc",
                  "_id": "1",
                  "_version": 1,
                  "_seq_no": 0,
                  "_primary_term": 1,
                  "found": true,
                  "_source": {
                      "name": "哈登",
                      "team_name": "火箭",
                      "position": "得分后卫",
                      "play_year": "10",
                      "jerse_no": "13"
                  }
              },
              {
                  "_index": "nba",
                  "_type": "_doc",
                  "_id": "2",
                  "found": false
              }
          ]
      }
      
  • 修改文档

    • 根据提供的文档片段更新数据

      • 请求

        curl -X POST "http://172.25.45.150:9200/nba/_update/1" -H 'Content-Type:application' -d '
        {
            "doc":{
            "name":"哈登",
            "team_name":"火箭",
            "position":"双能卫",
            "play_year":"10",
            "jerse_no":"13"
        	}
        }
        '
        
      • 响应

        {
            "_index": "nba",
            "_type": "_doc",
            "_id": "1",
            "_version": 2,
            "result": "updated",
            "_shards": {
                "total": 2,
                "successful": 1,
                "failed": 0
            },
            "_seq_no": 2,
            "_primary_term": 1
        }
        
  • 删除文档

    • 请求

      curl -X DELETE "http://172.25.45.150:9200/nba/_doc/22"
      
    • 响应

      {
          "_index": "nba",
          "_type": "_doc",
          "_id": "22",
          "_version": 3,
          "result": "deleted",
          "_shards": {
              "total": 2,
              "successful": 1,
              "failed": 0
          },
          "_seq_no": 10,
          "_primary_term": 1
      }
      
  • _source 字段

    在上面请求文档数据的时候我们会发现,我们需要的数据存放在_source的字段下面,_source他是作为一个载体,我们如果需要修改一些字段的信息,也可以通过_srouce去操作

    • 新增一个字段(age)

      • 请求

        curl -X POST "http://172.25.450.156:9200/nba/_update/1" -H 'Content-Type:application/json' -d '
        	{
                "script" : "ctx._source.age=18"
            }
        '
        
      • 响应

        {
            "_index": "nba",
            "_type": "_doc",
            "_id": "1",
            "_version": 3,
            "result": "updated",
            "_shards": {
                "total": 2,
                "successful": 1,
                "failed": 0
            },
            "_seq_no": 3,
            "_primary_term": 1
        }
        

        新增成功之后,再通过

        curl -X GET “http://172.25.45.150:9200/nba/_doc/1”

        就会发现_srouce下面多了一个age字段

    • 删除一个字段

      • 请求

        curl -X POST "http://172.25.450.156:9200/nba/_update/1" -H 'Content-Type:application/json' -d '
        	{
                "script" : "ctx._source.remove(\"age\")"
            }
        '
        
      • 响应

        {
            "_index": "nba",
            "_type": "_doc",
            "_id": "1",
            "_version": 4,
            "result": "updated",
            "_shards": {
                "total": 2,
                "successful": 1,
                "failed": 0
            },
            "_seq_no": 4,
            "_primary_term": 1
        }
        
    • 修改字段

      • 请求
      curl -X POST "http://172.25.450.156:9200/nba/_update/1" -H 'Content-Type:application/json' -d '
      	{
              "script":{
              	"source":"ctx._source.age += params.age",
              	 "params":{
              		"age":4
              	  }
              }
         '
      
      • 响应

        {
            "_index": "nba",
            "_type": "_doc",
            "_id": "1",
            "_version": 7,
            "result": "updated",
            "_shards": {
                "total": 2,
                "successful": 1,
                "failed": 0
            },
            "_seq_no": 7,
            "_primary_term": 1
        }
        
    • 修改不存在的文档字段

      • 请求

        curl -X POST "http://172.25.450.156:9200/nba/_update/22" -H 'Content-Type:application/json' -d '
        	{
                "script":{
                	"source":"ctx._source.age += params.allstart",
                	 "params":{
                		"allstart":4
                	  }
                },
                "upsert":{
                	"allstart":1
                }
               }
           '
        

        请求说明:

        • upsert: 当指定的文档不存在时,upsert参数包含的内容将会被插入到索引中,作为一个新文档;如果指定的文档存在,ElasticSearch引擎将会执行指定的更新逻辑(script),通过在创建该文档的数据同事,会更新同步mapping的对应属性。
      • 响应

        {
            "_index": "nba",
            "_type": "_doc",
            "_id": "22",
            "_version": 2,
            "result": "updated",
            "_shards": {
                "total": 2,
                "successful": 1,
                "failed": 0
            },
            "_seq_no": 9,
            "_primary_term": 1
        }
        
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈橙橙丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值