# 日本語全文検索について

**URL:** <https://discuss.elastic.co/t/topic/69140>\
**Category:** 日本語による質問・議論はこちら\
**Created:** [December 15, 2016, 9:48am UTC](https://discuss.elastic.co/t/topic/69140 "2016-12-15T09:48:43Z")\
**Posts on this page:** 1\
**Showing post:** 4

<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 16, 2016, 8:15am UTC](https://discuss.elastic.co/t/topic/69140/4 "2016-12-16T08:15:38Z")

</div>

お使いになられているelasticsearchのバージョンはどれでしょう？

こちらで試した限りですと、5.1.1でkuromojiを設定したフィールドに"私は一眼レフカメラが欲しい"という語を入れて見たところ、  
「一眼」が「一」と「眼」に分かれないのです。

mappingの設定や、どのようなクエリをelasticsearchに発行したのかがわかると良いのですが・・・。  
なお、[検索の精度を上げるアプローチの仕方](https://discuss.elastic.co/t/topic/61728/4)が参考になるかもしれません。

以下、長くなりますがこちらで確認した内容です。

1. 事前準備

indexの作成。 kuromoji\_tokenizerのmodeごとの差異があるかどうかを確認するため、3つAnalyzerを作りました。

- ja-normal-analyzer
- ja-search-analyzer
- ja-extended-analyzer

```auto
PUT kuromoji-test
{
  "template": "kuromoji-test",
  "settings": {
    "analysis": {
      "analyzer": {
        "ja-normal-analyzer": {
          "type": "custom",
          "tokenizer": "ja-normal-tokenizer"
        },
        "ja-search-analyzer": {
          "type": "custom",
          "tokenizer": "ja-search-tokenizer"
        },
        "ja-extended-analyzer": {
          "type": "custom",
          "tokenizer": "ja-extended-tokenizer"
        }
      },
      "tokenizer": {
        "ja-normal-tokenizer": {
          "type": "kuromoji_tokenizer",
          "mode": "normal"
        },
        "ja-search-tokenizer": {
          "type": "kuromoji_tokenizer",
          "mode": "search"
        },
        "ja-extended-tokenizer": {
          "type": "kuromoji_tokenizer",
          "mode": "extended"
        }
      }
    }
  },
  "mappings": {
    "_default_": {
      "dynamic_templates": [
        {
          "test": {
            "match": "message",
            "mapping": {
              "type": "text",
              "fields": {
                "normal": {
                  "type": "text",
                  "analyzer": "ja-normal-analyzer",
                  "term_vector": "with_positions_offsets",
                  "store": true
                },
                "search": {
                  "type": "text",
                  "analyzer": "ja-search-analyzer",
                  "term_vector": "with_positions_offsets",
                  "store": true
                },
                "extended": {
                  "type": "text",
                  "analyzer": "ja-extended-analyzer",
                  "term_vector": "with_positions_offsets",
                  "store": true
                }
              }
            }
          }
        }
      ]
    }
  }
}

```

2.Analyze APIによる分割の確認

```auto
POST kuromoji-test/_analyze
{
  "analyzer": "ja-normal-analyzer",
  "text" : "私は一眼レフカメラが欲しい"
}

```

normal, search, extendedのいずれで行っても 一眼は１つのトークンになりました。  
これはnormalの結果です。

```auto
{
  "tokens": [
    {
      "token": "一眼",
      "start_offset": 0,
      "end_offset": 2,
      "type": "word",
      "position": 0
    },
    {
      "token": "レフ",
      "start_offset": 2,
      "end_offset": 4,
      "type": "word",
      "position": 1
    },
    {
      "token": "カメラ",
      "start_offset": 4,
      "end_offset": 7,
      "type": "word",
      "position": 2
    },
    {
      "token": "が",
      "start_offset": 7,
      "end_offset": 8,
      "type": "word",
      "position": 3
    },
    {
      "token": "欲しい",
      "start_offset": 8,
      "end_offset": 11,
      "type": "word",
      "position": 4
    }
  ]
}

```

3.検索実行による確認

結果が取得できて、またハイライトも行われることを確認しました。（ちゃんと _タグの中に一眼が入っている）_

```auto
PUT kuromoji-test/test/1
{
  "message" : "私は一眼レフカメラが欲しい"
}

```

```auto
GET kuromoji-test/_search
{
  "query": {
    "match_phrase": {
      "message.normal": "一眼"
    }
    
  },
  "highlight": {
    "require_field_match": false,
    "fields": {
      "message.normal" : {},
      "message.search" : {},
      "message.extended" : {}
    }
  }
}

```

```auto
{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.28582606,
    "hits": [
      {
        "_index": "kuromoji-test",
        "_type": "test",
        "_id": "1",
        "_score": 0.28582606,
        "_source": {
          "message": "私は一眼レフカメラが欲しい"
        },
        "highlight": {
          "message.search": [
            "私は<em>一眼</em>レフカメラが欲しい"
          ],
          "message.extended": [
            "私は<em>一眼</em>レフカメラが欲しい"
          ],
          "message.normal": [
            "私は<em>一眼</em>レフカメラが欲しい"
          ]
        }
      }
    ]
  }
}

```

---

_[View the full topic](https://discuss.elastic.co/t/topic/69140)._
