今回はかなり流行っていると思われるElasticsearchを動かしてみます。
Elasticsearchはリアルタイム分散検索&分析エンジンで、RESTfulなAPIを提供し、オープンソースなのが特徴です。Kibanaという見た目がよい可視化分析ツールも利用できることから、人気があるようです。Treasure DataでもおなじみのJSON形式で設定や検索ができます。
公式サイトのgetting startedを参照し、elasticsearchを設定します。
【環境】
CentOS release 6.5 (Final)
CentosにElasticsearchを設定します。現在の最新バージョンである1.1.1を利用しました。
まず、ダウンロードページから取得します。その後、解凍して起動します。
参照:installing elasticsearch
$ wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.1.1.tar.gz $ tar zxf elasticsearch-1.1.1.tar.gz $ cd elasticsearch-1.1.1 $ bin/elasticsearch
別のターミナルを起動して、下記コマンドを実行します。
正しく起動できていれば、status 200が表示されます。
(?prettyは結果のJSONを見やすく表示してくれるオプションです。?prettyなしで実行すると結果は1行で表示されます。)
$ curl 'https://localhost:9200/?pretty'
{
"status" : 200,
"name" : "Cheetah",
"version" : {
"number" : "1.1.1",
"build_hash" : "f1585f096d3f3985e73456debdc1a0745f512bbc",
"build_timestamp" : "2014-04-16T14:27:12Z",
"build_snapshot" : false,
"lucene_version" : "4.7"
},
"tagline" : "You Know, for Search"
}
curlコマンドを利用して、を利用することができます。 クラスター中にいくつのレコードがあるかを検索します。
$ curl -XGET 'https://localhost:9200/_count?pretty' -d '
{
"query": {
"match_all": {}
}
}'
データを投入してみます。 パス部分のmegacorpがindexという普通のDBでいうところのDB名にあたり、employeeがtypeという普通のDBでいうところのテーブルになります。最後の1はemployee中の一意なIDになります。
$ curl -XPUT 'https://localhost:9200/megacorp/employee/1?pretty' -d '
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}'
下記の結果が表示されました。
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "1",
"_version" : 1,
"created" : true
}
続けて、2件のデータを投入します。
$ curl -XPUT 'https://localhost:9200/megacorp/employee/2?pretty' -d '
{
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests": [ "music" ]
}'
$ curl -XPUT 'https://localhost:9200/megacorp/employee/3?pretty' -d '
{
"first_name" : "Douglas",
"last_name" : "Fir",
"age" : 35,
"about": "I like to build cabinets",
"interests": [ "forestry" ]
}'
投入した1番めのデータの内容を参照してみます。
$ curl -XGET 'https://localhost:9200/megacorp/employee/1?pretty'
ID=1のデータが表示されました。
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "1",
"_version" : 1,
"found" : true, "_source" :
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
}
次に検索をしてみましょう。
$ curl -XGET 'https://localhost:9200/megacorp/employee/_search?pretty'
employeeのデータが表示されます。次に条件を付けてみます。q=の後ろに検索条件を指定します。
$ curl -XGET 'https://localhost:9200/megacorp/employee/_search?pretty&q=last_name:Smith'
・・・
"hits" : {
"total" : 2,
"max_score" : 0.30685282,
"hits" : [ {
・・・
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
}
, {
・・・
{
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests": [ "music" ]
}
} ]
}
}
2件のデータが検索されました。
条件をJSON形式で指定することもできます。
$ curl -XGET 'https://localhost:9200/megacorp/employee/_search?pretty' -d '
{
"query" : {
"match" : {
"first_name" : "Jane"
}
}
}'
今回は検索を行っただけですが、Java,JavaScript,PHP,Perl,Rubyなどから利用でき、Restful APIでも利用可能で、JSON形式で結果が取得できると言う点とわかりやすさが人気の秘訣かなと感じました。これからも注目して行きたいと思います。
