Elasticsearch 之入门

短命女 2023-10-07 10:01 158阅读 0赞

Elasticsearch入门级介绍

  • 术语介绍
  • Document
  • Index
  • 如何创建索引和写入数据
    • Rest API
    • Index API
    • Document API

术语介绍

  • 文档 Document:用户存储在es 中的数据文档,es中存储的最小单元,类似于MySQL表中的一行数据
  • 索引 Index:由具有相同字段的文档列表组成,表示一个文档的集合,类似于table,在6.0后一个Index下只有一个type。(在6后的版本会把type去除)
  • 节点 Node:一个Elasticsearch的运行实例,是集群的构成单元
  • 集群 Cluster:由一个或多个节点组成,对外提供服务

Document

Json Object,由字段(Field)组成,常见数据类型如下:

  • 字符串:text, keyword
  • 数值型:long, integer, short, byte, double, float, half_float, scaled_float
  • 布尔:boolean
  • 日期:date
  • 二进制:binary
  • 范围类型:integer_range, float_range, long_range, double_range, date_range

每一个文档都有唯一的id标识

  • 自行指定
  • es自动生成

Demo

  1. 93.180.71.3 --[17/May/2019:08:21:02 +0000] "GET/downloads/product_1 HTTP/1.1" 304 0 "-" "Debian APT-HTTP/1.3 (0.8.16~exp12ubuntu10.21)"

以上日志记录到es 后:

  1. {
  2. "remote_ip":"93.180.71.3",
  3. "user_name":"-",
  4. "@timestamp":"2019-05-17T08:21:02.000Z",
  5. "request_action":"GET",
  6. "request":"/downloads/product_1",
  7. "http_version":"1.1",
  8. "response":"304",
  9. "bytes":"0",
  10. "referrer":"-",
  11. "agent":"Debian APT-HTTP/1.3 (0.8.16~exp12ubuntu10.21)"
  12. }

MetaData(元数据),用于标注文档的相关信息

  • _index:文档所在的索引名
  • _type:文档所在的类型名
  • _id:文档唯一id
  • _uid:组合id,由 _type和 _id组成(6.x _type不再起作用,这里同_id一样)
  • _source:文档的原始Json数据,可以从这里获取每个字段的内容
  • _all:整合所有字段内容到该字段,默认禁用(很占磁盘空间)

Index

  • 索引中存储具有相同结构的文档(Document);每个索引都有自己的mapping定义,用于定义字段名和类型
  • 一个集群可以有多个索引,比如:nginx日志存储的时候可以按照日期每天生成一个索引来存储
    – nginx-log-2019-01-01
    – nginx-log-2019-01-02
    – nginx-log-2019-01-03

如何创建索引和写入数据

Rest API

Elasticsearch 集群对外提供 RESTful API

  • REST – REpresentational State Transfer
  • URI 指定资源,如 Index、Document 等
  • Http Method 指明资源操作类型,如 GET、POST、PUT、DELETE等

常用两种交互方式

  • Curl 命令行

    1. curl -X PUT 'http://localhost:9200/employee/doc/1' -i -H "Content-Type: application/json" -d '{
    2. "username": "bear"
    3. "job": "php"
    4. }'
  • Kibana DevTools
    在这里插入图片描述

Index API

用于创建、更新、删除索引配置等

  • 创建索引如下:

    1. PUT /user_index

    在这里插入图片描述

  • 查看现有索引

    1. GET _cat/indices

    在这里插入图片描述

  • 删除索引如下:

    1. DELETE /user_index

    在这里插入图片描述

Document API

