首页 > 数据库 >ES 实战复杂sql查询、修改字段类型

ES 实战复杂sql查询、修改字段类型

时间:2023-07-15 13:22:04浏览次数:31  
标签:实战 fields keyword text ignore syslog sql type ES

转载请注明出处:

1.查询索引得 mapping 与 setting

 

  get 直接查询 索引名称时,会返回 该 索引得 mapping 和 settings 得配置,上述返回得结构如下:

{
  "terra-syslog_2023-07-12" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "@timestamp" : {
          "type" : "date"
        },
        "@version" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "host" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "message" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "received_at" : {
          "type" : "date"
        },
        "received_from" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "syslog_facility" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "syslog_facility_code" : {
          "type" : "long"
        },
        "syslog_hostname" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "syslog_message" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "syslog_program" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "syslog_severity" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "syslog_severity_code" : {
          "type" : "long"
        },
        "syslog_timestamp" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "tags" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "type" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "user" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1689137630855",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "Qew4uoNUQ9q8-JQDPTWVPw",
        "version" : {
          "created" : "7080199"
        },
        "provided_name" : "terra-syslog_2023-07-12"
      }
    }
  }
}
View Code

2. 执行复杂条件得查询:

   该dsl 为:

GET terra-syslog_2023-07-15/_search

{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "wildcard": {
            "syslog_program.keyword": {
              "wildcard": "*SSH_USER_LOGIN*",
              "boost": 1
            }
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  },
  "aggregations": {
    "time_agg": {
      "date_histogram": {
        "field": "received_at",
        "format": "EEE",
        "fixed_interval": "1d",
        "offset": 0,
        "order": {
          "_key": "asc"
        },
        "keyed": false,
        "min_doc_count": 0
      },
      "aggregations": {
        "user_agg": {
          "terms": {
            "field": "user.keyword",
            "size": 10,
            "min_doc_count": 1,
            "shard_min_doc_count": 0,
            "show_term_doc_count_error": false,
            "order": [
              {
                "_count": "desc"
              },
              {
                "_key": "asc"
              }
            ]
          }
        }
      }
    }
  }
}

这段 DSL 具有以下作用:

  1. "size": 0: 设置返回的结果集大小为 0,即只返回聚合结果,不返回匹配的文档。

  2. query 部分:构建了一个布尔查询,包含多个 existsrange 子查询,用于过滤符合条件的文档。

    • exists 子查询检查指定字段是否存在,这里依次检查了 source.ipsource.portdestination.ipdestination.porthost.nameflow.rep_tags 字段的存在。

    • range 子查询指定了对 @timestamp 字段进行范围筛选,从给定的时间戳范围中选择满足条件的文档。

  3. aggregations 部分:定义了聚合操作,通过 terms 聚合按照 host.name 字段进行分组,并计算每个组内的文档数。

    • terms 聚合将按照 host.name 字段的值进行分组。设置 size 为最大整数 2147483647,以确保返回所有分组。

    • min_doc_count 设置为 1,表示只返回至少拥有一个文档的分组。

    • shard_min_doc_count 设置为 0,表示在单个分片上没有要求文档数量的最小要求。

    • show_term_doc_count_error 设置为 false,不显示术语文档计数错误。

    • order 指定了排序规则,首先按照分组中的文档数 _count 降序排序,然后按照 host.name 字段的值升序排序。

    • terms 聚合内部定义了一个子聚合 cardinality,用于计算每个分组内唯一组合的数量。这里通过拼接 source.ipsource.portdestination.ipdestination.port 字段的值来作为唯一标识。

  该 DSL 查询的作用是在给定时间范围内,统计满足一系列条件(存在指定字段)的文档,并按照 host.name 进行分组并计算每个组内唯一组合的数量。

  另外,在查询时,使用 _search 可以执行DSL, 如果没有_search 时,可以查询该索引得文档结构类型,以及该索引得副本、分片等信息

3.修改 该 索引得 mapping 中得字段类型

  将前面的映射中的 syslog_timestamp 字段类型修改为日期类型(date),需要更新映射定义并重新创建索引。  

  1. 删除现有的索引,或者创建一个新的索引。

  2. 更新映射定义,将 syslog_timestamp 的类型更改为 "date"。以下是更新后的映射示例:

{
  "mappings": {
    "_doc": {
      "properties": {
        // 其他字段...
        "syslog_timestamp": {
          "type": "date"
        },
        // 其他字段...
      }
    }
  }
}
  1. 使用上述修改后的映射定义来创建索引或更新现有索引的映射。可以使用 Elasticsearch 的 RESTful API 或管理工具(如 Kibana Console)执行以下请求:
