别名

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

  1. 只查询某一天的数据,只查询某个对应的index<br /> 2021-07-07 ------> GET ods_a_2021-07-07_index
  2. 查询这个月的所有数据?<br /> 这个月的index在创建时,为它们赋予一个别名 2021-07_index<br /> 使用别名查询: GET 2021-07_index
  3. 查询每一天所有的数据?<br /> 每个index在创建时,为它们赋予一个别名 ods_a_index<br /> 使用别名查询: GET ods_a_index

自己的理解:

别名,
理解为指定组织更恰当
例如一个组织可以有多个人,一个人可以加入多个组织
人和组织都是独立的存在互不干扰,只有加入与离开的关系

2.别名练习

如果你有100w记录,其中男性只有10条,需求都是对所有的男性统计
#可以,为所有的男性数据,创建一个别名,在统计时,只扫描指定的子集
GET /man-query/_search

  1. #增
  2. #①在最初创建index时,直接指定
  3. PUT movie_index
  4. {
  5. "aliases": {
  6. "movie_chn_2020-query": {},
  7. "aindex":{}
  8. },
  9. "mappings": {
  10. "movie_type":{
  11. "properties": {
  12. "id":{
  13. "type": "long"
  14. },
  15. "name":{
  16. "type": "text",
  17. "analyzer": "ik_smart"
  18. }
  19. }
  20. }
  21. }
  22. }
  23. PUT movie_index1
  24. {
  25. "aliases": {
  26. "bindex":{},
  27. "aindex":{}
  28. },
  29. "mappings": {
  30. "movie_type":{
  31. "properties": {
  32. "id":{
  33. "type": "long"
  34. },
  35. "name":{
  36. "type": "text",
  37. "analyzer": "ik_smart"
  38. }
  39. }
  40. }
  41. }
  42. }
  43. # 与上面联合,演示一个别名可以有多个索引
  44. PUT /movie_index1/movie_type/1
  45. {
  46. "id":1,
  47. "name": "《速度与Gay情6》"
  48. }
  49. GET /bindex/_search
  50. #②在index建好后,指定
  51. POST _aliases
  52. {
  53. #改
  54. "actions": [
  55. {
  56. "add": {
  57. "index": "movie_index",
  58. "alias": "bindex"
  59. }
  60. },
  61. {
  62. #删
  63. "remove": {
  64. "index": "movie_index"
  65. , "alias": "aindex"
  66. }
  67. }
  68. ]
  69. }
  70. #查
  71. #查询当前定义的所有的别名
  72. GET /_cat/aliases?v
  73. #查看某个index的别名
  74. GET /.kibana_1/_alias
  75. #建立子集
  76. POST _aliases
  77. {
  78. "actions": [
  79. {
  80. "add": {
  81. "index": "test",
  82. "alias": "man-query",
  83. "filter": {
  84. "term": {
  85. "gender": "男"
  86. }
  87. }
  88. }
  89. }
  90. ]
  91. }

子集?

  1. 给某张表的一部分数据建立别名

模版

1.模版练习

  1. #创建模版 index_patterns,创建index时,什么样的index
  2. #能够匹配上模块
  3. PUT _template/template_movie2020
  4. {
  5. "index_patterns": ["movie_test*"],
  6. "aliases" : {
  7. "{index}-query": {},
  8. "movie_test-query":{}
  9. },
  10. "mappings": {
  11. "_doc": {
  12. "properties": {
  13. "id": {
  14. "type": "keyword"
  15. },
  16. "movie_name": {
  17. "type": "text",
  18. "analyzer": "ik_smart"
  19. }
  20. }
  21. }
  22. }
  23. }
  24. #查询当前所有的模版
  25. GET /_cat/templates?v
  26. #只查询某个模版的信息
  27. GET /_template/template_movie2020
  28. #基于模版创建index,向index插入数据,让es自动为你创建index
  29. #在创建时,如果能够匹配上模版,使用模版上定义的mappings
  30. #创建index,否则将使用类型推断,为你创建index
  31. #没有匹配上模版,由ES自动推断创建的
  32. PUT /abc/_doc/1
  33. {
  34. "id":1,
  35. "name":"jack"
  36. }
  37. GET /abc
  38. #index名称匹配上了模版,基于模版创建index
  39. PUT /movie_test2021-07-08/_doc/1
  40. {
  41. "id":1,
  42. "name":"jack"
  43. }
  44. GET /movie_test2021-07-08
  45. #删除
  46. DELETE /_template/template_movie2020
  47. #判断是否存在
  48. HEAD /_template/template_movie2020

特别的结构:
1、score:分数,越匹配条件,分数越高

2、

“_source”: [
“empid”,
“age”,
“balance”
]