别名
1.对应关系
别名和索引是N对N的关系!
1个别名 对于 N个索引!
1个索引可以拥有多个别名!
别名的主要应用场景:
在hive中有分区表,常见按照数据的日期分区。比如表ods_a,按照dt分区
/ ods_a / dt= 2021-07-07
/ ods_a / dt= 2021-07-08
只查询某一天的数据,使用分区字段进行过滤
where dt= 2021-07-07
如果是全表查询,不加where过滤!
在ES中,如何实现一个分区表的效果?
要实现分区的效果:
只能将每天产生的数据,放入到一个独立的index中
2021-07-07 —————> ods_a_2021-07-07_index
2021-07-08 —————> ods_a_2021-07-08_index
只查询某一天的数据,只查询某个对应的index<br /> 2021-07-07 ------> GET ods_a_2021-07-07_index
查询这个月的所有数据?<br /> 这个月的index在创建时,为它们赋予一个别名 2021-07_index<br /> 使用别名查询: GET 2021-07_index
查询每一天所有的数据?<br /> 每个index在创建时,为它们赋予一个别名 ods_a_index<br /> 使用别名查询: GET ods_a_index
自己的理解:
别名,
理解为指定组织更恰当
例如一个组织可以有多个人,一个人可以加入多个组织
人和组织都是独立的存在互不干扰,只有加入与离开的关系
2.别名练习
如果你有100w记录,其中男性只有10条,需求都是对所有的男性统计
#可以,为所有的男性数据,创建一个别名,在统计时,只扫描指定的子集
GET /man-query/_search
#增
#①在最初创建index时,直接指定
PUT movie_index
{
"aliases": {
"movie_chn_2020-query": {},
"aindex":{}
},
"mappings": {
"movie_type":{
"properties": {
"id":{
"type": "long"
},
"name":{
"type": "text",
"analyzer": "ik_smart"
}
}
}
}
}
PUT movie_index1
{
"aliases": {
"bindex":{},
"aindex":{}
},
"mappings": {
"movie_type":{
"properties": {
"id":{
"type": "long"
},
"name":{
"type": "text",
"analyzer": "ik_smart"
}
}
}
}
}
# 与上面联合,演示一个别名可以有多个索引
PUT /movie_index1/movie_type/1
{
"id":1,
"name": "《速度与Gay情6》"
}
GET /bindex/_search
#②在index建好后,指定
POST _aliases
{
#改
"actions": [
{
"add": {
"index": "movie_index",
"alias": "bindex"
}
},
{
#删
"remove": {
"index": "movie_index"
, "alias": "aindex"
}
}
]
}
#查
#查询当前定义的所有的别名
GET /_cat/aliases?v
#查看某个index的别名
GET /.kibana_1/_alias
#建立子集
POST _aliases
{
"actions": [
{
"add": {
"index": "test",
"alias": "man-query",
"filter": {
"term": {
"gender": "男"
}
}
}
}
]
}
子集?
给某张表的一部分数据建立别名
模版
1.模版练习
#创建模版 index_patterns,创建index时,什么样的index名
#能够匹配上模块
PUT _template/template_movie2020
{
"index_patterns": ["movie_test*"],
"aliases" : {
"{index}-query": {},
"movie_test-query":{}
},
"mappings": {
"_doc": {
"properties": {
"id": {
"type": "keyword"
},
"movie_name": {
"type": "text",
"analyzer": "ik_smart"
}
}
}
}
}
#查询当前所有的模版
GET /_cat/templates?v
#只查询某个模版的信息
GET /_template/template_movie2020
#基于模版创建index,向index插入数据,让es自动为你创建index
#在创建时,如果能够匹配上模版,使用模版上定义的mappings
#创建index,否则将使用类型推断,为你创建index
#没有匹配上模版,由ES自动推断创建的
PUT /abc/_doc/1
{
"id":1,
"name":"jack"
}
GET /abc
#index名称匹配上了模版,基于模版创建index
PUT /movie_test2021-07-08/_doc/1
{
"id":1,
"name":"jack"
}
GET /movie_test2021-07-08
#删除
DELETE /_template/template_movie2020
#判断是否存在
HEAD /_template/template_movie2020
特别的结构:
1、score:分数,越匹配条件,分数越高
2、
“_source”: [ “empid”, “age”, “balance” ] |
---|