ES数据库学(2),2024年最新程序员工作2年月薪12K

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新大数据全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip204888 (备注大数据)
img

正文

5、分词器

  • ES的默认分词设置是standard,单字拆分

了ik分词器,有两种

ik_smart会将“清华大学”整个分为一个词

ik_max_word 会将“清华大学”分为“清华大学”,“清华”和“大学”

6、文档

6.1、简介

es是面向文档的,文档是es中可搜索的最小单位,es的文档由一个或多个字段组成,类似于关系型数据库中的一行记录

6.2、元数据
  • _index:文档所在索引名称
  • _source:原始json数据
  • _type:文档所属类型,es7.0以后只有为 _doc
  • _version:文档版本,如果对文档进行修改,则该字段会增加
  • _score:相关性打分
  • id:文档唯一id

7、字段类型

  • 核心数据类型
    • 字符串: text,keyword(不分词,智能使用完整词搜索)
    • 数值型:long,integer,short,byte,double,float,half_float,scaled_float
    • 布尔类型:boolean
    • 二进制: binary(二进制经过base64编码之后的字符串,不可搜索)
    • 范围类型:integer_range,float_range,long_range,double_range,date_rage
    • 日期: date
  • 复杂数据类型
    • 数组,Array: es并无专门的数组类型,可以在插入文档的时候直接声明一个数组插入即可,([1,2]-整形数组,[”1“,”2“]-字符串数组,[{"name":"nick"},{"name": "elaine"}])
    • 对象,Object:
  • 专用数据类型,如IP;

命令

环境:

​ kibana—>控制台

1、基本命令

methodurl地址描述
PUTlocalhost:9200/索引名称/类型名称/文档id创建文档(指定文档id)
POSTlocalhost:9200/索引名称/类型名称创建文档(随机文档id)
POSTlocalhost:9200/索引名称/类型名称/文档id/_update修改文档
DELETElocalhost:9200/索引名称/类型名称/文档id删除文档
GETlocalhost:9200/索引名称/类型名称/文档id查询文档通过文档id
POSTlocalhost:9200/索引名称/类型名称/_search查询所有数据
  1. 字符串类型
    textkeyword
  2. 数值类型
    long, integer, short, byte, double, float, half_float, scaled_float
  3. 日期类型
    date
  4. te布尔值类型
    boolean
  5. 二进制类型
    binary
  6. 范围类型
    integer_range , float_range, long_range, double_range, date_range
1.1、查询所有_cat命令
GET /_cat

1.2、查询es集群服务健康状态
GET /_cat/health?v

结果:

epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1684116543 02:09:03  my-application yellow       1         1     15  15    0    0        2             0                  -                 88.2%


cluster ,集群名称
status,集群状态 green代表健康;yellow代表分配了所有主分片,但至少缺少一个副本,此时集群数据仍旧完整;red代表部分主分片不可用,可能已经丢失数据。
node.total,代表在线的节点总数量
node.data,代表在线的数据节点的数量
shards, active_shards 存活的分片数量
pri,active_primary_shards 存活的主分片数量 正常情况下 shards的数量是pri的两倍。
relo, relocating_shards 迁移中的分片数量,正常情况为 0
init, initializing_shards 初始化中的分片数量 正常情况为 0
unassign, unassigned_shards 未分配的分片 正常情况为 0
pending_tasks,准备中的任务,任务指迁移分片等 正常情况为 0
max_task_wait_time,任务最长等待时间
active_shards_percent,正常分片百分比 正常情况为 100%

1.3、查看es节点信息
GET /_cat/nodes?v

1.4、查看es指定节点信息-node-1
GET /_nodes/nodeName?pretty=true
示例:GET /_nodes/node-1?pretty=true

2、索引命令

2.1、查看es中所有索引
GET /_cat/indices?v

结果:

health status index                   uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   index_1     rerwerwrewrwrwe  20   1        208            0      1.1mb        609.8kb
green  open   index_2     eewfdsffhwehfoeif3  30   1          4            1    222.4kb        111.2kb

health:  green代表健康;yellow代表分配了所有主分片,但至少缺少一个副本,此时集群数据仍旧完整;red代表部分主分片不可用,可能已经丢失数据。
pri:primary缩写,主分片数量
rep:副分片数量
docs.count: Lucene 级别的文档数量
docs.deleted: 删除的文档
store.size:全部分片大小(包含副本)
pri.store.size:主分片大小

