LoginSignup
9
9

More than 5 years have passed since last update.

elasticsearchでtemplateをconfigファイルで管理する方法

Last updated at Posted at 2015-09-16

通常上記の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ファイルをおきます

9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9