LoginSignup
59
59

More than 5 years have passed since last update.

Elasticsearch の index templates で解析方法を指定する(または、default mapping が効かない時に)

Last updated at Posted at 2014-04-12

Elasticsearch の(恐らく)1.0 から /etc/elasticsearch/default-mapping.json に設定を書いても mappings に反映されなくなりました。今後は index templates 機能を使う必要があります。

index templates は API 経由で設定することも、ファイルを設置することもできます。運用上適した方を使えばいいでしょう。

API

localhost:9200/_template/<template_name> に json を PUT/GET/DELETE すれば ok です。

例えば kibana で使う accesslog-YYYY.MM.DD という index 用の設定は次のようになります。

{
  "template": "accesslog-*",
  "mappings": {
    "fluent": {
      "properties": {
        "host": {
          "type": "string",
          "index": "not_analyzed"
        },
        "uri": {
          "type": "string",
          "index": "not_analyzed"
        }
      }
    }
  }
}

これを curl -XPUT localhost:9200/_template/accesslog -d ... のようにして保存します。

template キーの accesslog-* という値が、各 index に対してこのテンプレートを適用するか判定するパターンになります。accesslog-2014.04.12 という名前の index が作成されると、パターンにマッチするのでこのテンプレートが適用されます。

mappings の下の fluent はタイプ名で、fluent というタイプで投入したデータに対して mappings を適用するように範囲を限定しています。properties 以下がデータの各キーに対する mapping で、この例では hosturi を string 型で indexing 時に解析しない(そのままのデータを使う)ように指定しています。それ以外のキーは dynamic mapping 機能で自動的に処理されます。

Fluentd と fluent-plugin-elasticsearch でデータを流し込むなら、fluent.conf は以下のようになるでしょう。

<match pyfluent.**>
  type elasticsearch
  host localhost
  port 9200
  logstash_format true
  logstash_prefix accesslog
  type_name fluent
  include_tag_key true
  tag_key @tag
  flush_interval 5s
</match>

流し込むデータは次のようなものを想定しています。

{"host": "127.0.0.1", "uri": "/users/1", "method": "GET", ...}

ファイル

API 経由ではなく、ファイルを使って設定することもできます。その場合 /etc/elasticsearch/templates/<template_name>.json ファイルを作成します。

注意が必要なのは、API 経由の時と微妙にデータ構造が異なることです。先ほどの例だと、次の内容で /etc/elasticsearch/templates/accesslog.json を作成します。

{
  "accesslog": {
    "template": "accesslog-*",
    "mappings": {
      "fluent": {
        "properties": {
          "host": {
            "type": "string",
            "index": "not_analyzed"
          },
          "uri": {
            "type": "string",
            "index": "not_analyzed"
          }
        }
      }
    }
  }
}
59
59
1

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
59
59