2.2、新建索引
PUT /test

成功返回

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "test"
}

demo1:

#自定义类型 type
PUT /test
{
  "mappings": {
    "properties": {
      "info": {
        "type": "text",
        "analyzer": "ik_smart"  #analyzer分词器选择
      },
      "email": {
        "type": "keyword", #字段类型
        "index": false
      },
      "name": {
        "properties": {
          "firstName": {
            "type": "keyword"
          },
          "lastName": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

demo2

#-----------用户user-----------------
#不自定义类型
PUT /user

#不自定义类型 会默认配置 如字段类型 分片 以及id
PUT /user/_doc/1
{
  "name":"张三",
  "age":10,
  "sex":"男",
  "address":"江苏苏州"
}

GET /user/_search

#批量创建文档数据
POST _bulk
{"create":{"_index":"user", "_type":"_doc", "_id":2}}
{"id":2,"name":"李四","age":"20","sex":"男","address":"苏州园区"}
{"create":{"_index":"user", "_type":"_doc", "_id":3}}
{"id":3,"name":"王芳","age":"30","sex":"女","address":"园区华为"}
{"create":{"_index":"user", "_type":"_doc", "_id":4}}
{"id":4,"name":"赵六","age":"40","sex":"女","address":"华为汽车"}

#批量获取文档数据
docs : 文档数组参数
_index : 指定index
_type : 指定type
_id : 指定id
_source : 指定要查询的字段
--------------------------------------------
GET _mget
{
  "docs": [
    {
      "_index": "user",
      "_type": "_doc",
      "_id": 1
    },
    {
      "_index": "user",
      "_type": "_doc",
      "_id": 2
    }
  ]
}

GET /user/_mget
{
  "docs": [
    {
      "_type": "_doc",
      "_id": 1
    },
    {
      "_type": "_doc",
      "_id": 2
    }
  ]
}

GET /user/_doc/_mget
{
  "docs": [
    {
      "_id": 1
    },
    {
      "_id": 2
    }
  ]
}

GET /user/_mget
{
  "docs": [
    {
      "_id": 1
    },
    {
      "_id": 2
    },
    {
      "_id": 3
    },
    {
      "_id": 4
    }
  ]
}

#批量修改文档数据,不存在则创建,存在则替换
POST _bulk
{"index":{"_index":"user", "_type":"_doc", "_id":2}}
{"id":2,"name":"李四","age":"20","sex":"男","address":"苏州园区"}
{"index":{"_index":"user", "_type":"_doc", "_id":3}}
{"id":3,"name":"王芳","age":"30","sex":"女","address":"园区华为"}
{"create":{"_index":"user", "_type":"_doc", "_id":4}}
{"id":4,"name":"赵六","age":"40","sex":"女","address":"华为汽车"}

#批量修改update
POST _bulk
{"update":{"_index":"user","_type":"_doc","_id":2}}
{"doc":{"address":"苏州园区XX"}}
{"update":{"_index":"user","_type":"_doc","_id":3}}
{"doc":{"address":"园区华为XX"}}

#批量删除
POST _bulk
{"delete":{"_index":"user", "_type":"_doc", "_id":3}}
{"delete":{"_index":"user", "_type":"_doc", "_id":4}}



2.3、删除索引,“acknowledged”:true表示删除成功
DELETE /test

2.4、查看索引的统计信息
GET /_stats?pretty

2.5、修改索引

​ 倒排索引结构,一旦数据结构改变(比如改变了分词器),就需要重新创建倒排索引,这简直是灾难。因此索引库一旦创建,无法修改mapping

​ 然无法修改mapping中已有的字段,但是却允许添加新的字段到mapping中,因为不会对倒排索引产生影响。

方法1:覆盖PUT
PUT first/_doc/1
{
  "name":"林",
  "age":18,
  "from":"gu",
  "desc":"念能力,学生,暗属性",
  "tags":["能力者","男","暗"]
}


方法2:更新 POST

​ 使用 POST 命令,在 id 后面跟 _update ,要修改的内容放到 doc 文档(属性)中即可。

POST first/_doc/3/_update 
{
  "doc": {
    "name":"愚者",
    "desc":"塔罗",
    "tags":["魔法","超能力","塔罗"]
  }
}

2.6、插入数据
PUT first/_doc/1
{
  "name":"林",
  "age":18,
  "from":"gu",
  "desc":"念能力",
  "tags":["能力者","学院","男"]
}

PUT first/_doc/2
{
  
  "name":"宝儿姐",
  "age":22,
  "from":"gu", 
  "desc":"道法",
  "tags":["道", "驱魔","女"]
}

2.7、查看索引
2.7.1、查看指定索引
GET /first?pretty  #查看结构

GET /first/_search #查看表内容 select * from first
or
GET /first/_search
{
  "query": {
    "match_all": {}
  }
}

{
  "took" : 787,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "first",
        "_type" : "chunsheng",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "春生",
          "age" : 18,
          "from" : "gu",
          "desc" : "念能力,学生,暗属性",
          "tags" : [
            "能力者",
            "男",
            "暗"
          ]
        }
      },
      {
        "_index" : "first",
        "_type" : "chunsheng",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "愚者",
          "age" : 22,
          "from" : "gu",
          "desc" : "塔罗",
          "tags" : [
            "魔法",
            "超能力",
            "塔罗"
          ]
        }
      },
      {
        "_index" : "first",
        "_type" : "chunsheng",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "宝儿姐",
          "age" : 18,
          "from" : "sheng",
          "desc" : "道法",
          "tags" : [
            "长生",
            "超能力",
            "道法"
          ]
        }
      }
    ]
  }
}


