Elastic Searchをミニマムに試してみる

# TL;DR
Elastic SearchをローカルPCでミニマムに動かしてみました。

参考:
https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html

# Elastic Searchのセットアップ

Dockerで専用の`elastic`ネットワークを作成し、docker runします。
その後、docker execでコンテナ内に入り、
Elastic Searchにアクセスするための秘匿情報を取得します。

```txt:Elastic Searchの起動
[~]$ docker network create elastic
9b4c9a5d079c5cea3e7a304da98e472391bd4841dc0297df73d45710f668f765

[~]$ docker run -d --name es01 --net elastic -p 9200:9200 -it -m 1GB docker.elastic.co/elasticsearch/elasticsearch:8.13.4
Unable to find image 'docker.elastic.co/elasticsearch/elasticsearch:8.13.4' locally
8.13.4: Pulling from elasticsearch/elasticsearch
4f3de075860a: Pull complete
5e7ec49b10f4: Pull complete
be4f1231f84f: Pull complete
4ca545ee6d5d: Pull complete
6c89c5bcb8ae: Pull complete
ff6055024a22: Pull complete
fc5194d81b8b: Pull complete
da526bd63089: Pull complete
ee69d3e08081: Pull complete
d9b78d4eee23: Pull complete
Digest: sha256:dfd318b417be1356d9c7fdd6a5577c8a45553ac9d34354929a416c69c85daa9f
Status: Downloaded newer image for docker.elastic.co/elasticsearch/elasticsearch:8.13.4
47a05935d6d157625c11a80893f82a5c15a65fbf09ff82490863bd51fb53ad42
```

```txt:秘匿情報の標準出力とcrtファイルの入手
[~]$ docker exec -it es01 /usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic
WARNING: Owner of file [/usr/share/elasticsearch/config/users] used to be [root], but now is [elasticsearch]
WARNING: Owner of file [/usr/share/elasticsearch/config/users_roles] used to be [root], but now is [elasticsearch]
This tool will reset the password of the [elastic] user to an autogenerated value.
The password will be printed in the console.
Please confirm that you would like to continue [y/N]y


Password for the [elastic] user successfully reset.
New value: e2OU7YFHNg0ZmXHR*K4b

[~]$ docker exec -it es01 /usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s kibana
eyJ2ZXIiOiI4LjEzLjQiLCJhZHIiOlsiMTcyLjE5LjAuMjo5MjAwIl0sImZnciI6ImQ3YjE1NjJjMTdhOWE2Mjk4MWE4ZjAxN2NjNTc4NmE5ZTEwNjA0NTNkNWU5OWNmNDUxMjdhZDUyYWI1MDVkNjEiLCJrZXkiOiJvOGsxNG84QkgySk51N25Xd1BIcTo3ZjVrVW40TlNCS2w0Mm5ma3YwRUFRIn0=

[~]$ export ELASTIC_PASSWORD="e2OU7YFHNg0ZmXHR*K4b"

[~]$ docker cp es01:/usr/share/elasticsearch/config/certs/http_ca.crt .
Successfully copied 3.58kB to /Users/tobita_yoshiki/.
```

# インデックス作成/取得

以下はローカルで起動したElastic Searchでインデックスを作成/取得した結果です。

```txt:インデックス作成
[~]$ curl --cacert http_ca.crt -u elastic:$ELASTIC_PASSWORD -X PUT "https://localhost:9200/my-index-000001?pretty"
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "my-index-000001"
}
```

```txt:インデックス取得
[~]$ curl --cacert http_ca.crt -u elastic:$ELASTIC_PASSWORD -X GET "https://localhost:9200/my-index-000001?pretty"
{
  "my-index-000001" : {
    "aliases" : { },
    "mappings" : { },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "my-index-000001",
        "creation_date" : "1717487343042",
        "number_of_replicas" : "1",
        "uuid" : "jJ6JLLI3RiOm2vUcYg4XBw",
        "version" : {
          "created" : "8503000"
        }
      }
    }
  }
}
```

# データ投入/検索

以下はローカルで起動したElastic Searchでデータを投入した結果と、
データを全検索した結果です。

```txt:データ投入
[~]$ curl --cacert http_ca.crt -u elastic:$ELASTIC_PASSWORD -X POST "https://localhost:9200/my-index-000001/_doc" -H 'Content-Type: application/json' -d '{"k": "v"}'
{"_index":"my-index-000001","_id":"pck94o8BH2JNu7nWA_Fi","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}%
```

```txt:データ検索
[~]$ curl --cacert http_ca.crt -u elastic:$ELASTIC_PASSWORD -X GET "https://localhost:9200/my-index-000001/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match_all": {}
  }
}
'
{"took":132,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"my-index-000001","_id":"pck94o8BH2JNu7nWA_Fi","_score":1.0,"_source":{"k": "v"}}]}}%
```