5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Elasticsearch のマッピング。日本語の単語が解析されてバラバラになっちゃう場合の対応

Posted at

Elasticsearchのマッピング定義についてです。

Elasticsearchにデータを登録して、いざKibanaで可視化しようと思ったら、
日本語の単語がバラバラに認識されちゃって困ったときの対応方法です。
ログのキーワード単位で集計したい時に、日本語がバラバラだと集計できないです。。

マッピングを定義してから、データを登録しましょう。

Elasticsearchでは、マッピングを定義しないで、データを登録すると自動マッピングされますが、
想定したマッピングにならないことがありますので注意です。
ちなみに、マッピングについては、こちらのサイトが勉強になります。

Elasticsearch におけるマッピングとは、リレーショナルDBでいうところのテーブル定義に相当します。しかし、単にデータを格納する為のフィールドを用意して型を設定するだけではありません。Elasticsearch では、フィールドの型の他に言語解析処理などのドキュメントを検索可能にする為の各種設定が可能です。

マッピング定義の仕方

日本語で、分かち書きしたくない項目については、not_analyzed を指定して、登録します。
そうすれば、日本語の文章がそのまま格納でき、ログの内容等でKibanaでの集計が可能になります。

curl -XPUT 'http://your_es_host_address/elasticsearch' -d
{
  "Your_Index_Name" : {
    "mappings" : {
      "Your_Document_Name" : {
        "properties" : {
          "@timestamp" : {
            "type" : "date",
            "format" : "dateOptionalTime"
          },
          "hoge" : {
            "type" : "string",
            "index": "not_analyzed"
          },
          "fuga" : {
             "type" : "string",
             "index": "not_analyzed"
          },
          "contents" : {
             "type" : "string",
             "index": "not_analyzed"
          },
          
        }
      }
    }
  }
}

マッピング定義の確認

以下のコマンドでマッピング定義が確認できます。
not_analyzed属性がついていればOKです。

curl -XGET 'http://your_es_host_address/elasticsearch/Your_Index_Name/Your_Document_Name/_mapping?pretty=true'
5
4
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
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?