零からの

ゆるーく綴るブログ

Elasticsearchにkuromojiを導入する

kuromojiを導入することで日本語の分析が可能になるので試してみる。

環境

Dockerの設定を以下のように変更した。

構成

.
├── docker
│   └── elasticsearch
│       └── Dockerfile
└── docker-compose.yml

Dockerfile

FROM docker.elastic.co/elasticsearch/elasticsearch:7.8.0

RUN /usr/share/elasticsearch/bin/elasticsearch-plugin install analysis-kuromoji

docker-compose.yml

version: '3'
services:
  elasticsearch:
    build:
      context: .
      dockerfile: docker/elasticsearch/Dockerfile
    container_name: elasticsearch
    environment:
      - TZ=Asia/Tokyo
      - discovery.type=single-node
    ports:
      - 9200:9200

インデックス作成

以下のように作成する。

curl -H "Content-Type: application/json" -XPUT "http://localhost:9200/kuromoji_index?pretty" -d '
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_kuromoji_analyzer": {
          "type": "custom",
          "tokenizer": "kuromoji_tokenizer"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "sample_text": {
        "type": "text",
        "analyzer": "my_kuromoji_analyzer",
        "fielddata": true,
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      }
    }
  }
}
'

設定できたことを確認する。

curl -XGET "http://localhost:9200/kuromoji_index?pretty"

{
  "kuromoji_index" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "sample_text" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword"
            }
          },
          "analyzer" : "my_kuromoji_analyzer",
          "fielddata" : true
        }
      }
    },
    "settings" : {
      "index" : {
        "number_of_shards" : "1",
        "provided_name" : "kuromoji_index",
        "creation_date" : "1596157759579",
        "analysis" : {
          "analyzer" : {
            "my_kuromoji_analyzer" : {
              "type" : "custom",
              "tokenizer" : "kuromoji_tokenizer"
            }
          }
        },
        "number_of_replicas" : "1",
        "uuid" : "RRaSWI1ES7SnxGtEkPJe4Q",
        "version" : {
          "created" : "7080099"
        }
      }
    }
  }
}

確認

データ投入

curl -H "Content-Type: application/json" -XPOST "http://localhost:9200/kuromoji_index/_doc?pretty" -d '{ "sample_text": "今日の晩ごはんはカレー" }'

クエリ実行

curl -H "Content-Type: application/json" -XGET "http://localhost:9200/kuromoji_index/_search?pretty" -d '
{
  "query": { "match": { "sample_text": "今日" } }
}'

{
  "took" : 148,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "kuromoji_index",
        "_type" : "_doc",
        "_id" : "nWvye3MBck8r_7svDEjq",
        "_score" : 0.2876821,
        "_source" : {
          "sample_text" : "今日の晩ごはんはカレー"
        }
      }
    ]
  }
}

ヒットする。

ちなみに「今」だと

curl -H "Content-Type: application/json" -XGET "http://localhost:9200/kuromoji_index/_search?pretty" -d '
{
  "query": { "match": { "sample_text": "今" } }
}'

{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

ヒットしない。

最後に

kuromojiの設定から使用までをざっと確認した。

kuromojiの詳細についてはまだ理解できていないが、こちらが参考になりそう。