ES學習筆記之---template的使用

es提供template功能的出發點在哪裏呢? 做爲NoSQL數據庫, ES在數據入庫前是不作schema設定的, 也就是不限定數據字段.這對日誌類型的數據來講, 是個利好的場景. 可是這種不設定schema的作法, 有時有太過自由. 有些業務場景, 咱們須要預先設定field的分詞方式. 這時當然能夠使用mappings解決. 可是業務接入前要通知一下,先建個索引, 想一想有點不智能. 有沒有更靈活一點的作法呢? templates數據庫

templates的使用很簡單, 可是想用好, 不出問題或者少出問題, 得有一整套流程:app

  1. 建立templatecurl

    curl -XPUT localhost:9200/_template/template_1 -d '
    {
    "template" : "te*",
    "settings" : {
        "number_of_shards" : 1,
        "number_of_replications":2
    },
    "mappings" : {
        "type1" : {
            "_source" : { "enabled" : false }
        }
    }
    }
    '
  2. 查看templateide

    curl -XGET localhost:9200/_template/template_1?pretty
  3. 若是templates建立出錯, 刪除templatepost

    curl -XDELETE localhost:9200/_template/template_1
  4. template建好後, 要測試一下是否符合預期, 添加一條數據
    $ curl -XPUT 'http://localhost:9200/template_test/tweet/1' -d '{
    "user" : "kimchy",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch"
    }'
  5. 查看集羣的狀態, 若是分片副本設置錯誤, 有可能集羣變成yellow測試

    curl -XGET 'http://localhost:9200/_cluster/health?pretty=true'
  6. 查看索引結構及數據樣例
    curl -XGET 'http://localhost:9200/twitter/_settings,_mappings?pretty'
    curl -XGET 'http://localhost:9200/template_test/tweet/1'

通過後面這些驗證, 通常就能規避大多數問題了.url

相關文章
相關標籤/搜索