通常上記のtemplateをelasticsearchに登録する場合は、以下のようなhttpリクエストを送って行います。
curl -XPUT localhost:9200/_template/template1 -d `cat template1.json`'
しかし、ansibleなどを使った場合、curlでtemplateを登録しようとすると「まだelasticsearchがready状態じゃないからリクエストを受け付けてくれない」場合があるので、ファイルでtemplateを管理する方法を記載します。
elasticsearchのtemplateとは
elasticsearchでは、template(データベースでいうschema)を定義できます。
urlなどを保存する場合、elasticsearchが勝手にurlを解析して、protocol,domainなどに分けてしまい、解析に支障が出る場合があります。
その際は、urlフィールドが勝手に解析されないよう、以下のようなtemplateを定義することになると思います。
template1.json
{
"template":"{{ index名 }}-*",
"mappings":{
"{{ タイプ名 }}":{
"_source": { "compress": true },
"properties":{
"url":{"type":"multi_field",
"fields":{
"url":{"type":"string","index":"analyzed"},
"full":"type":"string","index":"not_analyzed"}
}
}
}
}
}
}
templateをconfigファイルで管理する方法
1./etc/elasticsearch/elasticsearch.yml
にて、path.confを定義します。
elasticsearch.yml
# Path to directory containing configuration (this file and logging.yml):
# ↓アンコメント
path.conf: /usr/share/elasticsearch/config
2./usr/share/elasticsearch/config/templates
に先ほどのtemplate1.json
ファイルをおきます