李成笔记网

专注域名、站长SEO知识分享与实战技巧

Elasticsearch入门教程之索引篇(elasticsearch索引过程)

在这前的文章中我们介绍了Elasticsearch的相关概念等内容,在这一篇中我们开始介绍Elasticsearch中比较具体的功能,也就是索引的相关内容。那么通过之前的介绍我们知道在Elasticsearch中的索引就当于关系型数据库中数据库。所以在Elasticsearch中是可以按照业务的不同创建多个不同的索引的。既然我们非常熟悉数据库,所以我们Elasticsearch中的索引内容,也无非就是增删改查等操作。唯一不同于数据库的是,在数据库中如果要操作则使用create、drop等命令。而在Elasticsearch中所有的操作都是RESTful API的方式,不管是操作索引,实际上所有Elasticsearch的操作都是采用的RESTful API的方式。这时有可能有人会想,既然都是RESTful API的方式,那怎么区别增删改查呢?实际上Elasticsearch采用了一种比较巧妙的方式来实现增删改查操作。也就是请求的类型。我们知道HTTP请求类型分为GET、POST、PUT、DELETE等。Elasticsearch就是采用这种方式区分增删改查的。下面我们看一下具体是怎么区分的。

  • GET:获取资源
  • POST:创建或更新资源
  • PUT:创建或更新资源
  • DELETE:删除资源


下面我们按照上面的介绍来具体操作一下Elasticsearch中索引的增删改查。我们首先检测一下我们的Elasticsearch是否启动成功。请求的接口地址如下:

GET http://127.0.0.1:9200/

请求响应结果:

{
  "name": "localhost",
  "cluster_name": "elasticsearch",
  "cluster_uuid": "778BQIY_TK--vuRqe-eOPQ",
  "version": {
    "number": "7.9.3",
    "build_flavor": "default",
    "build_type": "tar",
    "build_hash": "c4138e51121ef06a6404866cddc601906fe5c868",
    "build_date": "2020-10-16T10:36:16.141335Z",
    "build_snapshot": false,
    "lucene_version": "8.6.2",
    "minimum_wire_compatibility_version": "6.8.0",
    "minimum_index_compatibility_version": "6.0.0-beta1"
  },
  "tagline": "You Know, for Search"
}

我们看上面的信息,我们的Elasticsearch已经启动成功了。下面我们介绍一下怎么创建索引。具体命令如下:

PUT http://127.0.0.1:9200/es

请求响应结果:

{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "es"
}

上面的返回结果显示的是true,所以表示我们es索引创建成功。下面我们查询一下这个索引,看一下在Elasticsearch中一个索引都包含哪些信息。

查询索引的命令如下:

GET http://127.0.0.1:9200/es

请求返回结果:

{
  "es": {
    "aliases": {},
    "mappings": {},
    "settings": {
      "index": {
        "creation_date": "1605247926879",
        "number_of_shards": "1",
        "number_of_replicas": "1",
        "uuid": "sEFUNSLQTWyRsJEISv90Tw",
        "version": {
          "created": "7090399"
        },
        "provided_name": "es"
      }
    }
  }
}

我们看该命令返回了一些json信息,下面我们简单介绍一下上述的返回结果。

  • creation_date:创建索引的时间。
  • uuid:索引的唯一标识,自动生成的。类似于MongoDB中的_id字段。
  • version:版本号,每一次索引的改动,该版本号都会增加。
  • provided_name:索引的名字。

下面我们介绍一下删除索引的命令:

DELETE http://127.0.0.1:9200/es

请求响应结果:

{
  "acknowledged": true
}

这时es索引就被我们成功删除了。下面我们再调用查询索引命令,看一下es索引是否删除成功。

GET http://127.0.0.1:9200/

请求响应结果:

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

我们看这时查询索引的接口返回的错误信息,并且状态是404,则标识该索引,确实被我们删除成功了。

下面我们介绍一下,怎么通过一条命令查询多个索引,在我们演示这个之前,我们先提前使用PUT命令创建多个索引来方便我们演示,例如:es、es2、es3等。下面我们可以使用下面的命令,查询多个索引信息。具体命令如下:

GET http://127.0.0.1:9200/es,es2

请求响应结果:

{
  "es": {
    "aliases": {},
    "mappings": {},
    "settings": {
      "index": {
        "creation_date": "1605249664839",
        "number_of_shards": "1",
        "number_of_replicas": "1",
        "uuid": "_kZJ-12PRl-mgRwXoUTuzg",
        "version": {
          "created": "7090399"
        },
        "provided_name": "es"
      }
    }
  },
  "es2": {
    "aliases": {},
    "mappings": {},
    "settings": {
      "index": {
        "creation_date": "1605249708887",
        "number_of_shards": "1",
        "number_of_replicas": "1",
        "uuid": "ZNstN1QET2ui217V6SbENQ",
        "version": {
          "created": "7090399"
        },
        "provided_name": "es2"
      }
    }
  }
}

我们看上述的响应结果,我们只要使用GET方式,并且将我们的索引名字用逗号分割,就可以用一条命令一次性查询出我们指定的索引信息。这时有人会想到,如果我们索引比较多时,那应该怎么办呢?别着急Elasticsearch还提供了其它两种方式,来方便我们一次性查询出索引的索引。

下面我介绍第一种查询全部索引的方式,也就是_all方式,具体命令如下:

GET http://127.0.0.1:9200/_all

