Elasticsearch7基础2-文档基础操作

浏览器:127.0.0.1:5601 找到Dev Tools

1.集群运行状况检查

GET /_cat/health?v

2.获取集群中的节点列表

GET /_cat/nodes?v

3.列出所有索引

GET /_cat/indices?v

index索引操作
1.1 新建 Index,可以直接向 Elastic服务器发出PUT请求

PUT /goods

创建了一个goods的Index
服务器返回结果:

{
  "acknowledged" : true, #表示成功
  "shards_acknowledged" : true,
  "index" : "goods"
}

命名规则:

仅小写
不能包括\ / * ? " < > | ``(空格字符) , #
7.0之前的索引可能包含冒号: ,但已过时,并且在7.0+中不支持
不能以 - _ + 开始
不能为.或..
不能超过255个字节

1.2 删除索引
delete请求 删除index

DELETE /goods

文档操作
1.3 创建文档
1.3.1 手动指定id, body必填

PUT /book/_doc/1
{
  "name":"书名",
  "desc":"简介",
  "studymodel":"201201",
  "price": "13.8",
  "time": "2024-01-02 12:23:00",
  "pic":"/ugc/aa.png",
  "tags":["boots", "dev"]
}

返回结果:


1.3.2 查看文档

GET /book/_doc/1

返回结果:

{
  "_index": "book",
  "_id": "1",
  "_version": 1,
  "_seq_no": 7,
  "_primary_term": 1,
  "found": true,
  "_source": {
    "name": "书名",
    "desc": "简介1",
    "studymodel": "201201",
    "price": "13.8",
    "time": "2024-01-02 12:23:00",
    "pic": "/ugc/aa.png",
    "tags": [
      "boots",
      "dev"
    ]
  }
}

1.3.3 更新文档
方式1:全局更新

PUT /book/_doc/1
{
  "name":"书名",
  "desc":"简介1",
  "studymodel":"201201",
  "price": "13.8",
  "time": "2024-01-02 12:23:00",
  "pic":"/ugc/aa.png",
  "tags":["boots", "dev"]
}

方式2:指定字段更新

POST /book/_update/1
{
  "doc": {
  "desc":"简介1"
  }
}

1.4 删除

DELETE /book/_doc/1

1.5 自动生成id,自动id特点:长度为20个字符,URL安全,base64编码,GUID,分布式生成不冲突.

POST /test_file/_doc
{
  "test_field": "test"
}

返回结果:

{
  "_index": "test_file",
  "_id": "UAaMUY4BHZmdkzBcIILZ",
  "_version": 1,
  "_seq_no": 1,
  "_primary_term": 1,
  "found": true,
  "_source": {
    "test_field": "test"
  }
}

1.6 _source 字段:插入数据时的所有字段和值。在get获取数据时,会在_source字段中返回原数据
1.6.1 返回制定字段
查询指定字段 类似select name,age from

GET /book/_doc/1?_source_includes=name,price

返回结果:

{
  "_index": "book",
  "_id": "1",
  "_version": 1,
  "_seq_no": 7,
  "_primary_term": 1,
  "found": true,
  "_source": {
    "name": "书名",
    "price": "13.8"
  }
}

文档的替换与删除
1.1 全量替换

PUT /book/_doc/1
{
  "name":"书名",
  "desc":"简介1",
  "studymodel":"201201",
  "price": "13.8",
  "time": "2024-01-02 12:23:00",
  "pic":"/ugc/aa.png",
  "tags":["boots", "dev"]
}

执行两次,返回结果中版本号(_version)在不断上升
实质:旧文档的内容不会立即删除,只是标记为deleted,适当时机集群会自动删除文档

1.2 强制创建
为了防止原有数据被覆盖,在新增时设置为强制创建,不会覆盖原有文档 方法:put //_create/<_id>

PUT /test_file_1/_create/1
{
  "test_field": "test_create"
}

返回结果:

{
  "_index": "test_file_1",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}

再次创建时返回结果

{
  "error": {
    "root_cause": [
      {
        "type": "version_conflict_engine_exception",
        "reason": "[1]: version conflict, document already exists (current version [1])",
        "index_uuid": "GzF8IKezSTWKW0ZlEDdDAw",
        "shard": "0",
        "index": "test_file_1"
      }
    ],
    "type": "version_conflict_engine_exception",
    "reason": "[1]: version conflict, document already exists (current version [1])",
    "index_uuid": "GzF8IKezSTWKW0ZlEDdDAw",
    "shard": "0",
    "index": "test_file_1"
  },
  "status": 409
}

1.3 删除
DELETE //_doc/<_id>
实质:旧文档的内容不会立即删除,只是标记为deleted,适当时机集群会自动删除文档
1.4 局部更新
方法:

post /<index>/_update/<_id>
{
  "doc": {
    "field":"value"
  }
}

eg:

POST /book/_update/1
{
  "doc": {
  "desc":"简介1"
  }
}

返回结果:

{
  "_index": "book",
  "_id": "1",
  "_version": 2,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 8,
  "_primary_term": 1
}

内部与全量替换是一样的,旧文档标记为删除,新建一个文档。
优点:大大减少网络传输次数和流量,提升性能;减少并发冲突发生的概率

脚本更新
1.1 内置脚本更新
比如一个值的自增

PUT /test_file_2/_doc/1
{
  "num": 0
}

实现num 自增1

POST /test_file_2/_update/1
{
  "script": "ctx._source.num+=1"
}

查询 GET /test_file_2/_doc/1
返回结果

{
  "_index": "test_file_2",
  "_id": "1",
  "_version": 2,
  "_seq_no": 2,
  "_primary_term": 1,
  "found": true,
  "_source": {
    "num": 1
  }
}
posted @ 2024-03-18 20:54  py卡卡  阅读(9)  评论(0编辑  收藏  举报