ElasticSearch基础介绍:2:使用Restful API进行index的增删改查

在这里插入图片描述
ElasticSearch提供了方便的Restful API对数据进行操作,这篇文章将通过具体的示例来介绍如何使用Restful API对index进行操作。

环境准备

可参看如下内容:

  • https://liumiaocn.blog.csdn.net/article/details/82493192

基础知识

ElasticSearch是基于Lucene的全文搜索,在搜索对象的管理上,虽然不是一个数据库,在一些常见的概念上还是可以进行大体的类比,相关常见的概念如下所示:

常用术语 关系型数据库 Elasticsearch
数据库 database index
table type
row document
column field

对于Index的增删改查

相较于数据库此类操作类似于create database或者drop database之类的操作,而在ElasticSearch中,则可以通过Restful的API非常容易地做到,以下结合具体使用示例来进行说明。

创建index

操作内容:
创建一个名为test_index_1的index

使用如下Restful API即可在Elasticsearch中创建测试用的index

设定项 设定值 说明
ElasticSearch URL http://localhost:9200 可以访问到的Elasticsearch的环境,此处为本机的服务
HTTP方法 PUT 使用PUT进行索引的创建
index名称 /test_index_1 指定创建的索引的名称

执行日志如下所示:
从返回的json结果的acknowledged字段为true可以判断此index已经成功创建。

[root@host131 ~]# curl -XPUT http://localhost:9200/test_index_1
{"acknowledged":true,"shards_acknowledged":true,"index":"test_index_1"}[root@host131 ~]# 
[root@host131 ~]# 

查询index

操作内容:
查询名称为test_index_1的index

使用如下Restful API即可在Elasticsearch中查询刚刚创建的index

设定项 设定值 说明
ElasticSearch URL http://localhost:9200 可以访问到的Elasticsearch的环境,此处为本机的服务
HTTP方法 GET 使用GET进行索引的查询
index名称 /test_index_1 指定查询的索引名称

执行日志如下所示:

[root@host131 ~]# curl -XGET http://localhost:9200/test_index_1
{"test_index_1":{"aliases":{},"mappings":{},"settings":{"index":{"creation_date":"1564871150071","number_of_shards":"1","number_of_replicas":"1","uuid":"1E_-j2INSL6z4ZeXgT32pg","version":{"created":"7030099"},"provided_name":"test_index_1"}}}}[root@host131 ~]# 
[root@host131 ~]# 

由于返回的json信息查看起来不是很清晰,指定pretty参数可以对输出进行格式化

[root@host131 ~]# curl -XGET http://localhost:9200/test_index_1?pretty
{
  "test_index_1" : {
    "aliases" : { },
    "mappings" : { },
    "settings" : {
      "index" : {
        "creation_date" : "1564871150071",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "1E_-j2INSL6z4ZeXgT32pg",
        "version" : {
          "created" : "7030099"
        },
        "provided_name" : "test_index_1"
      }
    }
  }
}
[root@host131 ~]# 

另外诸如mysql中通过使用show databases可以列出所有databases一样,使用_cat/indices也可以同样列出Elasticsearch中的index

操作内容:
查询所有的index

使用如下Restful API即可在Elasticsearch中的index的信息

设定项 设定值 说明
ElasticSearch URL http://localhost:9200 可以访问到的Elasticsearch的环境,此处为本机的服务
HTTP方法 GET 使用GET进行索引的查询
_cat/indices /- 查询所有的索引信息

执行日志如下所示:

[root@host131 ~]# curl http://localhost:9200/_cat/indices
yellow open test_index_1 1E_-j2INSL6z4ZeXgT32pg 1 1 0 0 283b 283b
[root@host131 ~]# 

指定?v也可以对输出进行更加清晰的显示

[root@host131 ~]# curl http://localhost:9200/_cat/indices?v
health status index        uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   test_index_1 1E_-j2INSL6z4ZeXgT32pg   1   1          0            0       283b           283b
[root@host131 ~]#

删除index

操作内容:
删除刚刚创建的名为test_index_1的index

使用如下Restful API即可在Elasticsearch中删除测试用的index

设定项 设定值 说明
ElasticSearch URL http://localhost:9200 可以访问到的Elasticsearch的环境,此处为本机的服务
HTTP方法 DELETE 使用DELETE进行索引的删除
index名称 /test_index_1 指定删除的索引的名称

执行日志如下所示:
从返回的json结果的acknowledged字段为true可以判断此index已经成功删除。

[root@host131 ~]# curl -XDELETE http://localhost:9200/test_index_1
{"acknowledged":true}[root@host131 ~]# 
[root@host131 ~]# 

当然也可以通过查询此index是否存在,查询所有的index信息,没有返回信息

[root@host131 ~]# curl http://localhost:9200/_cat/indices
[root@host131 ~]#

使用GET方法查询该index详细信息,返回404,也说明此index不存在

[root@host131 ~]# curl -XGET http://localhost:9200/test_index_1?pretty
{
  "error" : {
    "root_cause" : [
      {
        "type" : "index_not_found_exception",
        "reason" : "no such index [test_index_1]",
        "resource.type" : "index_or_alias",
        "resource.id" : "test_index_1",
        "index_uuid" : "_na_",
        "index" : "test_index_1"
      }
    ],
    "type" : "index_not_found_exception",
    "reason" : "no such index [test_index_1]",
    "resource.type" : "index_or_alias",
    "resource.id" : "test_index_1",
    "index_uuid" : "_na_",
    "index" : "test_index_1"
  },
  "status" : 404
}
[root@host131 ~]#

总结

Elasticsearch通过Restful API,可以非常简单地对index进行常见的操作,在接下来的文章中将继续使用具体的示例来说明在index中进行document的操作方法。

你可能感兴趣的:(#,ElasticSearch)