当前位置:   article > 正文

Gulimall学习:ElasticSearch入门_gulimall 样本测试数据

gulimall 样本测试数据

一. 节点状况_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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

三. 更新文档

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

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

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

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

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

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

四. 删除

  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
  • 2
  • 3
  • 4
  • 5
  • 6

(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
  • 2
  • 3
  • 4
  • 5
  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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  1. 导入测试样本数据
    主要提供后面练习
    测试样本数据地址:
    https://raw.githubusercontent.com/elastic/elasticsearch/master/docs/src/test/resources/accounts.json
POST /bank/account/_bulk
【此处贴入上述地址中的所有数据】
  • 1
  • 2
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/952167
推荐阅读
相关标签
  

闽ICP备14008679号