索引别名类似代理一样

  1. GET _cat/aliases?v

单索引单别名绑定解绑

案例:假定公司基本信息索引需要变更

  1. PUT ckiss-company-002
  2. {
  3. "aliases": {
  4. "ckiss-company": {}
  5. },
  6. "mappings": {
  7. "properties": {
  8. "companyId": {
  9. "type": "integer"
  10. },
  11. "companyName": {
  12. "type": "keyword"
  13. }
  14. }
  15. }
  16. }
  1. PUT ckiss-company-002/_doc/1
  2. {
  3. "companyId": 1,
  4. "companyName": "美团集团"
  5. }
  6. PUT ckiss-company-002/_doc/2
  7. {
  8. "companyId": 2,
  9. "companyName": "美团集团"
  10. }
  1. # 删除
  2. DELETE ckiss-company-002/_alias/ckiss-company
  3. #设置
  4. PUT ckiss-company-002/_alias/ckiss-company
  5. # 查看
  6. GET ckiss-company-002/_alias
  7. # 修改
  8. POST _aliases
  9. {
  10. "actions": [
  11. {
  12. "remove": {
  13. "index": "ckiss-company-002",
  14. "alias": "ckiss-company"
  15. }
  16. },
  17. {
  18. "add": {
  19. "index": "ckiss-company-002",
  20. "alias": "ckiss-company-fianl"
  21. }
  22. }
  23. ]
  24. }

索引别名不支持直接删除

filter别名查询过滤

案例:假设公司员工信息很多,按公司编号去做一些隔离查询

  1. DELETE company-staff-001
  2. PUT company-staff-001
  3. {
  4. "mappings": {
  5. "properties": {
  6. "companyId": {
  7. "type": "integer"
  8. },
  9. "userId": {
  10. "type": "keyword"
  11. },
  12. "userName":{
  13. "type": "keyword"
  14. }
  15. }
  16. }
  17. }
  1. PUT company-staff-001/_doc/1
  2. {
  3. "companyId": 1,
  4. "userId": "111",
  5. "userName": "lisi"
  6. }
  7. PUT company-staff-001/_doc/2
  8. {
  9. "companyId": 1,
  10. "userId": "121",
  11. "userName": "zhangs"
  12. }
  13. PUT company-staff-001/_alias/company-staff
  14. {
  15. "filter": {
  16. "term": {
  17. "userId": "121"
  18. }
  19. }
  20. }
  21. GET company-staff/_search
  22. {}
  1. "hits" : [
  2. {
  3. "_index" : "company-staff-001",
  4. "_type" : "_doc",
  5. "_id" : "2",
  6. "_score" : 1.0,
  7. "_source" : {
  8. "companyId" : 1,
  9. "userId" : "121",
  10. "userName" : "zhangs"
  11. }
  12. }
  13. ]

routing 路由控制

假设公司员工信息很多,按照公司编号做一些隔离查询

  1. DELETE company-staff-001
  2. PUT company-staff-001
  3. {
  4. "settings": {
  5. "number_of_shards": 3
  6. },
  7. "mappings": {
  8. "properties": {
  9. "companyId": {
  10. "type": "integer"
  11. },
  12. "userId": {
  13. "type": "keyword"
  14. },
  15. "userName":{
  16. "type": "keyword"
  17. }
  18. }
  19. }
  20. }
  21. #指定路由
  22. PUT company-staff-001/_doc/1?routing=1
  23. {
  24. "companyId": 1,
  25. "userId": "111",
  26. "userName": "员工111"
  27. }
  28. PUT company-staff-001/_doc/2
  29. {
  30. "companyId": 2,
  31. "userId": "121",
  32. "userName": "员工121"
  33. }
  34. PUT company-staff-001/_doc/3
  35. {
  36. "companyId": 3,
  37. "userId": "123",
  38. "userName": "员工123"
  39. }
  40. GET company-staff-001/_search
  41. {}
  42. # 建立别名时候指定路由过滤数据
  43. PUT company-staff-001/_alias/company-staff
  44. {
  45. "routing": "1"
  46. }

is_write_index 读写控制

  • is_write_index读写关键字
  • 创建一个索引读别名
  • 创建一个索引写别名

    is_write_index设置了此索引是可以通过此别名被写入的索引

  1. # 创建索引
  2. PUT company-staff-002
  3. {
  4. "mappings": {
  5. "properties": {
  6. "companyId": {
  7. "type": "integer"
  8. },
  9. "userId": {
  10. "type": "keyword"
  11. },
  12. "userName":{
  13. "type": "keyword"
  14. }
  15. }
  16. }
  17. }
  18. # 获取别名
  19. GET company-staff-001/_alias
  20. # 删除已经建立的别名
  21. DELETE company-staff-001/_alias/company-staff
  22. #设置读写属性
  23. POST /_aliases
  24. {
  25. "actions": [
  26. {
  27. "add": {
  28. "index": "company-staff-001",
  29. "alias": "company-staff",
  30. "is_write_index": false
  31. }
  32. },
  33. {
  34. "add": {
  35. "index": "company-staff-001",
  36. "alias": "company-staff",
  37. "is_write_index": true
  38. }
  39. }
  40. ]
  41. }