请求响应结果:

{
  "es": {
    "aliases": {},
    "mappings": {},
    "settings": {
      "index": {
        "creation_date": "1605249664839",
        "number_of_shards": "1",
        "number_of_replicas": "1",
        "uuid": "_kZJ-12PRl-mgRwXoUTuzg",
        "version": {
          "created": "7090399"
        },
        "provided_name": "es"
      }
    }
  },
  "es2": {
    "aliases": {},
    "mappings": {},
    "settings": {
      "index": {
        "creation_date": "1605249708887",
        "number_of_shards": "1",
        "number_of_replicas": "1",
        "uuid": "ZNstN1QET2ui217V6SbENQ",
        "version": {
          "created": "7090399"
        },
        "provided_name": "es2"
      }
    }
  },
  "es3": {
    "aliases": {},
    "mappings": {},
    "settings": {
      "index": {
        "creation_date": "1605249714254",
        "number_of_shards": "1",
        "number_of_replicas": "1",
        "uuid": "5aqdCn56T3ypvsBJAgU9gg",
        "version": {
          "created": "7090399"
        },
        "provided_name": "es3"
      }
    }
  }
}

我们上述命令中故意没有将es3的索引信息查询出来,而这次使用的_all命令则一次性地将所有索引都查询出来了。这就是_all命令的具体使用。

接下来我们介绍一下第二种查询所有索引的方式,也就是_cat/indices的方式。具体命令如下:

GET http://127.0.0.1:9200/_cat/indices

请求响应结果:

yellow open es3 5aqdCn56T3ypvsBJAgU9gg 1 1 0 0 208b 208b
yellow open es2 ZNstN1QET2ui217V6SbENQ 1 1 0 0 208b 208b
yellow open es  _kZJ-12PRl-mgRwXoUTuzg 1 1 0 0 208b 208b

虽然看上面的响应结果,不是我们平时常见的json返回方式。但这并不会影响我们查询全部所有索引的信息。虽然我们不太清楚上面显示的某些信息是什么意思,但我们仔细观察还是可以发现上述响应结果中已经显示了这3个索引的名字了。为了更直观地展示上面索引的信息,我们可以添加v参数。那v参数是干什么的呢。我们执行下面命令验证一下。具体命令如下:

GET http://127.0.0.1:9200/_cat/indices?v

请求响应结果:

health status index uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   es3   5aqdCn56T3ypvsBJAgU9gg   1   1          0            0       208b           208b
yellow open   es2   ZNstN1QET2ui217V6SbENQ   1   1          0            0       208b           208b
yellow open   es    _kZJ-12PRl-mgRwXoUTuzg   1   1          0            0       208b           208b

我们看这时显示的索引信息就有了每个信息的描述了。这就是_cat/indices的方式,查询全部索引的使用。

下面我们介绍一下怎么来验证一下索引是否存在,就类似于Redis中的exists命令。但无奈Elasticsearch中并没有提供exists命令来验证索引是否存在。而是采用了其它的方式。也就是还是RESTful API的方式。具体命令如下:

HEAD http://127.0.0.1:9200/es

请求响应结果:

HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 223

<Response body is empty>

我们看请求的响应状态码为200,这就表示该索引已存在。下面我们访问一个不存在的索引例如es4,看一下返回的结果是什么。

HEAD http://127.0.0.1:9200/es4

请求响应结果:

HTTP/1.1 404 Not Found
content-type: application/json; charset=UTF-8
content-length: 347

<Response body is empty>

我们看这时返回的响应码为 404,这就表示该索引不存在。

下面我们介绍一下Elasticsearch中索引的关闭功能。这一点和数据库不一样,在Elasticsearch中我们可以关闭一下指定的索引。具体命令如下:

POST http://127.0.0.1:9200/es/_close

请求响应结果:

{
  "acknowledged": true,
  "shards_acknowledged": true,
  "indices": {
    "es": {
      "closed": true
    }
  }
}

我们看上述的信息,已经提示closed为true。这就标识该索引已经关闭了,下面我们查询一下该索引,看一下返回什么信息。

GET http://127.0.0.1:9200/_cat/indices?v

请求响应结果:

health status index uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   es3   5aqdCn56T3ypvsBJAgU9gg   1   1          0            0       208b           208b
yellow open   es2   ZNstN1QET2ui217V6SbENQ   1   1          0            0       208b           208b
yellow close  es    _kZJ-12PRl-mgRwXoUTuzg   1   1            

我们看这时es索引的status状态已经为close状态了。下面我们在开启一下该索引。

POST http://127.0.0.1:9200/es/_open

请求响应结果:

{
  "acknowledged": true,
  "shards_acknowledged": true
}

我们在调用一下查询索引的接口,看一下es索引开启成功没有。

GET http://127.0.0.1:9200/_cat/indices?v

请求响应结果:

health status index uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   es3   5aqdCn56T3ypvsBJAgU9gg   1   1          0            0       208b           208b
yellow open   es2   ZNstN1QET2ui217V6SbENQ   1   1          0            0       208b           208b
yellow open   es    _kZJ-12PRl-mgRwXoUTuzg   1   1          0            0       230b           230b

这时我们的es索引就打开了。以上内容就是Elasticsearch中的索引相关的内容,如有不正确欢迎指出,谢谢大家的支持。

项目源码:
https://github.com/jilinwula/jilinwula-elasticsearch

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言