the industrial

ブログと言うより自分のためのメモ以外の何モノでもないです。でも読んでくださってありがとうございます。

Elasticsearch入門してます(1日目) インストール〜Index作成

最近、お仕事で割りとElasticsearchを使ったりしているのだけど、なんというかいつも行き当たりばったりで出来た・出来ないだのやっていて気持ち悪かったのと、Aggregationがとてもおもしろいので、この際しっかり1から勉強しようと思い立った。

そこで、この冬休みを利用して、いろいろなデータを登録して、いろいろなAggregationをやっていきたい...のだけど、最近ちょっとものぐさな人間になっちゃってるのでどこまでできるかわからないw

そしてこのエントリーは個人的なメモ以外の何物でもないので、誰かの役に立ちたいとかそういった内容ではない。

(Elasticsearchの勉強だったり調べ物するならば、こんなブログじゃなくて公式サイトなどを見たほうが100倍有意義な内容となっております。)

インストール

僕は普段Macを使っているので、Homebrewで入れちゃう。バージョンは2.1.1だった。

$ brew install elasticsearch
==> Downloading https://download.elasticsearch.org/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.1.1/elasticsearch
Already downloaded: /Library/Caches/Homebrew/elasticsearch-2.1.1.tar.gz
==> Caveats
Data:    /usr/local/var/elasticsearch/elasticsearch_omiend/
Logs:    /usr/local/var/log/elasticsearch/elasticsearch_omiend.log
Plugins: /usr/local/Cellar/elasticsearch/2.1.1/libexec/plugins/
Config:  /usr/local/etc/elasticsearch/

To have launchd start elasticsearch at login:
  ln -sfv /usr/local/opt/elasticsearch/*.plist ~/Library/LaunchAgents
Then to load elasticsearch now:
  launchctl load ~/Library/LaunchAgents/homebrew.mxcl.elasticsearch.plist
Or, if you don't want/need launchctl, you can just run:
  elasticsearch
==> Summary
🍺  /usr/local/Cellar/elasticsearch/2.1.1: 52 files, 31M, built in 1 second

起動

elasticsearch コマンドを打つと、なんか立ち上がる。

$ elasticsearch
[2015-12-29 22:51:22,052][INFO ][node                     ] [Master Menace] version[2.1.1], pid[22285], build[40e2c53/2015-12-15T13:05:55Z]
[2015-12-29 22:51:22,053][INFO ][node                     ] [Master Menace] initializing ...
[2015-12-29 22:51:22,178][INFO ][plugins                  ] [Master Menace] loaded [], sites []
〜割愛〜

稼働確認

基本的にcurlでやっていくのだけど、marvelというプラグインが好きなので、後で入れておこうかなと。

$ curl -XGET http://localhost:9200/
{
  "name" : "Master Menace",
  "cluster_name" : "elasticsearch_brew",
  "version" : {
    "number" : "2.1.1",
    "build_hash" : "40e2c53a6b6c2972b3d13846e450e66f4375bd71",
    "build_timestamp" : "2015-12-15T13:05:55Z",
    "build_snapshot" : false,
    "lucene_version" : "5.3.1"
  },
  "tagline" : "You Know, for Search"
}

Index登録用定義ファイル

Indexとは、RDBで言うところのDatabase。

まず、定義ファイルを作成する。今回やりたいのは時系列でのAggregationだったりするので、なんとなくこんな感じで定義してみた。

ファイル名は mapping_events.json とした。

Indexの名称は events

{
  "mappings": {
    "events": {
      "_source": {
        "enabled": true
      },
      "_all": {
        "enabled": false
      },
      "_timestamp": {
        "enabled": true,
        "path": "createdDate"
      },
      "properties": {
        "title": {
          "type": "string",
          "index": "not_analyzed"
        },
        "createdDate": {
          "type": "date",
          "format": "YYYY-MM-dd'T'HH:mm:ss.SSSZ"
        },
        "updatedDate": {
          "type": "date",
          "format": "YYYY-MM-dd'T'HH:mm:ss.SSSZ"
        }
      }
    }
  }
}

個人的にElasticsearchの良いな〜と思っていることの一つに、Elasticsearchとのやり取りはクエリーでもなんでも基本的にJSON形式でやり取りする。

やはり何をしててもRDBとの比較になってしまうのだけど、クエリーの組み立てはなかなか慣れない。

だけど、最近なんとなく間隔がつかめて来て、もしかしたらSQLよりもわかりやすく、柔軟なクエリーが掛けるのではないかなんて思ったりもしている。

Indexの登録

$ curl -XPOST http://localhost:9200/events --data @mapping_events.json
{"acknowledged":true}

Indexが登録されたことを確認

$ curl -XGET localhost:9200/_aliases
{"events":{"aliases":{}}}

ちなみに、Request Parameterもいろいろあるらしく、 pretty をつけるとこうなったり...

$ curl -XGET localhost:9200/_aliases?pretty
{
  "events" : {
    "aliases" : { }
  }
}

format=yaml とかつけたりするとyams形式で返してくれたり

$ curl -XGET localhost:9200/_aliases?format=yaml
---
events:
  aliases: {}

Indexの削除

単純

$ curl -XDELETE http://localhost:9200/events
{"acknowledged":true}

次回はデータを登録して、簡単なクエリーを投げるつもり。

omiend.hatenablog.jp

omiend.hatenablog.jp

omiend.hatenablog.jp