Elasticsearch 有专门的 Document API

  • 创建文档
    1、指定 id 创建文档(【创建文档时,如果索引不存在,es会自动创建对应的index(test_index)和type(doc)】

    1. PUT /test_index/doc/1
    2. {
    3. "username": "bear",
    4. "job": "php"
    5. }

    在这里插入图片描述
    2、不指定 id 创建文档

    1. POST /test_index/doc
    2. {
    3. "username": "tom",
    4. "job": "java"
    5. }

    在这里插入图片描述
    3、批量创建文档API — es允许一次创建多个文档,从而减少网络传输开销,提升写入速率;-endpoint为【_bulk】,bulk 每一行都是一个Json,第一行指定了操作的原始信息,如下:
    其中:【index(创建文档当文档已存在会覆盖)、create(创建文档当文档已存在会报错)、delete、update】属于action_type(操作要做什么)

    1. POST _bulk
    2. {
    3. "index":{
    4. "_index":"test_index","_type":"doc","_id":"2"}}
    5. {
    6. "username":"hebe","job":"web"}
    7. {
    8. "delete":{
    9. "_index":"test_index","_type":"doc","_id":"1"}}
    10. {
    11. "update":{
    12. "_index":"test_index","_type":"doc","_id":"2"}}
    13. {
    14. "doc":{
    15. "job":"web-vue"}}
    16. POST my_index/doc/_bulk
    17. {
    18. "index":{
    19. "_id": "1"
    20. }
    21. }
    22. {
    23. "username": "alfred way",
    24. "job": "java",
    25. "age": 18,
    26. "birth": "1990-02-02",
    27. "isMarried": false
    28. }
    29. {
    30. "index":{
    31. "_id": "2"
    32. }
    33. }
    34. {
    35. "username": "alfred",
    36. "job": "java and php specialist",
    37. "age": 28,
    38. "birth": "1990-02-02",
    39. "isMarried": true
    40. }
    41. 返回信息:
    42. {
    43. "took": 21,
    44. "errors": false, //批量操作是否成功
    45. "items": [ //每个bulk操作的返回结果
    46. {
    47. "index": {
    48. "_index": "test_index",
    49. "_type": "doc",
    50. "_id": "2",
    51. "_version": 1,
    52. "result": "created",
    53. "_shards": {
    54. "total": 2,
    55. "successful": 1,
    56. "failed": 0
    57. },
    58. "created": true,
    59. "status": 201
    60. }
    61. },
    62. {
    63. "delete": {
    64. "found": true,
    65. "_index": "test_index",
    66. "_type": "doc",
    67. "_id": "1",
    68. "_version": 2,
    69. "result": "deleted",
    70. "_shards": {
    71. "total": 2,
    72. "successful": 1,
    73. "failed": 0
    74. },
    75. "status": 200
    76. }
    77. },
    78. {
    79. "update": {
    80. "_index": "test_index",
    81. "_type": "doc",
    82. "_id": "2",
    83. "_version": 2,
    84. "result": "updated",
    85. "_shards": {
    86. "total": 2,
    87. "successful": 1,
    88. "failed": 0
    89. },
    90. "status": 200
    91. }
    92. }
    93. ]
    94. }
  • 查询文档
    1、指定要查询的文档id

    1. GET /test_index/doc/1

    查询存在时:其中【_source存储了文档完整的原始数据】
    在这里插入图片描述
    查询未找到时:
    在这里插入图片描述
    2、搜索所有文档,用到【_search】,查询语句,Json格式,放在http_body中发送到es:

    1. GET /test_index/doc/_search
    2. {
    3. "query":{
    4. "term":{
    5. "_id":"1"
    6. }
    7. }
    8. }

    took
    返回说明:

    1. {
    2. "took": 3, //查询耗时,单位ms
    3. "timed_out": false,
    4. "_shards": {
    5. "total": 5,
    6. "successful": 5,
    7. "skipped": 0,
    8. "failed": 0
    9. },
    10. "hits": {
    11. "total": 1, //符合条件的总文档数
    12. "max_score": 1,
    13. "hits": [ //返回的文档详情数据数组,默认前10个文档
    14. {
    15. "_index": "test_index", //索引名
    16. "_type": "doc",
    17. "_id": "1", //文档id
    18. "_score": 1, //文档的得分
    19. "_source": {
    20. //文档详情
    21. "username": "bear",
    22. "job": "php"
    23. }
    24. }
    25. ]
    26. }
    27. }

    3、批量查询文档API — es允许一次查询多个文档;-endpoint为【_mget】,可获取不同 index 的文档,如下:

    1. GET /_mget
    2. {
    3. "docs":[
    4. {
    5. "_index":"test_index",
    6. "_type":"doc",
    7. "_id":"1"
    8. },
    9. {
    10. "_index":"test_index",
    11. "_type":"doc",
    12. "_id":"2"
    13. }
    14. ]
    15. }

    在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,158人围观)

还没有评论,来说两句吧...

相关阅读

    相关 ElasticSearch入门ELK简介 02

    前言 日志,对于任何系统来说都是及其重要的组成部分。在计算机系统里面,更是如此。但是由于现在的计算机系统大多比较复杂,很多系统都不是在一个地方,甚至都是跨国界的;即使是在