alias 批量操作api

  • actions : 别名操作关键字
  • add:新增别名操作
  • remove:删除别名操作
  • is_write_index: 读写分离机设置
  • search_routing: 查询路由字段,路由值随意设置
  • index_routing: 写入路由字段

索引模板

索引模板创建

  • _template: 模板关键字,遗留版本
  • _index_template:模板关键字,新版本
  • index_patterns:索引模板匹配关键字,索引匹配可指定前缀和后缀

    案例:公司基本信息定期创建新索引,按照年份月份创建,限制动态增加索引字段,新增的字段仅仅保留元数据,设置索引别名,设置字段数量与属性,设置索引分片与副本。

dynamic

  • true:遇到陌生字段,就进行dynamic mapping
  • false:遇到陌生字段,就忽略
  • strict:遇到陌生字段,就报错 ```json GET _template GET _cat/templates?v

DELETE _template/ckiss-company-tp01

新建模板

PUT _template/ckiss-company-tp01 { “index_patterns”: [ “ckiss-company-2022“, “company-“ ], “settings”: { “number_of_shards”: 3, “number_of_replicas”: 1, “refresh_interval”: “15s” }, “mappings”: { “_source”: { “enabled”: true }, “properties”: { “companyId”: { “type”: “integer” }, “userId”: { “type”: “keyword” }, “userName”: { “type”: “keyword” } }, “dynamic”: false }, “aliases”: { “ckiss-company”: {} } }

PUT ckiss-company-2022-01/_create/1 { “companyId”: 1, “userId”: “111”, “userName”: “员工111” }

PUT ckiss-company-2022-01/_create/2 { “companyId”: 2, “userId”: “112”, “userName”: “员工112”, “area”: { “province”: “HB” } }

检索

GET ckiss-company-2022-01/_mapping GET ckiss-company/_search GET _template/ckiss-company-tp01

  1. <a name="qPVH7"></a>
  2. #### 组件式索引模板:`_component_template`
  3. - `_component_template`:组件模板关键字
  4. - 与索引模板创建语法类似,不能指定匹配规则,需要与索引模板一起使用
  5. - `composed_of`: 索引模板组合多个组件模板参数
  6. > 假定公司基本信息包括 区域信息;员工信息也包括区域信息,可以将区域信息独立 出来设计,需要的时候组合在 一起
  7. ```json
  8. GET _component_template
  9. PUT _component_template/area_tp01
  10. {
  11. "template": {
  12. "mappings": {
  13. "properties": {
  14. "area":{
  15. "properties": {
  16. "province":{
  17. "type": "keyword"
  18. },
  19. "city":{
  20. "type": "keyword"
  21. },
  22. "country": {
  23. "type": "keyword"
  24. }
  25. }
  26. }
  27. }
  28. }
  29. }
  30. }
  31. GET _index_template
  32. GET _cat/templates
  33. DELETE _index_template/company_tp30
  34. PUT _index_template/company_tp30
  35. {
  36. "index_patterns": [
  37. "company-20*",
  38. "company-*"
  39. ],
  40. "template": {
  41. "settings": {
  42. "number_of_shards": 1,
  43. "number_of_replicas": 1,
  44. "refresh_interval": "1s"
  45. }
  46. },
  47. "priority": 30,
  48. "composed_of": [
  49. "area_tp01"
  50. ]
  51. }
  52. # 数据
  53. DELETE company-2022
  54. PUT company-2022/_create/1
  55. {
  56. "companyId": 1,
  57. "companyName": "华住集团",
  58. "area": {
  59. "province": "HB",
  60. "city": "WH",
  61. "county": "HS"
  62. }
  63. }
  64. GET company-2022

根据别名索引不停机重建

  1. # 1.index_old取一个别名index_serviceindex_service对外提供服务
  2. POST /_aliases
  3. {
  4. "actions": [
  5. {
  6. "add": {
  7. "index": "index_old",
  8. "alias": "index_service"
  9. }
  10. }
  11. ]
  12. }
  13. # 2.新创建一个索引index_new,数据结构与index_old一样,但是name字段的类型为keyword
  14. PUT index_new
  15. {
  16. "mappings": {
  17. "_doc": {
  18. "properties": {
  19. "id": {"type": "long"},
  20. "name": {"type": "keyword"}
  21. }
  22. }
  23. }
  24. }
  25. # 3.index_old的数据同步到index_new
  26. POST /_reindex?wait_for_completion=false
  27. {
  28. "source": {
  29. "index": "index_old"
  30. },
  31. "dest": {
  32. "index": "index_new"
  33. }
  34. }
  35. # wait_for_completion=true,同步执行,会一直等待直到同步完成或者报错,默认
  36. # wait_for_completion=false,异步执行,返回taskId,然后后台进行同步,数据量大的情况下建议使用
  37. # 4.index_old的别名删除,index_new的别名设置为index_service
  38. POST /_aliases
  39. {
  40. "actions": [
  41. {
  42. "remove": {
  43. "index": "index_old",
  44. "alias": "index_service"
  45. }
  46. },
  47. {
  48. "add": {
  49. "index": "index_new",
  50. "alias": "index_service"
  51. }
  52. }
  53. ]
  54. }
  55. # 5.删除index_old
  56. DELETE /index_old