Elasticsearch 初步检索

1丶 _cat

GET /_cat/nodes: 查看所有节点

GET /_cat/health: 查看 es 健康状况

GET /_cat/master: 查看主节点

GET /_cat/indices: 查看所有索引 相当于show database 查看所有数据库信息

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

一 PUT

保存一定要带id,如果es里没有该id 的数据 就是新建 有就是更新

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

PUT customer/external/1; 在customer 索引(数据库的名字)下的external 类型下()保存1号数据为

PUT customer/external/1

 发送消息体

{
    "name":"John Doe"
}

 返回消息体

复制代码
{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}
复制代码

二 POST

保存时不指定id: 插入数据时会自动创建一个uuid

保存时指定id: 和put一样 如果es里没有该id 的数据 就是新建 有就是更新

POST customer/external/

 发送消息体

{
    "name":"John Doe"
}

返回消息体

 
复制代码
{
    "_index": "customer",
    "_type": "external",
    "_id": "yH8WeHgB_91avzot0K17",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 5,
    "_primary_term": 1
}
复制代码

3丶查询文档

GET customer/external/1

消息响应体

复制代码
{
    "_index": "customer",   //哪个索引(数据库)
    "_type": "external",    //哪个类型 (表)
    "_id": "1",             //记录id
    "_version": 2,          //版本号
    "_seq_no": 1,           //并发控制字段,每次更新就会+1 ,用来做乐观锁
    "_primary_term": 1,     //同上,主分片重新分配。如重启,就会变化
    "found": true,       
    "_source": {            //真正的内容
        "name": "John Doe"
    }
}
复制代码

4丶乐观锁

更新携带 ?if_seq_no=0&if_primary_term=1

一 当前id 为 1的数据是

复制代码
{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "_version": 8,
    "_seq_no": 13,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "name": "John Doe12"
    }
}
复制代码

二 如果用户A修改此数据带上

http://192.168.117.134:9200/customer/external/1?if_seq_no=13&if_primary_term=1

Body

复制代码
//修改成功
{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "_version": 9,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 14,
    "_primary_term": 1
}
//此时查询 此时_seq_no 变为15
{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "_version": 9,
    "_seq_no": 14,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "name": "John 大阿斯顿"
    }
}
复制代码

三 在A用户修改完后的基础上,如果B用户再次修改 并且if_seq_no=13

http://192.168.117.134:9200/customer/external/1?if_seq_no=13&if_primary_term=1

此时结果 报错

复制代码
{
    "error": {
        "root_cause": [
            {
                "type": "version_conflict_engine_exception",
                "reason": "[1]: version conflict, required seqNo [13], primary term [1]. current document has seqNo [14] and primary term [1]",
                "index_uuid": "rZoYIImoSOqivOxK5j-hXw",
                "shard": "0",
                "index": "customer"
            }
        ],
        "type": "version_conflict_engine_exception",
        "reason": "[1]: version conflict, required seqNo [13], primary term [1]. current document has seqNo [14] and primary term [1]",
        "index_uuid": "rZoYIImoSOqivOxK5j-hXw",
        "shard": "0",
        "index": "customer"
    },
    "status": 409
}
复制代码

5丶更新文档

一 Post 带_update(Put 并不支持这种操作)

http://192.168.117.134:9200/customer/external/1/_update

修改的消息体

{
    "doc":{
            "name":"John 大阿斯顿"
    }
}

这里会与原数据做对比,如果要修改的数据和原数据一致,就不做操作,如果不一致就做更新操作

并且一定要带上 

{
    "doc":{
    }
}

6丶删除文档&索引(ES 没有提供删除类型的操作)

一 删除文档

DELETE customer/external/1
DELETE customer

直接发DELETE请求

http://192.168.117.134:9200/customer/external/1            DELETE

删除成功

复制代码
{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "_version": 11,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 17,
    "_primary_term": 1
}
复制代码

再次查询此id

{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "found": false
}

二 删除索引

直接发DELETE请求

http://192.168.117.134:9200/customer/            DELETE

消息返回体

{
    "acknowledged": true
}

此时查询

复制代码
{
    "error": {
        "root_cause": [
            {
                "type": "index_not_found_exception",
                "reason": "no such index [customer]",
                "resource.type": "index_expression",
                "resource.id": "customer",
                "index_uuid": "_na_",
                "index": "customer"
            }
        ],
        "type": "index_not_found_exception",
        "reason": "no such index [customer]",
        "resource.type": "index_expression",
        "resource.id": "customer",
        "index_uuid": "_na_",
        "index": "customer"
    },
    "status": 404
}
复制代码

7丶bulk 批量API

直接怼进去kibana 的dev tools

POST /customer/external/_bulk
{"index":{"_id":"1"}}
{"name":"John Doe"}
{"index":{"_id":"2"}}
{"name":"Jane Doe"}

复杂实例

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","retry_on_conflict":3}}
{"doc" :{"title" : "My updated blog post"}}

 

 

 

 

 

 

本文作者:KwFruit

本文链接:https://www.cnblogs.com/mangoubiubiu/p/16757204.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   KwFruit  阅读(33)  评论(0编辑  收藏  举报
历史上的今天:
2020-10-06 上传头像开发-集成阿里云OSS
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起