天天看点

ElasticSearch系列之基础入门篇建议参考官方学习文档PUT,DELETE,GET

ElasticSearch基础入门

  • 建议参考官方学习文档
  • PUT,DELETE,GET
    • 新增数据(PUT请求)
    • 查询数据(GET请求)
    • 更新数据(PUT请求)
    • 删除数据(DELETE请求)

上一篇:ElasticSearch系列之Linux环境(CentOS)下安装ElasticSearch

在使用ES之前我们先了解一下ES中的index,type,document究竟是什么意思,它和普通的关系型数据库在理解上怎么区分。

刚开始我使用ES的时候也是有点蒙,一会儿index,一会type的。在使用过程当中突然发现index不就是mysql中的数据库嘛,type不就是数据库中的一张张表嘛,而document就是表中那一行行的数据。document和普通关系型数据库表中行数据的区别就是document存储的是json格式的数据。

能够区分以上三个名词的意思基本在使用层面上是没问题了,下面直接开始。

建议参考官方学习文档

在学习过程中无论是大V写的博客,还是百度出来的教程,我觉得还是官方文档写的更加容易入门。

官方中文文档地址:https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html

在阅读官方文档过程中可以直接略过前言从基础入门章节开始。

PUT,DELETE,GET

新增数据(PUT请求)

目前入门阶段我们先不适用ES图形化界面来操作,先使用postman来操作ES。

需要注意一点的是ES中默认是自动创建索引的,当你创建数据的时候会根据url来自动创建索引,并把数据放入type中。

例如:我在postman中请求类型选择put,url地址为http://192.168.254.128:9200/my_index_one/employee/1

请求体为:

{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}
           

那么发送之后会返回以下信息:

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

通过返回信息可以看出,创建的索引为my_index_one,类型为:employee,插入数据的id为1,版本为1(如果对id为1的这条数据有修改,删除操作,那么版本将会递增),返回结果为created创建成功,_shards里是一些分片信息。

查询数据(GET请求)

我们把刚刚的Put请求方式改为Get请求方式,url地址不变还是http://192.168.254.128:9200/my_index_one/employee/1那么ES将会返回以下结果

{
    "_index": "my_index_one",
    "_type": "employee",
    "_id": "1",
    "_version": 3,      #由于我操作了3次id为1的数据所以这里版本显示为3
    "_seq_no": 2,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "first_name": "John",
        "last_name": "Smith",
        "age": 25,
        "about": "I love to go rock climbing",
        "interests": [
            "sports",
            "music"
        ]
    }
}
           

可以看到数据正常被查询出来了。

更新数据(PUT请求)

如果想要更新数据的话其实和新增数据是一样的,比如,目前ES里已经有了一条id为1的数据,那么我们要对这条id为1的数据的年龄改为26岁进行更新

get请求:http://192.168.254.128:9200/my_index_one/employee/1

请求体:

{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        26,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}
           

返回结果:

{
    "_index": "my_index_one",
    "_type": "employee",
    "_id": "1",
    "_version": 4,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 7,
    "_primary_term": 1
}
           

可以在返回结果里看到,result为updated,说明数据更新成功。

删除数据(DELETE请求)

删除数据我们就要用到delete请求了,例如我们要删除id为1的数据。

delete请求:http://192.168.254.128:9200/my_index_one/employee/1

返回结果:

{
    "_index": "my_index_one",
    "_type": "employee",
    "_id": "1",
    "_version": 5,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 8,
    "_primary_term": 1
}
           

可以从返回结果里看出result为deleted,说明数据删除成功。

下一篇:ElasticSearch系列之搜索篇-条件查询,表达式搜索,全文搜索,高亮查询

继续阅读