ITPub博客

首页 > 应用开发 > Java > ElasticSearch模板和别名

ElasticSearch模板和别名

原创 Java 作者:壹頁書 时间:2016-05-20 22:04:43 0 删除 编辑
单位现在的搜索系统,每天凌晨,都会做一个全量更新.然后数据一天之内就不会变了...
以后引入准实时搜索之后,为了保险起见,我可能还会保留凌晨的全量更新.

我大致会采用如下方式,
创建两个索引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博客 ” ,链接:https://blog.itpub.net/29254281/viewspace-2104016/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2013-10-19

  • 博文量
    620
  • 访问量
    6339946