LoginSignup
46
41

More than 5 years have passed since last update.

Elasticsearchを使ってみる

Last updated at Posted at 2014-01-30

Elasticsearchは、REST APIでデータ(ドキュメント)の登録や、検索を行うことができるらしい。

とりあえず、GitHubにあるREADMEの内容をやってみる。

環境

Mac OS X 10.9

ダウンロード

今回は、0.90.10 を使う

起動

run
./bin/elasticsearch -f

確認

check
curl -X GET http://localhost:9200/

起動しているようだ。

インデックス

Twitterっぽいデータを入れて検索してみる。
twitterインデックスは自動的に作成されるようだ。

ユーザの作成

curl -XPUT 'http://localhost:9200/twitter/user/kimchy' -d '{ "name" : "Shay Banon" }'

Tweetデータの登録

curl -XPUT 'http://localhost:9200/twitter/tweet/1' -d '
{ 
    "user": "kimchy", 
    "postDate": "2009-11-15T13:12:00", 
    "message": "Trying out Elasticsearch, so far so good?" 
}'

curl -XPUT 'http://localhost:9200/twitter/tweet/2' -d '
{ 
    "user": "kimchy", 
    "postDate": "2009-11-15T14:12:12", 
    "message": "Another tweet, will it be indexed?" 
}'

登録したデータを取得してみる

curl -XGET 'http://localhost:9200/twitter/user/kimchy?pretty=true'
{
  "_index" : "twitter",
  "_type" : "user",
  "_id" : "kimchy",
  "_version" : 1,
  "exists" : true, "_source" : { "name" : "Shay Banon" }
}
curl -XGET 'http://localhost:9200/twitter/tweet/1?pretty=true'
{
  "_source": {
    "message": "Trying out Elasticsearch, so far so good?",
    "postDate": "2009-11-15T13:12:00",
    "user": "kimchy"
  },
  "exists": true,
  "_version": 1,
  "_id": "1",
  "_type": "tweet",
  "_index": "twitter"
}

検索

curl -XGET 'http://localhost:9200/twitter/tweet/_search?q=user:kimchy&pretty=true'
{
  "hits": {
    "hits": [
      {
        "_source": {
          "message": "Another tweet, will it be indexed?",
          "postDate": "2009-11-15T14:12:12",
          "user": "kimchy"
        },
        "_score": 1,
        "_id": "2",
        "_type": "tweet",
        "_index": "twitter"
      },
      {
        "_source": {
          "message": "Trying out Elasticsearch, so far so good?",
          "postDate": "2009-11-15T13:12:00",
          "user": "kimchy"
        },
        "_score": 0.30685282,
        "_id": "1",
        "_type": "tweet",
        "_index": "twitter"
      }
    ],
    "max_score": 1,
    "total": 2
  },
  "_shards": {
    "failed": 0,
    "successful": 5,
    "total": 5
  },
  "timed_out": false,
  "took": 3
}

クエリ文字列の変りに、JSONクエリ言語を使うことができる

curl -XGET 'http://localhost:9200/twitter/tweet/_search?pretty=true' -d '
{ 
    "query" : { 
        "text" : { "user": "kimchy" }
    } 
}'

全てのデータを取得

curl -XGET 'http://localhost:9200/twitter/_search?pretty=true' -d '
{ 
    "query" : { 
        "matchAll" : {} 
    } 
}'

期間指定でのデータ取得

postDataの、fromとtoを指定する。

curl -XGET 'http://localhost:9200/twitter/_search?pretty=true' -d '
{ 
    "query" : { 
        "range" : { 
            "postDate" : { "from" : "2009-11-15T13:00:00", "to" : "2009-11-15T14:00:00" } 
        } 
    } 
}'

Lucene互換のクエリが使用可能とのこと。

マルチテナント

Elasticsearchは、複数のインデックスをサポートしている。
Twitter全体ではインデックスが大きくなりすぎるので、ユーザ毎にインデックスを分割する。

curl -XPUT 'http://localhost:9200/kimchy/info/1' -d '{ "name" : "Shay Banon" }'

curl -XPUT 'http://localhost:9200/kimchy/tweet/1' -d '
{ 
    "user": "kimchy", 
    "postDate": "2009-11-15T13:12:00", 
    "message": "Trying out Elasticsearch, so far so good?" 
}'

curl -XPUT 'http://localhost:9200/kimchy/tweet/2' -d '
{ 
    "user": "kimchy", 
    "postDate": "2009-11-15T14:12:12", 
    "message": "Another tweet, will it be indexed?" 
}'

上記の例では、infoとtweetに分割されている。

シャードとレプリカ

インデックスレベルの完全な制御を行うことができる。
この例では、インデックス毎に、1シャードと1レプリカとなっている。

curl -XPUT http://localhost:9200/another_user/ -d '
{ 
    "index" : { 
        "numberOfShards" : 1, 
        "numberOfReplicas" : 1 
    } 
}'

複数インデックスにまたがる検索

curl -XGET 'http://localhost:9200/kimchy,another_user/_search?pretty=true' -d '
{ 
    "query" : { 
        "matchAll" : {} 
    } 
}'

もしくは、全てのインデックス

curl -XGET 'http://localhost:9200/_search?pretty=true' -d '
{ 
    "query" : { 
        "matchAll" : {} 
    } 
}'
46
41
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
46
41