PUT terra-syslog_2023-07-15
{
  "mappings": {
    "_doc": {
      "properties": {
        // 其他字段...
        "syslog_timestamp": {
          "type": "date"
        },
        // 其他字段...
      }
    }
  }
}

  这样,syslog_timestamp 字段的类型就会被修改为日期类型,并可以存储、索引和查询日期值。根据数据的格式和需求,Elasticsearch 会自动解析日期字符串并将其转换为适当的日期对象。

 

标签:实战,fields,keyword,text,ignore,syslog,sql,type,ES
From: https://www.cnblogs.com/zjdxr-up/p/17556004.html

相关文章

  • idea进行maven打包的时候报错Cannot create resource output directory
    今天在进行maven打包的时候报错了Cannotcreateresourceoutputdirectory:XXXXX,之前遇到过,但是之前选择了重启就好了,这次真不想重启,有很多需要保存的shell页面原因呢就是target被占用,导致打包不了。但是我仔仔细细检查了,真的没这情况啊,没有任何文件夹占用了然后各种看,最后,在......
  • mysql修改所有表的编码排序规则
    #查询数据库各表的排序规则SELECTTABLE_NAME,TABLE_COLLATIONFROMINFORMATION_SCHEMA.TABLESWHERETABLE_SCHEMA='database'; #查询要修改排序规则表的SQL语句SELECTconcat('ALTERTABLE',TABLE_NAME,'CONVERTTOCHARACTERSETutf8mb4COLLATEutf8mb4_unicod......
  • Codeforces Round 881 (Div. 3) D - Apple Tree(dfs)
    https://codeforces.com/contest/1843/problem/D题目大意:一颗树中,每次给定两个结点,每个结点都可以移动到孩子结点,最后可以到达叶子结点,问我们这两个结点最终移到叶子结点有多少种组合?(其实就是让求以这两个节点为根的子树的叶子结点个数的乘积)input2512345332......
  • mysql使用记录
    mysql一些实际使用记录查看数据库showdatabases;选择某个数据库usexxxxx;创建数据库createdatabasetestdb;选择某个数据库后,查看该数据库下有那些表showtables;查看当前正在使用的数据库selectdatabase();表数据库当中最基本的单元是表:table;表中每一个字......
  • SQL语句执行顺序
    selectdistinct查询列表(要查的字段)from左边的表们s连接类型(left|inner)join右边的表们son连接条件where筛选条件groupby分组的列表(按什么字段分组)havinghaving_conditionorderby排序的字段limitlimitnumber;1f......
  • Angular Schematics 实战 - 项目根目录新建一个文件
    AngularSchematics是一个由Angular团队提供的工作流工具,用于在Angular应用程序中自动化开发过程。Schematics可以创建一个新的Angular应用,生成简单或复杂的代码片段,或者修改现有代码以添加新功能或行为。它提供了一个可扩展的方法,允许开发者以一种可维护的方式定制或扩展......
  • SAP ABAP 函数 TR_REQUEST_CHOICE
    TR_REQUEST_CHOICE是SAPABAP中的一个函数模块,它用于在系统中处理传输请求。传输请求是SAP系统中的一个重要概念,它用于管理和控制系统中对象的传输。这些对象可以是程序、表、视图等。TR_REQUEST_CHOICE函数模块提供了一种界面,允许用户在系统中选择一个传输请求。它有一个......
  • 关于 SAP ABAP 事务码 SM30 里的 Restrict Data Range
    SAPABAP事务码SM30里的RestrictDataRange区域的Enterconditions和Variant这两个选项有什么作用?SAPABAP中的SM30事务码用于维护表的条目。在使用SM30事务时,RestrictDataRange区域允许用户定义一些限制条件,可以帮助缩小查询或更改的数据范围。这对于大型表......
  • DockerDeskTop系列---【启动DockerDeskTop时报错:Please try shutting WSL down (wsl
    DockerDeskTop安装完成后,无法启动报错信息如下:PleasetryshuttingWSLdown(wsl--shutdown)and/orrebootingyourcomputer.Ifnotsufficient,WSLmayneedtobereinstalledfully.Asalastresort,trytouninstall/reinstallDockerDesktop.解决方案:以管......
  • Mysql导入sql脚本报错,时间格式数据为空
    Mysql导入sql脚本报错,时间格式数据为空Mysql导入sql脚本时候导入的sql脚本中有时间格式为空的时候时间格字段会报错。解决方式也很简单:查看sql_mode:select@@session.sql_mode;查询结果:STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,N......