谷粒商城学习——P105-109es入门

初步检索

1、_cat

get方式查询es的一些信息/_cat/allocation

/_cat/shards
/_cat/shards/{index}
/_cat/master查看主节点  
/_cat/nodes查看所有节点
/_cat/tasks
/_cat/indices查看所有索引
/_cat/indices/{index}
/_cat/segments
/_cat/segments/{index}
/_cat/count
/_cat/count/{index}
/_cat/recovery
/_cat/recovery/{index}
/_cat/health查看健康状况
/_cat/pending_tasks
/_cat/aliases
/_cat/aliases/{alias}
/_cat/thread_pool
/_cat/thread_pool/{thread_pools}
/_cat/plugins
/_cat/fielddata
/_cat/fielddata/{fields}
/_cat/nodeattrs
/_cat/repositories
/_cat/snapshots/{repository}
/_cat/templates

 2,索引一个文档(保存)

保存一个数据,保存在哪个索引的哪个类型下,指定用哪个唯一标识

PUT customer/external/1

{ "name":"John Doe"

 在customer索引下的external类型下保存1号数据,postman下请求

 

 

 返回数据如下:带_的统称为元数据,反应了一些基本信息

 

{
    "_index": "customer",表明该数据在哪个索引(数据库)下;
    "_type": "external",哪些类型下(表)
    "_id": "1",保存数据的id是几
    "_version": 1,保存数据的版本
    "result": "created",保存的结果,created为新建,同样的请求发送多次,后面的均为更新updated,看下图
    "_shards": {分片信息
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

 

 

 

 上面的请求为PUT,保存新增二合一,必须指定id,这个id下的数据没有就新增,有就更新

也可以post请求,post具有PUT的功能,put不指定id会保存,但post可以不指定id,这样post的数据永远都是新增(返回它自己生成的id),若指定id则和put功能一样

 

3,查询文档

es的api是restful的,rest api可移步阮一峰RESTful API 设计指南

因此查询,就是把上面put示例的PUT改为GET即可

 

 

 

{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "_version": 2,
    "_seq_no": 1,_seq_no和_primary_term乐观锁操作相关, 
    "_primary_term": 1,
    "found": true,found为true代表找到了数据
    "_source": {找到的数据的内容
        "name": "John Doe"
    }
}

乐观锁示例:

put或post请求后挂参:

if_seq_no=1&if_primary_term=1

 

 

 

 

 

执行后,_seq_no变成了4,如果比改变_seq_no再次执行上述请求

则会报错,提示current document has seqNo [4] and primary term [1] ,现在seqNo 已经变成4了。这也就控制了一部分并发

 

 "reason": "[1]: version conflict, required seqNo [1], primary term [1]. current document has seqNo [4] and primary term [1]",

 4,更新文档

前面post数据基础上,加上/_update,同事请求体用{"doc":}包装起来,同样可实现更新操作。ps:put不行,405not allowed

 

加不加_update有什么区别呢?

加了_update,除了请求体需要用{"doc":}包起来,在更新内容不变的情况下,_version和_seq_no是不变的

{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "_version": 3,
    "result": "noop",no+operation,数据没有变化的情况下,执行_update更新时没有操作的
    "_shards": {
        "total": 0,
        "successful": 0,
        "failed": 0
    },
    "_seq_no": 4,
    "_primary_term": 1
}

 5,删除文档&索引

删除文档

DELETE /customer/external/1

 

{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "_version": 4,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 5,
    "_primary_term": 1
}

 

 删除索引 

DELETE customer即可

 

 

 

 

 

类型相当于mysql中的表,无法直接删除

6.bulk批量API

POST customer/external/_bulk

{"index":{"_id":"1"}}//index索引一条数据,id为1
{"name":"Jone Doe"}//index索引数据的内容
{"index":{"_id":"2"}}
{"name":"Jane Doe"}

 必须是post,两行为一个整体,语法格式

{action:{metadata}}\n
{request body  }\n

{action:{metadata}}\n
{request body  }\n

postman不支持换行类型的数据,移步Kibana

 

 

 

 

#! Deprecation: [types removal] Specifying types in bulk requests is deprecated.
{
  "took" : 112,花费了112毫秒
  "errors" : false,没有发生任何错误
  "items" : [items有两条数据,每条数据独立统计它自己的数据,任何一条记录的失败,都不会影响任何一条其他数据
    {
      "index" : {保存的原信息体
        "_index" : "customer",
        "_type" : "external",
        "_id" : "1",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 0,
        "_primary_term" : 1,
        "status" : 201
      }
    },
    {
      "index" : {
        "_index" : "customer",
        "_type" : "external",
        "_id" : "2",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 1,
        "_primary_term" : 1,
        "status" : 201
      }
    }
  ]
}

复杂实例POST /_bulk

POST /_bulk 没有指定任何索引的任何类型,说明是要对整个es批量操作
{"delete":{"_index":"website","_type":"blog","_id":"123"}}删除哪个索引哪个类型下id为哪个的数据,删除没有请求体,请求信息都在元数据中  
{"create":{"_index":"website","_type":"blog","_id":"123"}}创建操作
{"title":"My first blog post"}创建操作的内容
{"index":{"_index":"website","_type":"blog"}}保存记录
{"title":"my second blog post"}保存的内容
{"update":{"_index":"website","_type":"blog","_id":"123"}}更新记录
{"doc":{"title":"My updated blog post"}}更新的内容

运行结果:

{
  "took" : 190,
  "errors" : false,
  "items" : [
    {
      "delete" : {
        "_index" : "website",
        "_type" : "blog",
        "_id" : "123",
        "_version" : 1,
        "result" : "not_found",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 0,
        "_primary_term" : 1,
        "status" : 404说明没找到这条记录
      }
    },
    {
      "create" : {
        "_index" : "website",
        "_type" : "blog",
        "_id" : "123",
        "_version" : 2,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 1,
        "_primary_term" : 1,
        "status" : 201创建成功
      }
    },
    {
      "index" : {
        "_index" : "website",
        "_type" : "blog",
        "_id" : "H62vk3oB2xZASEa9yqEY",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 2,
        "_primary_term" : 1,
        "status" : 201
      }
    },
    {
      "update" : {
        "_index" : "website",
        "_type" : "blog",
        "_id" : "123",
        "_version" : 3,
        "result" : "updated",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 3,
        "_primary_term" : 1,
        "status" : 200
      }
    }
  ]
}

index和create有何区别?

create对于已有的文档,不会插入,会报文档已存在异常 

index对于已有的文档,会检查_version,一致会覆盖并_version递增,不一致会失败

 

准备了一份顾客银行账户信息的虚构的JSON文档样本。每个文档都有下列的schema(模式)。

我用的时候原地址以及404了,感谢评论区给出的地址

https://gitee.com/xlh_blog/common_content/blob/master/es测试数据.json,不太方便复制,也不太方便插入到代码,我给大家放到文件里了

谷粒商城P109数据.json

导入测试数据

POST bank/account/_bulk

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章