ElasticSearch模板和別名

壹頁書發表於2016-05-20
單位現在的搜尋系統,每天凌晨,都會做一個全量更新.然後資料一天之內就不會變了...
以後引入準實時搜尋之後,為了保險起見,我可能還會保留凌晨的全量更新.

我大致會採用如下方式,
建立兩個索引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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章