elasticsearch笔记之rest风格的操作

一、elasticsearch增删改查

跟数据库一样,把它当做一个数据库,有增删改查的功能,开启了elasticsearch服务之后(localhost:9200),然后通过开启elasticsearch-head-master(localhost:9100)就可以查看里面有什么数据了,最主要的是通过kibana操作elasticsearch的数据(locaihost:5601)。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、使用步骤

1.基本操作

#kibana左侧输入的数据
put /index1/type1/haihui
{
    
    
  "name":"廖晨威",
  "age":18
}
#kibana右侧的数据
#! Deprecation: [types removal] Specifying types in document index requests is deprecated, use the typeless endpoints instead (/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id}).
{
    
    
  "_index" : "index1",
  "_type" : "type1",
  "_id" : "haihui",
  "_version" : 3,
  "result" : "updated",
  "_shards" : {
    
    
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 2,
  "_primary_term" : 1
}

在这里插入图片描述
我们打开,elasticsearch-head-master之后,通过localhost:9100就可以查看elasticsearch中的数据。
在这里插入图片描述

#kibana左侧输入索引,数据的类型
put /test2
{
    
    
  "mappings":{
    
    
    "properties":{
    
    
      "name":{
    
    
        "type":"text"
      },
      "age":{
    
    
        "type":"integer"
      },
      "birthday":{
    
    
        "type":"date"
      }
    }
  }
}
#kibana右侧显示数据类型定义成功
{
    
    
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "test2"
}
#kibana左侧获取索引的信息
get test2
#kibana右侧得到索引的信息
{
    
    
  "test2" : {
    
    
    "aliases" : {
    
     },
    "mappings" : {
    
    
      "properties" : {
    
    
        "age" : {
    
    
          "type" : "integer"
        },
        "birthday" : {
    
    
          "type" : "date"
        },
        "name" : {
    
    
          "type" : "text"
        }
      }
    },
    "settings" : {
    
    
      "index" : {
    
    
        "creation_date" : "1614336789596",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "pmBaQzMeQuyG7_6rr2YQFQ",
        "version" : {
    
    
          "created" : "7060299"
        },
        "provided_name" : "test2"
      }
    }
  }
}

在这里插入图片描述

#定义好规则之后,往test2索引里边加入数据(kibana左侧数据)
put /test2/_doc/haihui
{
    
    
  "name":"海辉一定飞天",
  "age":12,
  "birthday":"1999-01-16"
}
#kibana右侧数据
{
    
    
  "_index" : "test2",
  "_type" : "_doc",
  "_id" : "haihui",
  "_version" : 4,
  "result" : "updated",
  "_shards" : {
    
    
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 3,
  "_primary_term" : 1
}

在这里插入图片描述
ps:这里需要注意的是,如果我们直接往索引里面添加数据,并不指定数据的类型,那么elasticsearch会默认给我们添加字段的类型。(以上面的index1索引为例)

#kibana左侧获取index1的数据
get index1
#kibana右侧数据显示结果
{
    
    
  "index1" : {
    
    
    "aliases" : {
    
     },
    "mappings" : {
    
    
      "properties" : {
    
    
        "age" : {
    
    
          "type" : "long"
        },
        "name" : {
    
    
          "type" : "text",
          "fields" : {
    
    
            "keyword" : {
    
    
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    },
    "settings" : {
    
    
      "index" : {
    
    
        "creation_date" : "1614335283933",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "RemiFiRBQSOYXFMZYd3hPg",
        "version" : {
    
    
          "created" : "7060299"
        },
        "provided_name" : "index1"
      }
    }
  }
}

#kibana左侧获取更多关于elasticsearch的信息,比如文档数量,存储数据的大小,删除的数量等等
get _cat/indices?v
health status index                    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   mall-debug-2021.02.24    3wVVsg99S1il98S-NSeXjA   1   1       2982            0    839.1kb        839.1kb
yellow open   test2                    pmBaQzMeQuyG7_6rr2YQFQ   1   1          1            1     13.7kb         13.7kb
yellow open   mall-business-2021.02.10 8rQVzZ-lSYOrKX6mKCDkZA   1   1          6            0     32.4kb         32.4kb
green  open   .kibana_task_manager_1   jjzmXQdbREujMNHPe5Cp5A   1   0          2            1     26.6kb         26.6kb
yellow open   index1                   RemiFiRBQSOYXFMZYd3hPg   1   1          1            0      8.3kb          8.3kb
yellow open   jd_goods                 npol-_qeQyO7-HJrJyc5tw   5   1          0            0      1.3kb          1.3kb
green  open   .apm-agent-configuration sT7VY13lQzKgnYDRmWiD7Q   1   0          0            0       283b           283b
yellow open   mall-error-2021.02.24    gwkVMLXNQ5miMrqleAdxHQ   1   1         96            0    145.2kb        145.2kb
yellow open   mall-error-2021.02.10    d3_Qip-HRgOuR_7ku2Wtpw   1   1         32            0    139.7kb        139.7kb
yellow open   mall-business-2021.02.24 NRqi8nNRRPGlCFjPSKm7zA   1   1         18            0     66.6kb         66.6kb
yellow open   mall-debug-2021.02.10    OVjkNQGqQuerQkIXzTcUCQ   1   1        994            0    422.4kb        422.4kb
yellow open   luhaihui                 YwIPSvteRv-OM8nIyqF2Jw   5   1          0            0      1.3kb          1.3kb
green  open   .kibana_1                jxlwNm9SSOWQxiPG_Rpcog   1   0         13            3     30.6kb         30.6kb

#kibana左侧数据更新test2索引,
post /test2/_doc/haihui/_update
{
    
    
  "doc":{
    
    
    "name":"miracle辉",
    "age":22
  }
}
#kibana右侧显示结果
#! Deprecation: [types removal] Specifying types in document update requests is deprecated, use the endpoint /{index}/_update/{id} instead.
{
    
    
  "_index" : "test2",
  "_type" : "_doc",
  "_id" : "haihui",
  "_version" : 5,
  "result" : "updated",
  "_shards" : {
    
    
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 4,
  "_primary_term" : 1
}

在这里插入图片描述
在这里插入图片描述

#删除一个索引,kibana左侧
delete /test2
#kibana右侧数据
{
    
    
  "acknowledged" : true
}

在这里插入图片描述
删除索引的所有语法汇总

  • delete /索引名
  • delete /索引名1,索引名2…
  • delete /index_*(删除所有带index_前缀的索引)
  • delete /_all(删除所有的索引)
  • delete /*(删除所有的索引)

总结

以上基本完成了如何操作elasticsearch的索引,增删改查,以及自定义字段的类型,默认情况下,如果不指定字段类型,elasticsearch会默认指定一个类型。

猜你喜欢

转载自blog.csdn.net/qq_41486775/article/details/114146581