首页 > 其他分享 >Elasticsearch 初步检索

Elasticsearch 初步检索

时间:2022-10-06 11:14:30浏览次数:79  
标签:检索 customer index primary 初步 Elasticsearch external type id

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"}}

8丶样本测试数据

es官方提供的测试数据

https://raw.githubusercontent.com/elastic/elasticsearch/master/docs/src/test/resources/accounts.json (此链接失效)

https://github.com/elastic/elasticsearch/blob/7.5/docs/src/test/resources/accounts.json

 

 

 

 

 

 

标签:检索,customer,index,primary,初步,Elasticsearch,external,type,id
From: https://www.cnblogs.com/mangoubiubiu/p/16757204.html

相关文章

  • ElasticSearch Head
    ElasticSearchHead在谷歌浏览器安装ElasticSearchHead谷歌浏览器----右上角---扩展程序---搜索ElasticSearchHead插件基于插件可以查看ElasticSearch相关数据和集......
  • Docker 安装 Elasticsearch
    1、下载镜像文件#存储和检索数据dockerpullelasticsearch:7.4.2#可视化检索数据dockerpullkibana:7.4.22、创建实例1、创建数据卷映射目录及相关配置mkdir-p......
  • 对比python学julia(第四章:人工智能)--(第一节)OpenCV编程初步(3)
    1.4. 人脸检测(续上)3.检测视频中的人脸在VSCode环境中,新建一个空白源文件,以detect_video.jl作为文件名保存到项目文件夹中,然后编写程序检测视频流......
  • ElasticSearch-7.10版本最新万字长文教程【距离搞懂ELK核心你只差这一片文章】
    ES万字长文教程​​一、认识ELK、ES​​​​1.什么是ELK?​​​​2.什么是ElasticSearch​​​​3.ElasticSearch下载安装教程​​​​二、索引的CRUD​​​​1.创建索引​​......
  • 对比python学julia(第四章:人工智能)--(第一节)OpenCV编程初步(2)
    1.4.       人脸检测人脸检测的任务是从一个图像中寻找出人脸所在的位置和大小。0penCV提供了级联分类器(CascadeClassifier)和人脸特征数据,只用少量代码就能实现......
  • Elasticsearch搜索引擎的使用
    Elasticsearch搜索引擎的使用1.需求分析当用户在搜索框输入关键字后,我们要为用户提供相关的搜索结果。这种需求依赖数据库的模糊查询like关键字可以实现,但是like关键字......
  • SSMS检索Appointment数据
    Appointment表是基于activitypointer生成的表,通过SSMS直接连接到dataverse环境时,虽然表一览里不显示Appointment,要想检索Appointment的数据,只需要直接写SQL类似:select*FRO......
  • 07-Elasticsearch-ES集群搭建
    ElasticSearch集群搭建Elasticsearch集群准备3台虚拟机IP规划192.168.247.142192.168.247.143192.168.247.144三台虚拟机搭建ES建议采用新的机器,我用了之前......
  • 08-Elasticsearch-ES集群脑裂
    集群脑裂什么是集群脑裂如果发生网络中断或者服务器宕机,那么集群会有可能被划分为两部分,各自有自己的master来管理,那么这就是脑裂。集群脑裂解决方案master主节点......
  • 09-Elasticsearch-ES集群文档读写原理
    ES集群的文档读写原理文档写原理文档读原理......