Gulimall学习:ElasticSearch入门

一. 节点状况_cat

  1. Get /_cat/nodes: 查看所有节点
  2. Get /_cat/health: 查看es健康状况
  3. Get /_cat/master: 查看主节点
  4. Get /_cat/indices: 查看所有索引 类似与sql的show databases

二. 查询文档

  1. GET customer/external/1

响应结果

{
    "_index": "customer", // 在哪个索引
    "_type": "external", // 在哪个类型
    "_id": "1", // 记录id
    "_version": 2, // 版本号
    "_seq_no": 1, //并发控制字段,每次更新就会+1,用来做乐观锁
    "_primary_term": 1, // 同上,主分片重新分配,如重启,就会变化
    "found": true,
    "_source": {  //真正的内容
        "name": "test"
    }
}
  1. _seq_no和_primary_ter乐观锁的使用

postman中测试:

PUT http://localhost:9200/customer/external/1?if_seq_no=7&if_primary_term=1

只有_seq_no和_primary_term是最新值时,才能更新成功,如果不是,则返回409错误:

{
    "error": {
        "root_cause": [
            {
                "type": "version_conflict_engine_exception",
                "reason": "[1]: version conflict, required seqNo [7], primary term [1]. current document has seqNo [8] and primary term [1]",
                "index_uuid": "BVsVjaw_Rzaxkhu6LGd4-A",
                "shard": "0",
                "index": "customer"
            }
        ],
        "type": "version_conflict_engine_exception",
        "reason": "[1]: version conflict, required seqNo [7], primary term [1]. current document has seqNo [8] and primary term [1]",
        "index_uuid": "BVsVjaw_Rzaxkhu6LGd4-A",
        "shard": "0",
        "index": "customer"
    },
    "status": 409
}

三. 更新文档

  1. post更新 带_update
    http://localhost:9200/customer/external/1/_update
    注意在用_update时,必须要使用"doc"
    请求示例
{
    "doc": {
        "name": "test111"
    }
}

这种方式会将传入数据和原来的存储数据做比较,如果一样,则不执行更新,_version、_seq_no、_primary_term字段也不会发生变化

  1. post更新 不带_update
    http://localhost:9200/customer/external/1
    注意在不用_update时,不要携带"doc"
    请求示例
{
    "name":"123"
}

这种方式不会将传入数据和原来的存储数据做比较,每次都会更新

  1. put更新 不能携带_update
    http://localhost:9200/customer/external/1
    请求示例:
{
    "name": "qus",
    "age": "男"
}

这种方式不会将传入数据和原来的存储数据做比较,每次都会更新

四. 删除

  1. 删除文档:DELETE /customer/external/1
    postman发送请求示例: DELETE http://localhost:9200/customer/external/1
  2. 删除索引:DELETE /customer
    postman发送请求示例: DELETE http://localhost:9200/customer

五. 批量bulk

  1. 批量操作指定索引
    语法格式:
POST /index/type/_bulk
{action:{metadata}}
{requestBody}
{action:{metadata}}
{requestBody}
...

(1). action(行为):
create:文档不存在时创建
update:更新文档
index:创建新文档或替换已有文档
delete:删除一个文档
(create和index的区别:如果文档存在,使用create会失败,会提示文档已经存在;但使用index则可以成功执行,将替换已有文档)
(2). metadata:_index,_type,_id
示例:

POST /customer/external/_bulk
{"index":{"_id":"1"}}
{"name":"qus2"}
{"index":{"_id":"2"}}
{"name":"qus3"}
  1. 批量操作不同的索引,不同的文档
    复杂示例:
POST /_bulk
{"delete":{"_index":"website","_type":"blog","_id":"123"}}
{"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 update blog post"}}
  1. 导入测试样本数据
    主要提供后面练习
    测试样本数据地址:
    https://raw.githubusercontent.com/elastic/elasticsearch/master/docs/src/test/resources/accounts.json
POST /bank/account/_bulk
【此处贴入上述地址中的所有数据】
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值