# ES5で形態素解析した結果がaggrigationできない。

**URL:** <https://discuss.elastic.co/t/es5-aggrigation/70315>\
**Category:** 日本語による質問・議論はこちら\
**Created:** [December 31, 2016, 5:42am UTC](https://discuss.elastic.co/t/es5-aggrigation/70315 "2016-12-31T05:42:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![boomin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/boomin/32/14238_2.png) [@boomin](https://discuss.elastic.co/u/boomin)\
**Post date:** [December 31, 2016, 5:42am UTC](https://discuss.elastic.co/t/es5-aggrigation/70315/1 "2016-12-31T05:42:47Z")

</div>

初めてここで質問させていただきます。  
悩みはタイトル通りなのですが、これまでkibana4やElasticSearch4.xで実現できていたことが、ver5でできずに困っております。

## 実現したいこと

- kibanaのvisualizeで、Aggregation -\> Termで形態素解析結果のフィールドを選択し、集計できるようにしたい。
  - 以下のmapping例で、text.jaを選択できるようにしたい

- kuromojiで形態素解析する場合としない場合の両方を利用したいので、Multi Fieldを使いたい
  - v5でもこんな感じのことがしたい
  - [http://qiita.com/harukasan/items/4ec517d8d96f557367e1](http://qiita.com/harukasan/items/4ec517d8d96f557367e1)

いろいろelasticsearchのmappingをいじったのですが、、、  
形態素解析自体はできているようなのですが、現時点では、kibana5からそのフィールドをvisualize時のfieldで選択できません。

## mapping定義

### elasticsearch4のマッピング定義(抜粋)

ElasticSearch4の時のmapping定義は以下の通りです。

```
  {
  "template": "*",
  "mappings": {
    "_default_": {
      "_all": {
        "analyzer": "ja_analyzer"
      },
      "dynamic_templates": [{
        "analyzed_fields": {
          "match_pattern": "regex",
          "match": ".*text.*|.*text",
          "mapping": {
            "type": "string",
            "fields": {
              "raw": {
                "type": "string",
                "index": "not_analyzed"
              },
              "facet": {
                "type": "string",
                "analyzer": "facet_analyzer"
              },
              "ja": {
                "type": "string",
                "analyzer": "ja_analyzer"
              }
            }
          }
        }
      }, {
        "string_template": {
          "match_mapping_type": "string",
          "mapping": {
            "type": "string",
            "fields": {
              "raw": {
                "type": "string",
                "index": "not_analyzed"
              }
            }
          }
        }
      }]
    }
  }
}

```

### elasticsearch5のマッピング定義(抜粋)

ElasticSearch5の時のmapping定義は以下の通りです。  
5では、stringやらindex周りの書き方に変更があったので、以下のように変えています。

```
{
  "template": "*",
  "mappings": {
    "_default_": {
      "_all": {
        "analyzer": "ja_analyzer"
      },
      "dynamic_templates": [{
        "analyzed_fields": {
          "match_pattern": "regex",
          "match": ".*text.*|.*text",
          "mapping": {
            "type": "text",
            "fields": {
              "raw": {
                "type": "keyword"
              },
              "ja": {
                "type": "text",
                "analyzer": "ja_analyzer"
              },
              "yomi": {
                "type": "text",
                "analyzer": "yomi_analyzer"
              }
            }
          }
        }
      }, {
        "string_template": {
          "match_mapping_type": "string",
          "match": "*",
          "mapping": {
            "type": "keyword"
          }
        }
      }]
    }
  }
}

```

kibana4ではtext.rawもtext.jaも両方ともaggrigationの対象となっていたのでいろいろ集計できました。  
しかし残念ながら、kibana5ではanalyzed された領域がaggregatable に対してtrueになりません。

なお、elasticsearch上にtext.raw、text.ja、text.yomiというフィールド自体は作成されており、 analyzed が有効（true）となっていますが、text.raw だけがaggregatable が有効（true）になっています。そして、もちろんtext.raw のanalyzed はfalseです。

以上、解決方法をご存知の方がいらっしゃったらご教示よろしくお願いします。

---

<div class="post-metadata">

**Author:** ![tsgkdt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsgkdt/32/39151_2.png) [@tsgkdt](https://discuss.elastic.co/u/tsgkdt)\
**Post date:** [December 31, 2016, 8:24am UTC](https://discuss.elastic.co/t/es5-aggrigation/70315/2 "2016-12-31T08:24:02Z")

</div>

当方が持っている環境(5.1.1)で確認したところ、Analyze対象フィールドに **”fielddata”=true** を設定するとaggregationで指定できるようになりました。

提示された例だと、こんな風に。

```json
"fields": {
              "raw": {
                "type": "keyword"
              },
              "ja": {
                "type": "text",
                "fielddata": true,
                "analyzer": "ja_analyzer"
              },

```

textを指定したフィールドで、デフォルトはfalseになっています。fielddataについては、こちらを参照しました。

> **[fielddata | Elasticsearch Guide \[5.1\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/5.1/fielddata.html)**

aggregationに指定できるようになりますが、5,1,1でも4.x系のときと同じくWARNINGが表示されます。

> Careful! The field selected contains analyzed strings. Analyzed strings are highly unique and can use a lot of memory to visualize. Values such as foo-bar will be broken into foo and bar. See Mapping Types for more information on setting this field as keyword (not analyzed).

---

<div class="post-metadata">

**Author:** ![boomin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/boomin/32/14238_2.png) [@boomin](https://discuss.elastic.co/u/boomin)\
**Post date:** [January 1, 2017, 1:17am UTC](https://discuss.elastic.co/t/es5-aggrigation/70315/3 "2017-01-01T01:17:11Z")

</div>

おお！できました！  
だいぶ悩んでいたのですが、自分のスキル不足を痛感しますね。

ver4ではどのような設定がデフォルトだったのかわかりませんが、String型のデータ回りのmappingの書き方が変わるに伴って、この辺の仕様も変更されたのでしょうかね。

ともあれ、解決できました。ありがとうございました。

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 29, 2017, 1:17am UTC](https://discuss.elastic.co/t/es5-aggrigation/70315/4 "2017-01-29T01:17:20Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
