前回(Elasticsearch5.6.1(最新版)インストール手順 tar.gz)でElasticsearchをインストールしましたが、今回はそのElasticsearchに使用するcurlコマンドの使い方についてご紹介していきます。
Elasticsearchバージョン
$ /usr/local/elasticsearch/bin/elasticsearch -V Version: 5.6.1, Build: 667b497/2017-09-14T19:22:05.189Z, JVM: 9
Elasticsearch curlコマンド基礎(REST API)
1. Elasticsearchの情報取得
Elasticsearchでは、以下のようにHTTPのリクエストを送ることで、JSONの表記で情報を取得できます。
※JSONについては → JavaScript Object Notation – Wikipedia
$ curl -XGET 'localhost:9200/?pretty' ------------------------------------------------- { "name" : "yUTLDl8", "cluster_name" : "elasticsearch", "cluster_uuid" : "8b0C5hPrTx2mIWx883sfWw", "version" : { "number" : "5.6.1", "build_hash" : "667b497", "build_date" : "2017-09-14T19:22:05.189Z", "build_snapshot" : false, "lucene_version" : "6.6.1" }, "tagline" : "You Know, for Search" } -------------------------------------------------
2. クラスタのヘルスチェックができる
$ curl -XGET localhost:9200/_cluster/health?pretty ------------------------------------------------- { "cluster_name" : "elasticsearch", "status" : "green", "timed_out" : false, "number_of_nodes" : 1, "number_of_data_nodes" : 1, "active_primary_shards" : 0, "active_shards" : 0, "relocating_shards" : 0, "initializing_shards" : 0, "unassigned_shards" : 0, "delayed_unassigned_shards" : 0, "number_of_pending_tasks" : 0, "number_of_in_flight_fetch" : 0, "task_max_waiting_in_queue_millis" : 0, "active_shards_percent_as_number" : 100.0 }
ちなみに、リクエストの最後についている「?pretty」の表記についてですが、
指定しない場合は通常改行が省略されたJSONオブジェクトを返します。
指定すると改行を入れてJSONが返されます。
・「?pretty」を指定しない場合 $ curl -XGET localhost:9200/_cluster/health ------------------------------------------------- {"cluster_name":"elasticsearch","status":"green","timed_out":false,"number_of_nodes":1,"number_of_data_nodes":1,"active_primary_shards":0,"active_shards":0,"relocating_shards":0,"initializing_shards":0,"unassigned_shards":0,"delayed_unassigned_shards":0,"number_of_pending_tasks":0,"number_of_in_flight_fetch":0,"task_max_waiting_in_queue_millis":0,"active_shards_percent_as_number":100.0} ------------------------------------------------- ・「?pretty」を指定する場合 $ curl -XGET localhost:9200/_cluster/health?pretty ------------------------------------------------- { "cluster_name" : "elasticsearch", "status" : "green", "timed_out" : false, "number_of_nodes" : 1, "number_of_data_nodes" : 1, "active_primary_shards" : 0, "active_shards" : 0, "relocating_shards" : 0, "initializing_shards" : 0, "unassigned_shards" : 0, "delayed_unassigned_shards" : 0, "number_of_pending_tasks" : 0, "number_of_in_flight_fetch" : 0, "task_max_waiting_in_queue_millis" : 0, "active_shards_percent_as_number" : 100.0 } -------------------------------------------------
3. クラスタとノードのシャットダウン
以前のバージョンでは、以下コマンドでクラスタや各ノードのシャットダウンができたのですが、どうやら、5.6からはなくなってしまったようです。
「curl -XPOST http://localhost:9200/_cluster/nodes/_shutdown」
「curl -XPOST http://localhost:9200/_cluster/nodes/[各ノードのID]/_shutdown」
The
_shutdown
API has been removed. Instead, setup Elasticsearch to run as a service (see Install Elasticsearch with RPM, Install Elasticsearch with Debian Package, or Install Elasticsearch with Windows MSI Installer) or use the-p
command line option to write the PID to a file.
Nodes shutdown Elasticsearch Reference [5.6] Elastic
4. ドキュメントの作成(PUT)
成功すると以下の出力のように「 “successful” : 1,」が表示されるかと思います。
$ curl -XPUT http://localhost:9200/awesomeblog/article/1?pretty -d' { "title":"Elasticsearch Curl Command", "post_date" : "2017-09-28T02:10:32", "content":"Today, I am gonna talk about Elasticsearch curl command.", "keyword":["elasticsearch","curl","command"] }' ---------------------------------- { "_index" : "awesomeblog", "_type" : "article", "_id" : "1", "_version" : 7, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "created" : true } ----------------------------------
PUTの場合は、ドキュメントID(ここでは、「1」)は省略できないので注意。
省略した場合は以下のようなエラーになります。
$ curl -XPUT http://localhost:9200/awesomeblog/article?pretty -d' { "title":"Elasticsearch Curl Command", "post_date" : "2017-09-28T02:10:32", "content":"Today, I am gonna talk about Elasticsearch curl command.", "keyword":["elasticsearch","curl","command"] }' No handler found for uri [/awesomeblog/article?pretty] and method [PUT]
5. ドキュメントの作成(POST)
POSTの場合は、ドキュメントIDを省略できます。
省略するとIDが自動的に生成されます。( ここでは、id は “AV7EOwllskDC3tOpZR4k”で自動生成されている)
$ curl -XPOST http://localhost:9200/awesomeblog/article?pretty -d' { "title":"Elasticsearch Curl Command", "post_date" : "2017-09-28T02:10:32", "content":"Today, I am gonna talk about Elasticsearch curl command.", "keyword":["elasticsearch","curl","command"] }' ---------------------------------- { "_index" : "awesomeblog", "_type" : "article", "_id" : "AV7EOwllskDC3tOpZR4k", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "created" : true } ----------------------------------
6. ドキュメントの取得
項番4のPUTで作ったやつを取得。
$ curl -XGET http://localhost:9200/awesomeblog/article/1?pretty ------------------------------------------------- { "_index" : "awesomeblog", "_type" : "article", "_id" : "1", "_version" : 7, "found" : true, "_source" : { "title" : "Elasticsearch Curl Command", "post_date" : "2017-09-28T02:10:32", "content" : "Today, I am gonna talk about Elasticsearch curl command.", "keyword" : [ "elasticsearch", "curl", "command" ] } } -------------------------------------------------
項番5のPOSTで作ったやつを取得。
$ curl -XGET http://localhost:9200/awesomeblog/article/AV7EOwllskDC3tOpZR4k?pretty ------------------------------------------------- { "_index" : "awesomeblog", "_type" : "article", "_id" : "AV7EOwllskDC3tOpZR4k", "_version" : 1, "found" : true, "_source" : { "title" : "Elasticsearch Curl Command", "post_date" : "2017-09-28T02:10:32", "content" : "Today, I am gonna talk about Elasticsearch curl command.", "keyword" : [ "elasticsearch", "curl", "command" ] } } -------------------------------------------------
7. ドキュメントの更新
ドキュメントの置き換え
リクエストを「_update」として、「ctx._source.[置き換えたいフィールドを指定]」のようにします。
$ curl -XPOST http://localhost:9200/awesomeblog/article/1/_update?pretty -d' { "script":"ctx._source.content=\"First, I am gonna talk about CURL COMMAND.\"" }' ------------------------------------------------- { "_index" : "awesomeblog", "_type" : "article", "_id" : "1", "_version" : 13, "result" : "updated", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 } } ------------------------------------------------- $ curl -XGET http://localhost:9200/awesomeblog/article/1?pretty ------------------------------------------------- { "_index" : "awesomeblog", "_type" : "article", "_id" : "1", "_version" : 13, "found" : true, "_source" : { "title" : "Elasticsearch Curl Command", "post_date" : "2017-09-28T02:10:32", "content" : "First, I am gonna talk about CURL COMMAND.", "keyword" : [ "elasticsearch", "curl", "command" ] } } -------------------------------------------------
ドキュメントの追記
「ctx._source.content」の部分を「=」ではなく「+=」とすると追記することができます。
$ curl -XPOST http://localhost:9200/awesomeblog/article/1/_update?pretty -d' { "script":"ctx._source.content+=\"Second, I am gonna talk about kibana.\"" }' ------------------------------------------------- { "_index" : "awesomeblog", "_type" : "article", "_id" : "1", "_version" : 14, "result" : "updated", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 } } ------------------------------------------------- $ curl -XGET http://localhost:9200/awesomeblog/article/1?pretty ------------------------------------------------- { "_index" : "awesomeblog", "_type" : "article", "_id" : "1", "_version" : 14, "found" : true, "_source" : { "title" : "Elasticsearch Curl Command", "post_date" : "2017-09-28T02:10:32", "content" : "First, I am gonna talk about CURL COMMAND.Second, I am gonna talk about kibana.", "keyword" : [ "elasticsearch", "curl", "command" ] } } -------------------------------------------------
8. ドキュメントの削除
「-XDELETE」を使って削除します。
削除後、「GET」した結果が「”found” : false」になっているので、削除されたことがわかります。
$ curl -XDELETE http://localhost:9200/awesomeblog/article/1?pretty ------------------------------------------------- { "found" : true, "_index" : "awesomeblog", "_type" : "article", "_id" : "1", "_version" : 15, "result" : "deleted", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 } } ------------------------------------------------- $ curl -XGET http://localhost:9200/awesomeblog/article/1?pretty ------------------------------------------------- { "_index" : "awesomeblog", "_type" : "article", "_id" : "1", "found" : false } -------------------------------------------------
お疲れ様でした。
次回以降は、ユーザ辞書やシノニム、プラグインを活用した方法をご紹介していきたいと思います。
コメント