0


Es初步检索命令

1、_cat

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

请求 :

http://192.168.107.129:9200/_cat/nodes

响应 :

** 127.0.0.1 15 95 8 0.19 0.16 0.24 dilm * 32bb46713f1b**

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

请求 :

http://192.168.107.129:9200/_cat/nodes

响应 :

1672289253 04:47:33 elasticsearch green 1 1 3 3 0 0 0 0 - 100.0%

GET /_cat/master:查看主节点

请求 :

http://192.168.107.129:9200/_cat/master

响应 :

0UIKX1bRRBWnz03krbYtXw 127.0.0.1 127.0.0.1 32bb46713f1b

GET /_cat/indices:查看所有索引 (show databases)

请求 :

http://192.168.107.129:9200/_cat/masterhttp://192.168.107.129:9200/_cat/indiceshttp://192.168.107.129:9200/_cat/master

响应 :

green open .kibana_task_manager_1 zrlBv8ZPRrelVQk8Ha-4GQ 1 0 2 0 38.3kb 38.3kb
green open .apm-agent-configuration isnuogXsRt-nDUuNNoRp9A 1 0 0 0 283b 283b
green open .kibana_1 ITqnjQoXRse1VhpieOA6oQ 1 0 2 0 11.3kb 11.3kb

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

发送put请求

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

PUT customer/external/1;在 customer 索引下的 external 类型下保存 1 号数据为

**PUT customer/external/1 **

** 请求:**

http://192.168.107.129:9200/customer/external/1

jsion数据:

{ 
    "name": "John Doe"
}

** 响应**

{
    "_index": "customer",//在哪个索引下
    "_type": "external",//在哪个type下
    "_id": "1",//id
    "_version": 1,//版本
    "result": "created",//结果新建
    "_shards": {//分片
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

**再一次这个发送请求 **

响应:

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

这个时候可以发现是更新操作。发送多次为更新

注意:再一次发送这个请求的时候如果发现出现的响如果为

{
    "error": {
        "root_cause": [
            {
                "type": "cluster_block_exception",
                "reason": "index [customer] blocked by: [FORBIDDEN/12/index read-only / allow delete (api)];"
            }
        ],
        "type": "cluster_block_exception",
        "reason": "index [customer] blocked by: [FORBIDDEN/12/index read-only / allow delete (api)];"
    },
    "status": 403
}

这个错的意思呢,就是你的索引库为只读状态。

解决方法:

配置文件后加上这句:

cluster.routing.allocation.disk.threshold_enabled: false

执行完了之后需要重启ES。

接下来,我们把只读状态改了:

**使用PUT提交:

http://127.0.0.1:9200/索引名称/_settings

**

{
"index.blocks.read_only_allow_delete": null
}

或者将其指定为false。自己根据自己需要来。

发送post请求时

这个时候不带id时

** 响应**

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

** "_id": "5SfpXIUB7T24Ga4R2BS6":返回一个随机的id**

** 再一次发送不带id的post请求**

** 响应**

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

我们可以发现这个时候也是新增,返回了一个随机的id

** 但是如果带了id发送post请求那么效果跟put请求的效果是一样的**

总结

** PUT 和 POST 都可以,**

POST 新增。如果不指定 id,会自动生成 id。指定 id 就会修改这个数据,并新增版本号

PUT 可以新增可以修改。PUT 必须指定 id;由于 PUT 需要指定 id,我们一般都用来做修改 操作,不指定 id 会报错。

3、查询文档

GET customer/external/1

http://192.168.107.129:9200/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"
            }
}

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

if_seq_no=0乐观锁的版本号,****当不符合版本号的时候报409

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

4、更新文档

带_update或put更新的post请求

POST customer/external/1/_update

{
     "doc":{ 
             "name": "John123"
          }
}

响应 :

{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "_version": 5,
    "result": "updated",//注意这个地方是updated的更新
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 8,
    "_primary_term": 5
}

json内容不变再一次发送响应:

{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "_version": 7,
    "result": "noop",//再一次发送的时候不是更新了
    "_shards": {
        "total": 0,
        "successful": 0,
        "failed": 0
    },
    "_seq_no": 10,
    "_primary_term": 5
}

post带_update对比原来的数据,与原来的一样什么都不做version和sql_no也不会改变

不带_update或put更新的post请求

POST customer/external/1

{ 
    "name": "John Doe2"
}

不会去比对信息

PUT customer/external/1

同理

总结

不同:POST 操作会对比源文档数据,如果相同不会有什么操作,文档 version 不增加

**PUT 操作总会将数据重新保存并增加 version 版本; **

** 带_update 对比元数据如果一样就不进行任何操作。**

** 看场景;**

** 对于大并发更新,不带 update;**

** 对于大并发查询偶尔更新,带 update;对比更新,重新计算分配规则**

更新同时增加属性

POST customer/external/1/_update

{
     "doc": { "name": "Jane Doe", "age": 20 }
}

**PUT 和 POST 不带_update 也可以 **

5、删除文档&索引

DELETE customer/external/1

响应:

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

DELETE customer

响应

{
    "acknowledged": true
}

不存在删除类型type的玩意

6、bulk 批量 API

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

语法格式:

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

**复杂的例子: **

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 updated blog post"} }

**响应 **

#! Deprecation: [types removal] Specifying types in bulk requests is deprecated.
{
  "took" : 137,
  "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" : "5ycmXYUB7T24Ga4RRRSB",
        "_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
      }
    }
  ]
}

本文转载自: https://blog.csdn.net/m0_62436868/article/details/128479917
版权归原作者 一个风轻云淡 所有, 如有侵权,请联系我们删除。

“Es初步检索命令”的评论:

还没有评论