2.7.2、简单查询
GET first/_search?q=from:gu
#使用下面的查询,结果一样 查询条件添加到 match 
GET /first/_search
{
  "query": {
    "match": {
      "from": "gu"
    }
  }
}

结果

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.4700036,
    "hits" : [
      {
        "_index" : "first",
        "_type" : "chunsheng",
        "_id" : "3",
        "_score" : 0.4700036,
        "_source" : {
          "name" : "愚者",
          "age" : 22,
          "from" : "gu",
          "desc" : "塔罗",
          "tags" : [
            "魔法",
            "超能力",
            "塔罗"
          ]
        }
      },
      {
        "_index" : "first",
        "_type" : "chunsheng",
        "_id" : "1",
        "_score" : 0.4700036,
        "_source" : {
          "name" : "春生",
          "age" : 18,
          "from" : "gu",
          "desc" : "念能力,学生,暗属性",
          "tags" : [
            "能力者",
            "男",
            "暗"
          ]
        }
      }
    ]
  }
}

2.7.3、控制返回结果
 _source 来控制仅返回

GET /first/_search
{
  "query": {
    "match_all": {}
  },
      "_source": ["tags","name"]
}

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "first",
        "_type" : "chunsheng",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "宝儿姐",
          "tags" : [
            "长生",
            "超能力",
            "道法"
          ]
        }
      },
      {
        "_index" : "first",
        "_type" : "chunsheng",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "愚者",
          "tags" : [
            "魔法",
            "超能力",
            "塔罗"
          ]
        }
      },
      {
        "_index" : "first",
        "_type" : "chunsheng",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "春生",
          "tags" : [
            "能力者",
            "男",
            "暗"
          ]
        }
      }
    ]
  }
}

2.7.4、排序 sort

desc[倒序] or asc[正序]

GET /first/_search
{
  "query": {
    "match_all": {}
  },
  "_source": ["age","name"],
  "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ]
}

结果:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "first",
        "_type" : "chunsheng",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "name" : "宝儿姐",
          "age" : 18
        },
        "sort" : [
          18
        ]
      },
      {
        "_index" : "first",
        "_type" : "chunsheng",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "name" : "春生",
          "age" : 18
        },
        "sort" : [
          18
        ]
      },
      {
        "_index" : "first",
        "_type" : "chunsheng",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "name" : "愚者",
          "age" : 22
        },
        "sort" : [
          22
        ]
      }
    ]
  }
}

2.7.5、分页查询 from size
GET /first/_search
{
  "query": {
    "match_all": {}
  },
  "_source": ["age","name"],
  "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ],
    "from":0, #第n条开始
    "size":1 #返回多少条数据
}

2.7.6、布尔查询
MUST

“select age,name where first where from=gu and age=18”

GET /first/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "from": "gu"
        }
        },
        {"match": {
          "age": "18"}
        }
      ]
    }
  },
  "_source": ["age","name"],
  "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ]
}

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "first",
        "_type" : "chunsheng",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "name" : "春生",
          "age" : 18
        },
        "sort" : [
          18
        ]
      }
    ]
  }
}

