- 公開日:2016年01月13日
記事概要
elasticsearchで作成済みのindexにanalyzerを設定する方法を記載した記事です。
環境
- vagrant centos6.5
- elasticsearch2.1.1
- java(OpenJDK) version 1.7.0_91
indexの作成
elasticsearchでindexを作成する場合は、以下のように実行します。
terminal
// command
curl -XPUT 'localhost:9200/sample_ana?pretty'
// result
{
"acknowledged" : true
}
settingを確認します。
terminal
// command
curl -XGET 'http://localhost:9200/sample_ana/_settings'
// result
{
"sample_ana": {
"settings": {
"index": {
"creation_date": "1452649163895",
"uuid": "fCWnTlE_SZ2OiWC796SBBA",
"number_of_replicas": "1",
"number_of_shards": "5",
"version": {
"created": "2010199"
}
}
}
}
}
indexが作成されていますね。
indexにanalyzerを定義
上記のindexにはanalyzerが設定されていません。なので、analyzerの設定を行います。
terminal
// command
curl -XPOST 'localhost:9200/sample_ana/_close'
// result
{
"acknowledged": true
}
まずはclose index APIでindexの_closeを実行する必要があります。
close index APIは、indexをクローズすることができます。また、その後、open index APIでindexをopenにすることもできます。
この処理を実行しないとindex定義後にanalyzerは定義できないので注意してください。
では、状態を確認してみましょう。
terminal
// command
curl 'localhost:9200/_cat/indices?v'
// result
health status index pri rep docs.count docs.deleted store.size pri.store.size
close sample_ana
index sample_anaがcloseの状態になっています。
次にanalyzerの設定を行います。tokenizerにはkuromojiを使います。
kuromojiに関しては、以前にも記事にしているので、kuromojiについて理解したい人は参考にしてみてください。
terminal
// command
curl -XPUT 'localhost:9200/sample_ana/_settings' -d '
{
"analysis": {
"tokenizer": {
"kuromoji_user_dict": {
"type": "kuromoji_tokenizer",
"mode": "extended",
"discard_punctuation": "false",
"user_dictionary": "userdict_ja.txt"
}
},
"analyzer": {
"my_analyzer": {
"type": "custom",
"tokenizer": "kuromoji_user_dict"
}
}
}
}'
// result
{
"acknowledged": true
}
うまくいったようです。
open index APIでindexをopenにします。
terminal
// command
curl -XPOST 'localhost:9200/sample_ana/_open'
// result
{
"acknowledged": true
}
状態を確認します。
terminal
// command curl 'localhost:9200/_cat/indices?v' // result health status index pri rep docs.count docs.deleted store.size pri.store.size yellow open sample_ana 5 1 0 0 650b 650b
動作しています。
最後に設定を確認します。
terminal
// command
curl -XGET 'http://localhost:9200/sample_ana/_settings'
// result
{
"sample_ana": {
"settings": {
"index": {
"creation_date": "1452649163895",
"uuid": "fCWnTlE_SZ2OiWC796SBBA",
"analysis": {
"analyzer": {
"my_analyzer": {
"type": "custom",
"tokenizer": "kuromoji_user_dict"
}
},
"tokenizer": {
"kuromoji_user_dict": {
"user_dictionary": "userdict_ja.txt",
"type": "kuromoji_tokenizer",
"discard_punctuation": "false",
"mode": "extended"
}
}
},
"number_of_replicas": "1",
"number_of_shards": "5",
"version": {
"created": "2010199"
}
}
}
}
}
ちゃんとanalyzerにkuromojiが設定されました。
まとめ
elasticsearchで作成済みのindexにあれこれと再設定をするときは、Open / Close Index APIを利用するのがコツです。
elasticsearchはあまり日本語の情報がありませんが、テスト環境を構築してコツコツといじっていくのが使いこなしていく近道です。
以上です。
0 件のコメント:
コメントを投稿