ElasticSearch模板和别名

单位现在的搜索系统,每天凌晨,都会做一个全量更新.然后数据一天之内就不会变了...
以后引入准实时搜索之后,为了保险起见,我可能还会保留凌晨的全量更新.

我大致会采用如下方式,
创建两个索引songod_m1和songod_m2,和一个别名 songod
每天凌晨,全量更新备用的索引,完成之后,切换别名.周而复始.
准实时搜索的功能,通过Canal将MySQL的变化,异步更新到ES集群.
这其中肯定有数据不一致的风险.但是我觉得可以接受.

创建模板.
  1. {  
  2.     "template""songod*",   
  3.     "order": 1,   
  4.     "settings": {  
  5.         "index": {  
  6.             "index.number_of_replicas": 1,   
  7.             "number_of_shards": 5,   
  8.             "refresh_interval""30s"  
  9.         }  
  10.     },   
  11.     "mappings": {  
  12.         "sod_song_ksc": {  
  13.             "dynamic_templates": [  
  14.                 {  
  15.                     "all_field": {  
  16.                         "mapping": {  
  17.                             "index""no",   
  18.                             "store""yes",   
  19.                             "type""{dynamic_type}",   
  20.                             "include_in_all"false  
  21.                         },   
  22.                         "match""*"  
  23.                     }  
  24.                 }  
  25.             ],   
  26.             "_source": {  
  27.                 "enabled"false  
  28.             },   
  29.             "_all": {  
  30.                 "enabled"true,   
  31.                 "analyzer""ik"  
  32.             },   
  33.             "properties": {  
  34.                 "SongID": {  
  35.                     "type""long",   
  36.                     "store""yes",   
  37.                     "index""not_analyzed",   
  38.                     "include_in_all"true  
  39.                 },   
  40.                 "Name": {  
  41.                     "type""multi_field",   
  42.                     "fields": {  
  43.                         "Name": {  
  44.                             "type""string",   
  45.                             "store""yes",   
  46.                             "index""analyzed",   
  47.                             "analyzer""ik"  
  48.                         },   
  49.                         "raw": {  
  50.                             "type""string",   
  51.                             "store""yes",   
  52.                             "index""not_analyzed",   
  53.                             "include_in_all"true  
  54.                         }  
  55.                     }  
  56.                 },   
  57.                 "SingerName": {  
  58.                     "type""string",   
  59.                     "store""yes",   
  60.                     "index""analyzed",   
  61.                     "analyzer""stop",   
  62.                     "include_in_all"true  
  63.                 }  
  64.             }  
  65.         }  
  66.     }  
  67. }  

提交模板.
[root@localhost/tmp]$curl -XPUT http://192.168.16.114:9200/_template/songod?pretty --data-binary @index.json
{
  "acknowledged" : true
}

初始化数据,并创建别名
[root@localhost/tmp]$curl -XPOST http://192.168.16.114:9200/_aliases -d '
> {
>     "actions": [
>         {
>             "add": {
>                 "index": "songod_m1", 
>                 "alias": "songod"
>             }
>         }
>     ]
> }
> '


凌晨时候,先查询别名的实际指向。
[root@localhost/tmp]$curl http://192.168.16.114:9200/songod/_aliases?pretty
{
  "songod_m1" : {
    "aliases" : {
      "songod" : { }
    }
  }
}

然后删除 songod_m2 索引,再重建.
重建完成,更改别名.
[root@localhost/tmp]$curl -XPOST http://192.168.16.114:9200/_aliases -d '
{
    "actions": [
        {
            "remove": {
                "index": "songod_m1", 
                "alias": "songod"
            }, 
            "add": {
                "index": "songod_m2", 
                "alias": "songod"
            }
        }
    ]
}
'

再次查看别名,已经更改.
[root@localhost/tmp]$curl http://192.168.16.114:9200/songod/_aliases?pretty
{
  "songod_m2" : {
    "aliases" : {
      "songod" : { }
    }
  }
}
[root@localhost/tmp]$

JAVA应用程序只需要引用 songod别名即可.

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29254281/viewspace-2104016/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29254281/viewspace-2104016/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值