Elasticsearchで遊んでみるメモ。 日本語情報はまだ少なく、書籍のverも古いらしいので調べながら手を動かしてみる。その記録。
高速スケーラブル検索エンジン ElasticSearch Server
- 作者: Rafal Kuc・Marek Rogozin’ski,株式会社リクルートテクノロジーズ,大岩達也、大谷純、兼山元太、水戸祐介、守谷純之介
- 出版社/メーカー: KADOKAWA/アスキー・メディアワークス
- 発売日: 2014/03/21
- メディア: 大型本
- この商品を含むブログ (3件) を見る
環境
作業はwinで。
- Win 7 Pro SP1
- java 1.8.0_45
- elasticsearch 1.7.1
インストール
ここからダウンロード。 解凍後、以下を実行。
bin/service install
JAVA_HOME
がねぇと怒られる場合はJAVA_HOME
を設定。
bin
の一階層上を指定する必要があるので注意。自分はこんな感じ。
JAVA_HOME=C:\Program Files\Java\jre1.8.0_45
サービスの開始・停止
サービスの開始
bin/searvice start
サービスの停止
bin/searvice stop
アンインストール
bin/searvice remove
Node.jsから触ってみる
公式からelasticsearch.js
が提供されているので使用。
ここを見ながら遊んでみる。
接続
elasticsearch = require 'elasticsearch' client = new elasticsearch.Client host : 'localhost:9200', log : 'trace'
データを入れてみる
client.index index: 'myindex' type: 'mytype' title: 'Test 1' body: title: 'Test 1' tags: ['y', 'z'] published: true published_at: '2013-01-01' counter: 1 (error) -> console.trace error.message
データを検索してみる
client.search q : '*' .then (body) -> console.dir body.hits.hits (error) -> console.trace error.message
ヒット結果
[ { "_id": "AU9DeRjFEKFiXk3KkQZv", "_index": "myindex", "_score": 1, "_source": { "counter": 1, "published": true, "published_at": "2013-01-01", "tags": [ "y", "z" ], "title": "Test 1" }, "_type": "mytype" } ]
データを消す
データを消してみる。
index
、type
、id
は指定必須ぽい。
client.delete index: 'myindex' type: 'mytype' id: 'AU9DjDe1EKFiXk3KkQZ0' (error, response) -> console.log response
消えた。
すべてのインデックスを消してみる。
client.indices.delete index : '*' .then (body) -> (error)-> console.trace error.message
プラグインのインストール
プラグインのインストールはbin/plugin install PLUGIN_NAME
で行う。
Kuromoji Analysis Pluginのインストール
デフォルトでelasticsearch
日本語を正しく扱えないためKuromoji Analysis Plugin
を入れる。
(先日JS版kuromoji.js
を使用したのでどこか親近感ある)
elasticsearch
が1.7系の場合、プラグインは2.7を使用するらしい。
bin/plugin install elasticsearch/elasticsearch-analysis-kuromoji/2.7.0
プラグインを有効にするには再起動が必要です。
ちなみにプラグインのインストールはproxy環境下ではこけることがあるっぽくて、いろいろ調べたんだけどどれもうまくいかなくてカット&トライで以下のようにしたらインストールできた。
plugin.bat
の先頭にset JAVA_OPTS=-Dhttp.proxyHost=proxy.hoge.com -Dhttp.proxyPort=8080 -Dhttps.proxyHost=proxy.hoge.com -Dhttps.proxyPort=8080
を追記。
日本語の解析
さっそく試してみる
client.indices.analyze analyzer : 'kuromoji' tokenizer : 'kuromoji_tokenizer' text : '関西国際空港' (error, response) -> console.log response
結果
{ "tokens": [ { "token": "関西", "start_offset": 0, "end_offset": 2, "type": "word", "position": 1 }, { "token": "関西国際空港", "start_offset": 0, "end_offset": 6, "type": "word", "position": 1 }, { "token": "国際", "start_offset": 2, "end_offset": 4, "type": "word", "position": 2 }, { "token": "空港", "start_offset": 4, "end_offset": 6, "type": "word", "position": 3 } ] }
できてるっぽい。 もう一個。
client.indices.analyze analyzer : 'kuromoji' tokenizer : 'kuromoji_tokenizer' text : 'すもももももももものうち' (error, response) -> console.log response
結果
{ "tokens": [ { "token": "すもも", "start_offset": 0, "end_offset": 3, "type": "word", "position": 1 }, { "token": "もも", "start_offset": 4, "end_offset": 6, "type": "word", "position": 3 }, { "token": "もも", "start_offset": 7, "end_offset": 9, "type": "word", "position": 5 } ] }
よさそう。
日本語で検索してみる
まずはインデックス作成。
settings = index : analysis : analyzer : my_analyzer : tokenizer : 'kuromoji_tokenizer' client.indices.create {index: 'myindex', body: settings}
日本語データをmyindex
に放り込む。
探す。
client.search index: 'myindex' type : 'mytype' body : query : match : title : '関西国際空港' analyzer : 'my_analyzer' .then (body) -> console.dir body.hits.hits
すもももももももものうち
とか関西国際空港
の例で検索にヒットするんだけど、正しい挙動かどうかわからずいい例を探したところ以下の記事を見つけました。倣ってカレーは飲み物
、カレーライスは和食
で検証してみる。
タイトルがカレーは飲み物
、カレーライスは和食
のデータを放り込む。
カレー
で検索。
結果。
{ "took": 2, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 1, "max_score": 0.13424811, "hits": [ { "_index": "myindex", "_type": "mytype", "_id": "AU9ESBt8-eH7ri-OwW1j", "_score": 0.13424811, "_source": { "title": "カレーは飲み物", "tags": [ "y", "z" ], "published": true, "published_at": "2013-01-01", "counter": 1 } } ] } }
カレーは
で検索。
結果。
{ "took": 24, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 2, "max_score": 0.18985549, "hits": [ { "_index": "myindex", "_type": "mytype", "_id": "AU9ESBt8-eH7ri-OwW1j", "_score": 0.18985549, "_source": { "title": "カレーは飲み物", "tags": [ "y", "z" ], "published": true, "published_at": "2013-01-01", "counter": 1 } }, { "_index": "myindex", "_type": "mytype", "_id": "AU9ESURN-eH7ri-OwW1k", "_score": 0.12713557, "_source": { "title": "カレーライスは和食", "tags": [ "y", "z" ], "published": true, "published_at": "2013-01-01", "counter": 1 } } ] } }
ほんとだ。動いているっぽい。
バージョンの違いか、入れるデータの違いかスコアは0.18985549
と0.12713557
と大きな差はなかった。
ひとまず
機能を全然使いこなせてないけど、これで最低限動くアプリケーションは作れそうなので、なんか作ってみる。 「基礎から全部やってやるぜっ」ってやると早々に挫折するので、実装時に必要な機能をのんびり調べながらやる。
参考となりそうな記事
記事を眺めているとかなり機能が多そうで、覚えることはたくさんありそう。 困ったらこの記事達を振り返る。
実践!Elasticsearch - Wantedly Engineer Blog
Elasticsearchとkuromojiでちゃんとした日本語全文検索をやるメモ | GMOメディア エンジニアブログ