第三章 - 简单的 API
万丈高楼平地起
ES 提供了多种操作数据的方式, 其中较为常见的方式就是 RESTful 风格的 API.
简单的体验
利用 Postman 发起 HTTP 请求 (当然也可以在命令行中使用 curl 命令).
索引 Index
创建索引
创建一个名叫 demo 的索引:
PUT http://localhost:9200/demo
ES 响应:
- {
- "acknowledged": true,
- "shards_acknowledged": true,
- "index": "demo"
- }
在创建索引时, 可指定主分片和分片副本的数量:
- PUT http://localhost:9200/demo
- {
- "settings":{
- "number_of_shards":1,
- "number_of_replicas":1
- }
- }
ES 响应:
- {
- "acknowledged": true,
- "shards_acknowledged": true,
- "index": "demo"
- }
查看指定索引
GET http://localhost:9200/demo
ES 响应:
- {
- "demo": {
- "aliases": {},
- "mappings": {},
- "settings": {
- "index": {
- "creation_date": "1561110747038",
- "number_of_shards": "1",
- "number_of_replicas": "1",
- "uuid": "kjPqDUt6TMyywg1P7qgccw",
- "version": {
- "created": "5060499"
- },
- "provided_name": "demo"
- }
- }
- }
- }
查询 ES 中的索引
查询 ES 中索引情况:
GET http://localhost:9200/_cat/indices?v
ES 响应:
health | status | index | uuid | pri | rep | docs.count | docs.deleted | store.size | pri.store.size |
---|---|---|---|---|---|---|---|---|---|
yellow | open | demo | wqkto5CCTpWNdP3HGpLfxA | 5 | 1 | 0 | 0 | 810b | 810b |
yellow | open | .kibana | pwKW9hJyRkO7_pE0MNE05g | 1 | 1 | 1 | 0 | 3.2kb | 3.2kb |
可以看到当前 ES 中一共有 2 个索引, 一个是我们刚创建的 demo, 另一个是 kibana 创建的索引. kibana. 表格中有一些信息代表了索引的一些状态.
health: 健康状态, red 表示不是所有的主分片都可用, 即部分主分片可用. yellow 表示主分片可用备分片不可用, 常常是单机 ES 的健康状态, greens 表示所有的主分片和备分片都可用.(官方对集群健康状态的说明,)
status: 索引状态, open 表示打开可对索引中的文档数据进行读写, close 表示关闭此时索引占用的内存会被释放, 但是此时索引不可进行读写操作.
index: 索引
uuid: 索引标识
pri: 索引的主分片数量
rep: 索引的分片副本数量, 1 表示有一个分片副本 (有多少主分片就有多少备分片, 此处表示 5 个备分片).
docs.count: 文档数量
docs.deleted: 被删除的文档数量
store.size: 索引大小
pri.store.size: 主分片占用的大小
删除索引
删除 demo 索引, 删除索引等同于删库跑路, 请谨慎操作.
DELETE http://localhost:9200/demo
ES 响应:
- {
- "acknowledged": true
- }
类型 Type(同时定义映射 Mapping 字段及类型)
创建类型
在前面基本术语中我们提到类型 Type 类似关系型数据库中的表, 映射 Mapping 定义表结构. 创建类型 Type 时需要配合映射 Mapping.
创建索引 demo 的类型为 example_type, 包含两个字段: created 类型为 date,message 类型为 keyword:
方式一:
- PUT http://localhost:9200/demo/_mapping/example_type
- {
- "properties":{
- "created":{
- "type":"date"
- },
- "message":{
- "type":"keyword"
- }
- }
- }
此时再次执行查询索引的操作, 已经可以发现类型 Type 被创建了, 遗憾的是, 如果类型 Type(或者映射 Mapping) 一旦定义, 就不能删除, 只能修改, 为了保证本教程顺利进行方式二创建类型, 所以此处执行 DELETE http://localhost:9200/demo 删除索引. 删除索引后不要再创建索引, 下面的这种方式是在创建索引的同时创建 Type 并定义 Mapping
方式二:
- PUT http://localhost:9200/demo
- {
- "mappings":{
- "example_type":{
- "properties":{
- "created":{
- "type":"date"
- },
- "message":{
- "type":"keyword"
- }
- }
- }
- }
- }
此时执行 GET http://localhost:9200/demo, 可以看到我们在 ES 中创建了第一个索引以及创建的表结构, 接下来插入就是数据 (即文档).
文档 Document
插入文档
系统定义_id
- POST http://localhost:9200/demo/example_type
- {
- "created":1561135459000,
- "message":"test1"
- }
ES 响应:
- {
- "_index": "demo",
- "_type": "example_type",
- "_id": "AWt67Ql_Tf0FgxupYlBX",
- "_version": 1,
- "result": "created",
- "_shards": {
- "total": 2,
- "successful": 1,
- "failed": 0
- },
- "created": true
- }
查询文档
Elasticsearch 的核心功能 -- 搜索.
POST http://localhost:9200/demo/example_type/_search?pretty
ES 响应:
- {
- "took": 183,
- "timed_out": false,
- "_shards": {
- "total": 5,
- "successful": 5,
- "skipped": 0,
- "failed": 0
- },
- "hits": {
- "total": 1,
- "max_score": 1,
- "hits": [
- {
- "_index": "demo",
- "_type": "example_type",
- "_id": "AWt67Ql_Tf0FgxupYlBX",
- "_score": 1,
- "_source": {
- "created": 1561135459000,
- "message": "test1"
- }
- }
- ]
- }
- }
关于文档的查询是 Elasticsearch 的核心, 后面的章节会详细介绍一些基本的简单查询和更为高级的复杂查询, 此处仅作为对插入数据的验证, 不做过多展开.
修改文档
根据文档_id 修改
- POST http://localhost:9200/demo/example_type/AWt67Ql_Tf0FgxupYlBX/_update
- {
- "doc":{
- "message":"updated"
- }
- }
ES 响应:
- {
- "_index": "demo",
- "_type": "example_type",
- "_id": "AWt67Ql_Tf0FgxupYlBX",
- "_version": 2,
- "result": "updated",
- "_shards": {
- "total": 2,
- "successful": 1,
- "failed": 0
- }
- }
删除文档
删除_id 为 AWt67Ql_Tf0FgxupYlBX 的文档
DELETE http://localhost:9200/demo/example_type/AWt67Ql_Tf0FgxupYlBX
ES 的响应:
- {
- "found": true,
- "_index": "demo",
- "_type": "example_type",
- "_id": "AWt67Ql_Tf0FgxupYlBX",
- "_version": 2,
- "result": "deleted",
- "_shards": {
- "total": 2,
- "successful": 1,
- "failed": 0
- }
- }
关注公众号: CoderBuff, 回复 "es" 获取《ElasticSearch6.x 实战教程》完整版 PDF, 回复 "抽奖" 参与《从 Lucene 到 Elasticsearch: 全文检索实战》图书抽奖活动 (7.17-7.21).
这是一个能给程序员加 buff 的公众号 (CoderBuff)
来源: https://www.cnblogs.com/yulinfeng/p/11210870.html