がじぇ

お金と家電とプログラミングのブログ

Elasticsearchのテスト環境をDockerで構築する

こんにちわ

がじぇったー (@hackmylife7) | Twitter


です。



仕事でelasticsearchを使う機会がありましたので、検証のため、Dockerを使ってます。
その備忘録になります。



検証環境の構築

MacでDockerのアプリを立ち上げたあとimageをPullしましょう

❯ docker pull docker.elastic.co/elasticsearch/elasticsearch:7.4.0


Docker runして立ち上げましょう

❯ docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.4.0

立ち上がると以下の起動ログが出力されます

{"type": "server", "timestamp": "2019-10-17T13:14:09,851Z", "level": "INFO", "component": "o.e.x.s.s.SecurityStatusChangeListener", "cluster.name": "docker-cluster", "node.name": "85ff8f03d56a", "message": "Active license is now [BASIC]; Security is disabled", "cluster.uuid": "lQ0kW48ZSPKQ82V7z6QD8w", "

別にターミナルを立ち上げてcurlコマンドでstatusを確認します。
以下の応答があれば、無事立ち上がってます。

❯ curl -X GET "localhost:9200/_cat/health?v&pretty"
epoch      timestamp cluster        status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1571318234 13:17:14  docker-cluster green           1         1      0   0    0    0        0             0                  -                100.0%

データの登録

PUTしてindexを作成しましょう

❯ curl -X PUT "localhost:9200/test_index?pretty&pretty"{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "test_index"
}


確認します。
priがプライマリシャード、repがレプリカシャードを意味するのですが、1ずつになっていればOKです

❯ curl -X GET "localhost:9200/_cat/indices?v&pretty"
health status index      uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   test_index YXirYMUIRgq50ynrKKc-qQ   1   1          0            0       230b           230b

続いてドキュメントを登録します。

❯ curl -X PUT "localhost:9200/test_index/_doc/1?pretty&pretty" -H 'Content-Type: application/json' -d'
> {
>   "test_key": "test_value"
> }
> '

↓レスポンス

{
  "_index" : "test_index",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

作成したドキュメントを取得しましょう。

登録したドキュメントが取得できていることがよくわかります。

❯ curl -X GET "localhost:9200/test_index/_doc/1?pretty&pretty"

↓出力

{
  "_index" : "test_index",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "test_key" : "test_value"
  }
}


jsonコマンドラインでハードコードするのはお辛いので、別ファイルに出しましょう。
accounts.jsonをダウンロードします。


curlコマンドでjsonファイルを指定して登録します。indexはfile_testを指定します。
indexは事前に作らなくても新しいものが指定されると勝手に登録されます。

❯ curl -H "Content-Type: application/json" -XPOST "localhost:9200/file_test/_bulk?pretty&refresh" --data-binary "@accounts.json"

自分でテストデータを作成して登録する際はjsonの形に注意してください。整形したままだと登録に失敗します。

{
    "index": {
        "_id": "92"
    }
} {
    "settings": {
        "index": {
            "number_of_shards": 1,
            "number_of_replicas": 1
        }
    }
}

以下のように詰める必要があります。
また、最終行は改行が必要です

{"index":{"_id":"92"}}
{"settings":{"index":{"number_of_shards":1,"number_of_replicas":1}}}


Elasticsearch実践ガイド (impress top gear)

Elasticsearch実践ガイド (impress top gear)