Elasticsearchを試してみた
最近ではApach Solrより、リアルタイム分析や、柔軟な設計がしやすい、Elasticsearchを耳にすることがよく増えて来たので、 ちょっとお試しで、どんなものなのかを体験してみたいと思います。
バージョンは最新安定版の5.1を参考にしています。
Elasticsearchとは
全文検索エンジンで、Elastic社が中心となって開発を進めているOSS。 RESTful APIで各オペレーションが可能。クラスタ構成が前提で実装されている。
コンセプト
Cluster
データ全体を保持するサーバのことで、Nodeの集合で構成されている 別環境で同じ名前のClusterを作成してはいけない(間違ったClusterに参加するNodeになる可能性があるらしい)
Node
データを格納し、検索、インデックス等の機能を提供する単一のサーバ
Index
やや類似した特性をもつドキュメントの集合
Type
ドキュメントの型
Document
Indexの情報の基本単位 Documentは物理的にはIndexに存在するが、実際にはIndex内のTypeに割り当てられている必要がある
インストール・起動
* for Mac
$ brew install elasticsearch $ ln -svf /usr/local/Cellar/elasticsearch/5.1.1/*.plist ~/Library/LaunchAgents/ $ brew services start elasticsearch
クラスタヘルスチェック
$ curl -XGET 'localhost:9200/_cat/health?v&pretty'
インデックス一覧
$ curl -XGET 'localhost:9200/_cat/indices?v&pretty'
インデックス作成
customerというインデックスを作成
$ curl -XPUT 'localhost:9200/customer?pretty&pretty' $ curl -XGET 'localhost:9200/_cat/indices?v&pretty' health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open customer Zsm5XyEkSD2N1pLEAT1UuA 5 1 0 0 650b 650b
インデックス内にドキュメント作成
- IDを自分で指定する場合
$ curl -XPUT 'localhost:9200/customer/external/1?pretty&pretty' -d'
{
"name": "John Doe"
}'
- IDを自動生成する場合
POSTでデータを渡す
$ curl -XPOST 'localhost:9200/customer/external?pretty&pretty' -d'
{
"name": "Jane Doe"
}'
# 下記のように自動で生成してくれる
{
"_index" : "customer",
"_type" : "external",
"_id" : "AVlLRxxLmDbnb-aTGuXe",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"created" : true
}
作成したドキュメント取得
$ curl -XGET 'localhost:9200/customer/external/1?pretty&pretty'
ドキュメントの修正
- PUTの場合
$ curl -XPUT 'localhost:9200/customer/external/1?pretty&pretty' -d'
{
"name": "John Doe"
}'
- POSTの場合
$ curl -XPOST 'localhost:9200/customer/external/1/_update?pretty&pretty' -d'
{
"doc": { "name": "Jane Doe", "age": 20 }
}'
インデックス削除
$ curl -XDELETE 'localhost:9200/customer?pretty&pretty' $ curl -XGET 'localhost:9200/_cat/indices?v&pretty'
RESTful Accessの基本形
<REST Verb> /<Index>/<Type>/<ID>
Batch Process
一括で操作する方法
$ curl -XPOST 'localhost:9200/customer/external/_bulk?pretty&pretty' -d'
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }'
JSON Import
JSONデータをインポートすることが可能
$ curl -XPUT 'localhost:9200/bank?pretty&pretty'
$ curl -XPOST 'localhost:9200/bank/account/_bulk?pretty&refresh' --data-binary "@accounts.json"
$ curl -XGET 'localhost:9200/bank/account/1?pretty'
> {
"_index" : "bank",
"_type" : "account",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"account_number" : 1,
"balance" : 39225,
"firstname" : "Amber",
"lastname" : "Duke",
"age" : 32,
"gender" : "M",
"address" : "880 Holmes Lane",
"employer" : "Pyrami",
"email" : "amberduke@pyrami.com",
"city" : "Brogan",
"state" : "IL"
}
}
検索
_searchの後にパラメータを付与する
$ curl -XGET 'localhost:9200/bank/_search?q=*&sort=account_number:asc&pretty&pretty'
レスポンス
- took : 検索にかかった時間
- time_out : タイムアウトしたかどうか
- _shards : 検索されたシャードの数、成功/失敗した検索されたシャードの数を示します
- hits.total : 検索条件に一致する文書の総数
- hits.hits : 実際の検索結果の配列(デフォルトは最初の10個のドキュメント)
- sort : 結果のソートキー(スコアによるソートの場合は表示されません)