ElasticSearch導入のための学習とまとめ。
環境
Talking to Elasticsearch
Elasticsearchへリクエストを送る場合、以下の構文を使用する
curl -X<VERB> '<PROTOCOL>://<HOST>/<PATH>?<QUERY_STRING>' -d '<BODY>'
例:クラスターの中のドキュメントの数をカウント
コマンドで叩きます
curl -XGET 'http://192.168.33.19:9200/_count?pretty' -d '
{
    "query": {
        "match_all": {}
    }
}
'
// 結果
{
  "count" : 0,
  "_shards" : {
    "total" : 0,
    "successful" : 0,
    "failed" : 0
  }
}
まだ何もやってないので、全部0なのかな。多分。header情報も見たいときはcurlに-iオプションを追加する。
curl -i -XGET 'http://192.168.33.19:9200/'
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 347
{
  "status" : 200,
  "name" : "Hate-Monger",
  "cluster_name" : "elasticsearch-changed",
  "version" : {
    "number" : "1.4.2",
    "build_hash" : "927caff6f05403e936c20bf4529f144f0c89fd8c",
    "build_timestamp" : "2014-12-16T14:11:12Z",
    "build_snapshot" : false,
    "lucene_version" : "4.10.2"
  },
  "tagline" : "You Know, for Search"
}
ちゃんとheaderが表示できましたね。
Document Oriented
Elasticsearchはドキュメントベースでデータを管理し、jsonを利用している。indexもドキュメント。
Finding Your Feet
チュートリアルをやってElasticsearchを理解しよう。ここでは従業員のディレクトリを作ってチャレンジするよ。要件は以下
    
      - 複数のタグ、値、数値、テキストを含むデータを利用できる
- あらゆる従業員の詳細を検索する
- 30歳以上の従業員を見つけるような、構造化された検索を許可
- 単純なフルテキスト検索と、より複雑なフレーズ検索を許可
- 一致するドキュメントのテキストの中から、強調された検索の断片を返す
- 分析的なダッシュボードを作成できる
Indexing Employee Documents
Elasticsearchにデータを保存することを「indexing」と呼ぶ。
「indexing」の前に保存場所を決める。
Elasticsearchでは、documentはtypeに属する。これらのtypeはindexの中にあるよ。
Relational DB  ⇒ Databases ⇒ Tables ⇒ Rows      ⇒ Columns
Elasticsearch  ⇒ Indices   ⇒ Types  ⇒ Documents ⇒ Fields
データの登録
curl -XPUT 'http://192.168.33.19:9200/megacorp/employee/1' -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}
/megacorp/employee/1に3つの情報が含まれているのに注意。
megacorp : The index name
employee : The type name
1 :The ID of this particular:employee
うまく登録できたみたいなので、ブラウザから叩く。
// ブラウザで叩く
http://192.168.33.19:9200/megacorp/employee/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 -XPUT 'http://192.168.33.19:9200/megacorp/employee/2' -d '
{
    "first_name" :  "Jane",
    "last_name" :   "Smith",
    "age" :         32,
    "about" :       "I like to collect rock albums",
    "interests":  [ "music" ]
}
'
// 登録
curl -XPUT 'http://192.168.33.19:9200/megacorp/employee/3' -d '
{
    "first_name" :  "Douglas",
    "last_name" :   "Fir",
    "age" :         35,
    "about":        "I like to build cabinets",
    "interests":  [ "forestry" ]
}
'
// 確認
curl -XGET 'http://192.168.33.19:9200/_count?pretty' -d '
{
    "query": {
        "match_all": {}
    }
}
'
{
  "count" : 3,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  }
}
Search Lite
簡単な検索の学習。まずは「単純検索」
// ブラウザ
http://192.168.33.19:9200/megacorp/employee/_search
{"took":22,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":3,"max_score":1.0,"hits":[{"_index":"megacorp","_type":"employee","_id":"1","_score":1.0,"_source":
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}
},{"_index":"megacorp","_type":"employee","_id":"2","_score":1.0,"_source":
{
    "first_name" :  "Jane",
    "last_name" :   "Smith",
    "age" :         32,
    "about" :       "I like to collect rock albums",
    "interests":  [ "music" ]
}
},{"_index":"megacorp","_type":"employee","_id":"3","_score":1.0,"_source":
{
    "first_name" :  "Douglas",
    "last_name" :   "Fir",
    "age" :         35,
    "about":        "I like to build cabinets",
    "interests":  [ "forestry" ]
}
}]}}
「条件を付加して検索」
// ブラウザ
http://192.168.33.19:9200/megacorp/employee/_search?q=last_name:Smith
{"took":22,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":2,"max_score":0.30685282,"hits":[{"_index":"megacorp","_type":"employee","_id":"1","_score":0.30685282,"_source":
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}
},{"_index":"megacorp","_type":"employee","_id":"2","_score":0.30685282,"_source":
{
    "first_name" :  "Jane",
    "last_name" :   "Smith",
    "age" :         32,
    "about" :       "I like to collect rock albums",
    "interests":  [ "music" ]
}
}]}}
長くなるので分割。
次はSearch with Query DSLからです。
どこまできちんとメモするか微妙です。
      
この記事がお役にたちましたらシェアをお願いします