shoud

“select age,name where first where from=gu or age=18”

GET /first/_search
{
  "query": {
    "bool": {
      "should": [
        {"match": {
          "from": "gu"
        }
        },
        {"match": {
          "age": "18"}
        }
      ]
    }
  },
  "_source": ["age","name","from"],
  "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ]
}

most_not

“select age,name where first where from!=gu and age!=18”

GET /first/_search
{
  "query": {
    "bool": {
      "must_not": [
        {"match": {
          "from": "gu"
        }
        },
        {"match": {
          "age": "22"}
        }
      ]
    }
  },
  "_source": ["age","name","from"],
  "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ]
}

filter 过滤查询

过滤条件的范围用 range 表示

  • gt 表示大于
  • gte 表示大于等于
  • lt 表示小于
  • lte 表示小于等于

“select age,name where first where from=gu and age>=18 and age<=20”

GET /first/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "from": "gu"
        }
        }
      ],
      "filter": [
        {"range": {
          "age": {
            "gte": 18,
            "lte": 20
          }
        }}
      ]
    }
  },
  "_source": ["age","name","from"],
  "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ]
}

2.7.7、短语检索【可用数组中检索关键字】
模糊查找
GET /first/_search
{
  "query": {
    "match": {
      "tags": "暗 魔"  #空格分开
    }
    } 
}

结果

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0732633,
    "hits" : [
      {
        "_index" : "first",
        "_type" : "chunsheng",
        "_id" : "1",
        "_score" : 1.0732633,
        "_source" : {
          "name" : "春生",
          "age" : 18,
          "from" : "gu",
          "desc" : "念能力,学生,暗属性",
          "tags" : [
            "能力者",
            "男",
            "暗"
          ]
        }
      },
      {
        "_index" : "first",
        "_type" : "chunsheng",
        "_id" : "3",
        "_score" : 0.9403362,
        "_source" : {
          "name" : "愚者",
          "age" : 22,
          "from" : "gu",
          "desc" : "塔罗",
          "tags" : [
            "魔法",
            "超能力",
            "塔罗"
          ]
        }
      }
    ]
  }
}


精准查找
GET /first/_search
{
  "query": {
    "match_phrase": {
      "tags": "魔法"
    }
    } 
}

2.7.8 、term查询

term查询是直接通过倒排索引指定的 词条,也就是精确查找。

term和match的区别:

  • match是经过分析(analyer)的,也就是说,文档是先被分析器处理了,根据不同的分析器,分析出的结果也会不同,在会根据分词 结果进行匹配。
  • term是不经过分词的,直接去倒排索引查找精确的值。
2.7.8.1、字段是否存在:exist
GET /first/_search
{
  "query": {
    "exists": {
      "field": "from_"
    }
  }
  
}



2.7.8.2、id查询:ids

ids 即对id查找

GET /first/_search
{
  "query": {
    "ids": {
      "values": [3, 1]
    }
  }
}

2.7.8.3、前缀:prefix

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注大数据)
img

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

“_score” : 0.9403362,
“_source” : {
“name” : “愚者”,
“age” : 22,
“from” : “gu”,
“desc” : “塔罗”,
“tags” : [
“魔法”,
“超能力”,
“塔罗”
]
}
}
]
}
}


###### 精准查找



GET /first/_search
{
“query”: {
“match_phrase”: {
“tags”: “魔法”
}
}
}


##### 2.7.8 、term查询


`term`查询是直接通过倒排索引指定的 词条,也就是精确查找。


term和match的区别:


* match是经过分析(analyer)的,也就是说,文档是先被分析器处理了,根据不同的分析器,分析出的结果也会不同,在会根据分词 结果进行匹配。
* term是不经过分词的,直接去倒排索引查找精确的值。


###### 2.7.8.1、字段是否存在:exist



GET /first/search
{
“query”: {
“exists”: {
“field”: "from
"
}
}

}




###### 2.7.8.2、id查询:ids


ids 即对id查找



GET /first/_search
{
“query”: {
“ids”: {
“values”: [3, 1]
}
}
}


###### 2.7.8.3、前缀:prefix




**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注大数据)**
[外链图片转存中...(img-1Gpp23ib-1713282746685)]

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值