• ELK ----elasticsearch笔记增删改查等


    简单的增删改查,检查集群健康 ,查看所有索引

    #检查集群健康
    GET /_cat/health?v
    
    #返回所有索引
    GET /_cat/indices?v
    
    
    #创建索引
    PUT /demo01?pretty
    
    #删除索引
    DELETE /demo01
    
    
    PUT /book
    
    #自动生成主键
    # id不会冲突
    POST /test_index/_doc{ 
      "test_field": "test1"
    }
    
    #放一条数据 doc:对文档(document)操作
    #对数据的改动会使version自增 
    PUT /book/_doc/1 
    {
    "name": "Bootstrap开发",
    "description": "Bootstrap是由Twitter推出的一个前台页面开发css框架,是一个非常流行的开发框架,此框架集成了多种页面效果。此开发框架包含了大量的CSS、JS程序代码,可以帮助开发者(尤其是不擅长css页面开发的程序人员)轻松的实现一个css,不受浏览器限制的精美界面css效果。",
    "studymodel": "201002",
    "price":38.6,
    "timestamp":"2019-08-25 19:11:35",
    "pic":"group1/M00/00/00/wKhlQFs6RCeAY0pHAAJx5ZjNDEM428.jpg",
    "tags": [ "bootstrap", "dev"]
    }
    
    #查询文档操作
    GET /book/_doc/1
    
    
    #修改文档操作,整个全局替换!! 替换成全新的
    PUT /book/_doc/1
    {
    "name": "spring开发"
    }
    
    #局部替换
    POST /book/_update/1/ 
    {
      "doc": {
       "name": " Bootstrap开发教程高级"
      }
    }
    #删除doc
    DELETE /book/_doc/1
    
    #获取指定索引字段的信息
    GET /book/_doc/1?_source_includes=name,price
    
    
    
    #强制创建:如果数据库里有了该索引就不创建了
    PUT /book/_doc/4/_create
    {
    "name": "goline开发",
    "description": "Bootstrap是由Twitter推出的一个前台页面开发css框架,是一个非常流行的开发框架,此框架集成了多种页面效果。此开发框架包含了大量的CSS、JS程序代码,可以帮助开发者(尤其是不擅长css页面开发的程序人员)轻松的实现一个css,不受浏览器限制的精美界面css效果。",
    "studymodel": "201002",
    "price":38.6,
    "timestamp":"2019-08-25 19:11:35",
    "pic":"group1/M00/00/00/wKhlQFs6RCeAY0pHAAJx5ZjNDEM428.jpg",
    "tags": [ "bootstrap", "dev"]
    }
    
    #lazy delete :先不删除标记为delete 攒者一块删除
    #当你删除id =1 的数据 时候version =1 ,然后再 新增id =1 的数据 version会奇迹般的变为 2,
    #由此证明了并不是真正的删除 ,而是延迟删除
    DELETE /book/_doc/1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76

    不同数据需要放入不同索引

    防止吞吐量大的索引影响吞吐量小的索引 。索引都是在一个个分片上的

    全量替换和局部更新的优缺点

    在这里插入图片描述
    在这里插入图片描述

    使用脚本更新

    
    #原始字段更新加一
    #先查再更新
    POST /test_index/_update/1
    {
      "doc":{
        "num":1
      }
    }
    
    GET /test_index/_doc/1
    
    # 使用脚本直接更新
    POST /test_index/_update/1
    {
      "script": "ctx._source.num+=1"
    }
    
    GET /test_index/_search
    {
      "script_fields": {
        "my_doubled_field": {
          "script": {
           "lang": "expression",
            "source": "doc['num'] * multiplier",
            "params": {
              "multiplier": 2
            }
          }
        }
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    ES 的并发问题

    基于乐观锁和悲观锁进行解释,和java中的乐观锁悲观锁差不多
    在这里插入图片描述

    mysql 利用乐观锁加版本号解决并发读写问题,很容易造成用户数据修改不成功
    es增删改都是基于版本号的

    ES 主从同步

    在这里插入图片描述

    带上版本号进行更新

    PUT /test_index/_doc/5
    {
      "test_field": "itcast"
    }
    
    
    GET /test_index/_doc/5
    
    # 带上版本号进行更新
    # 版本问题。Elasticsearch在6.7版本后不支持version#=1的参数指定方式,我本地测试使用7.4.2
    PUT /test_index/_doc/5?if_seq_no=6&if_primary_term=1
    {
      "test_field": "itcast1"
    }
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    自己手动控制版本号 external version

    修改时external version要大于当前文档的_version

    自己手动控制的版本号必须大于当前版本号 不然报错
    
    PUT /test_index/_doc/5?version=10&version_type=external
    {
      "test_field": "itcast1"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    更新时指定重试次数

    ##更新不成功,不能使用 version_type=external
    POST /test_index/_doc/5/_update?retry_on_conflict=3&version=22&version_type=external
    {
      "doc": {
        "test_field": "itcast1"
      }
    }
    #提示
    {
      "error": {
        "root_cause": [
          {
            "type": "action_request_validation_exception",
            "reason": "Validation Failed: 1: internal versioning can not be used for optimistic concurrency control. Please use `if_seq_no` and `if_primary_term` instead;"
          }
        ],
        "type": "action_request_validation_exception",
        "reason": "Validation Failed: 1: internal versioning can not be used for optimistic concurrency control. Please use `if_seq_no` and `if_primary_term` instead;"
      },
      "status": 400
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    批量更新

    #指定 _index 和_id 可以使用不同的索引
    GET /_mget
    { 
       "docs" : [
          {
             "_index" : "book",
             "_id" :    1
          },
          {
             "_index" : "test_index",
             "_id" :    5
          }
       ]
    }
    
    GET /test_index/_mget
    {
       "docs" : [
          {
             "_id" :    2
          },
          {
             "_id" :    3
          }
       ]
    }
    
    GET /book/_search
    {
      "query": {
        "ids": {
          "values": [1,2,3]
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    批量增删改 bulk

    POST /_bulk
    {"action": {"metadata"}}
    {"data"}
    
    POST /_bulk
    { "delete": { "_index": "test_index",  "_id": "5" }} 
    { "create": { "_index": "test_index",  "_id": "14" }}
    { "test_field": "test14" }
    { "update": { "_index": "test_index",  "_id": "2"} }
    { "doc" : {"test_field" : "bulk test"} }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  • 相关阅读:
    Spring源码解析——IOC属性填充
    Flutter:手势识别组件
    字符串流与文件操作——(学习笔记)
    基于WEB+APP的模拟工业仪器的监控预警系统
    数据仓库应该用什么方案——数据仓库实施方案概述
    C语言char与short取反以及符号判断问题
    35【Aseprite 作图】苹果——拆解
    警惕!出现这些表现,你的亲人/朋友正在认真考虑自杀
    【密码学】Java实现DH函数时出现“Unsupported secret key algorithm: AES“错误
    spark sql重分区
  • 原文地址:https://blog.csdn.net/weixin_45699541/article/details/126372298