Elasticsearch5とKibana5をインストールしてCRUDを試す
Elasticsearch5とKibana5をUbuntuにインストールしてCRUDを試してたメモです。
今更ながらやっとElasticsearch触ってみました。
とりあえずVirtualBox上のUbuntuにElasticsearch5とKibana5をインストールして、
CRUD操作をしてみました。
Elasticsearch5 インストール
現時点の最新版のElasticsearch5.1.2をUbuntuにインストールしてみます。
インストールはとても簡単です。
ソースをダウンロードして、展開するだけです。
$ mkdir elastic $ cd elastic/ $ curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.1.2.tar.gz $ tar -xvf elasticsearch-5.1.2.tar.gz
起動
$ cd elasticsearch-5.1.2/bin/ $ ./elasticsearch : : [2017-01-20T20:10:52,166][INFO ][o.e.h.HttpServer ] [-MpwHHo] publish_address {127.0.0.1:9200}, bound_addresses {[::1]:9200}, {127.0.0.1:9200} [2017-01-20T20:10:52,172][INFO ][o.e.n.Node ] [-MpwHHo] started
Kibana5 インストール
同じサーバに現時点の最新版のKibana5.1.2をインストールしてみます。
こちらもソースをダウンロードして、展開するだけです。
$ cd elastic/ $ curl -L -O https://artifacts.elastic.co/downloads/kibana/kibana-5.1.2-linux-x86_64.tar.gz $ tar -xvf kibana-5.1.2-linux-x86_64.tar.gz
起動
$ cd kibana-5.1.2-linux-x86_64/ $ ./bin/kibana : : log [11:19:27.394] [info][listening] Server running at http://localhost:5601 log [11:19:27.401] [info][status][ui settings] Status changed from uninitialized to yellow - Elasticsearch plugin is yellow log [11:19:32.469] [info][status][plugin:elasticsearch@5.1.2] Status changed from yellow to yellow - No existing Kibana index found log [11:19:33.415] [info][status][plugin:elasticsearch@5.1.2] Status changed from yellow to green - Kibana index ready log [11:19:33.416] [info][status][ui settings] Status changed from yellow to green - Ready
ホストOSからKibanaにアクセスする
Kibanaはデフォルトでは5601ポートで開いてるので、
Ubuntu上で http://localhost:5601 でアクセス出来ます。
しかし、ホストOSからVirtualBox上のKibanaにアクセスしようとしても出来なくて困りました。
色々調べてみると原因はこちらでした。
https://www.elastic.co/guide/en/kibana/current/breaking-changes-5.0.html#_kibana_binds_to_localhost_by_default
Kibana5(Elasticsearch5)からはlocalhostにバインドされるようになったようです。
それ以前は0.0.0.0にバインディングされており、どんなIPからのアクセスも受け付けていました。
という訳で、ifconfigでKibanaのサーバのIPを調べておき、
$ ifconfig
kibana.ymlにserver.hostを追記します。
$ cd config/
$ vi kibana.yml
server.host: 10.0.2.15
これでKibanaを再起動すると、ホストOSからアクセス出来るようになりました。
Console
RESTの送受信をKibanaのConsoleというツールを使って試してみます。
素でリクエストを投げてもいいのですが、Consoleを使うとサジェスト機能や整形など色々便利なようです。
以前はSenseという名のプラグインでしたが、Kibana5からは標準で組み込まれるようになりました。
Console | Kibana User Guide [5.4] | Elastic
Kibanaにアクセスして、左側のDev Toolsをクリックすると下記画面が表示されます。
左側のペインにリクエストを記述して、▶ボタンを押すとリクエストが実行され、
右側のペインにレスポンスが表示されます。
CRUD
Consoleからインデックスの参照、登録、更新、削除してみます。
登録
PUTメソッドで登録します。
peopleインデックスのfriendsタイプにid:1でドキュメントを登録。
PUT /people/friends/1 { "name": { "first": "Ritchie", "last": "Black" }, "country": "Britain", "age": 60 }
レスポンス。
{ "_index": "people", "_type": "friends", "_id": "1", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "created": true }
_shardsにインデックス操作の結果が入ってきます。
・total:インデックス操作するシャードコピー数
・successful:成功したシャードコピー数
・failed:失敗したシャードコピー数
Consoleだとこんな感じでリクエスト/レスポンスが表示されます。
登録したデータを参照
GET /people/friends/1
レスポンス。
{ "_index": "people", "_type": "friends", "_id": "1", "_version": 1, "found": true, "_source": { "name": { "first": "Ritchie", "last": "Black" }, "country": "Britain", "age": 60 } }
POSTを使用して登録すると、idが自動採番されます。
idを指定せず登録。
POST /people/friends/ { "name": { "first": "John", "last": "Lord" }, "country": "Britain", "age": 61 }
レスポンス。
_idが自動採番されて返ってきています。
{ "_index": "people", "_type": "friends", "_id": "AVnGf8C90nxsAlq4Ha9u", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "created": true }
自動採番されたidを指定して参照。
GET /people/friends/AVnGf8C90nxsAlq4Ha9u
レスポンス。
{ "_index": "people", "_type": "friends", "_id": "AVnGf8C90nxsAlq4Ha9u", "_version": 1, "found": true, "_source": { "name": { "first": "John", "last": "Lord" }, "country": "Britain", "age": 61 } }
更新
PUTメソッドで更新。
更新する値を記述します。更新しないフィールドは省略できません。
PUT /people/friends/1 { "name": { "first": "Ritchie", "last": "Black" }, "country": "America", "age": 66 }
レスポンス。
{ "_index": "people", "_type": "friends", "_id": "1", "_version": 3, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "created": false }
確認。
GET /people/friends/1
レスポンス。更新されている。
{ "_index": "people", "_type": "friends", "_id": "1", "_version": 3, "found": true, "_source": { "name": { "first": "Ritchie", "last": "Black" }, "country": "America", "age": 66 } }
POSTメソッドで部分更新。
_updateを指定して、docフィールドに更新するフィールドだけ指定すると部分更新が出来ます。
POST /people/friends/1/_update { "doc": { "country": "Japan", "age": 72 } }
レスポンス。
{ "_index": "people", "_type": "friends", "_id": "1", "_version": 4, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 } }
確認。
GET /people/friends/1
レスポンス。更新されている。
{ "_index": "people", "_type": "friends", "_id": "1", "_version": 4, "found": true, "_source": { "name": { "first": "Ritchie", "last": "Black" }, "country": "Japan", "age": 72 } }
削除
DELETEメソッドでインデックスから対象のドキュメントを削除する。
id:1のドキュメントを削除。
DELETE /people/friends/1
レスポンス。
{ "found": true, "_index": "people", "_type": "friends", "_id": "1", "_version": 5, "result": "deleted", "_shards": { "total": 2, "successful": 1, "failed": 0 } }
確認。
GET /people/friends/1 レスポンス。削除したため見つからない。 >|json| { "_index": "people", "_type": "friends", "_id": "1", "found": false }
peopleインデックスを削除。
DELETE /people
レスポンス。acknowledged:trueは正常に削除された事を示す。
{ "acknowledged": true }
確認。
GET /people/friends/AVnGf8C90nxsAlq4Ha9u
レスポンス。削除したため見つからない。
{ "error": { "root_cause": [ { "type": "index_not_found_exception", "reason": "no such index", "resource.type": "index_expression", "resource.id": "people", "index_uuid": "_na_", "index": "people" } ], "type": "index_not_found_exception", "reason": "no such index", "resource.type": "index_expression", "resource.id": "people", "index_uuid": "_na_", "index": "people" }, "status": 404 }
Elasticsearchの超基本的な事だけ確認しました。
下記2つの公式ドキュメントを参考にしました。
Elasticsearch Reference [5.1] | Elastic
Elasticsearchを始めてみよう | Elastic
終わり。