Elastic社のElastic Cloudで、Elasticsearchサーバーに対してCORSを設定する


Elasticsearchで試していると、ひとまず手取り早く試したいという時がくると思います。
その時のメモです。

Elastic社のElastic Cloudに作成したElasticsearchに対して、直接クエリを投げるWebアプリを(テスト的に)作った際に、デフォルトの設定のままではCORSの制限に引っかかります。

以下ドキュメントから
Add Elasticsearch user settings | Elasticsearch Service Documentation | Elastic

1. Log in to the Elasticsearch Service Console.

2. Select your deployment on the home page in the Elasticsearch Service card or go to the deployments page.

3. From your deployment menu, go to the Edit page.

4. At the bottom of each Elasticsearch node, expand the User settings overrides caret.

5. Update the user settings.

開いたエディタ部分にHTTPの設定を入れる

http:
  cors.enabled: true
  cors.allow-origin: /https?:\/\/(localhost(:[0-9]+)?|xxxxx.github.io)/
  cors.allow-methods: GET
  cors.allow-headers: Authorization, X-Requested-With,X-Auth-Token,Content-Type, Content-Length

※以下の2つからのアクセスを許可する設定にしています。

  • localhost
  • xxxxx.github.io

6. Click Save changes.

Saveボタンを押すとサーバーの更新が始まります。

ローディングが終わったら設定が反映されています。

コンソールログにもエラーは出なくなりました。

参考

Add Elasticsearch user settings | Elasticsearch Service Documentation | Elastic

HTTP | Elasticsearch Reference [7.9] | Elastic

YML 設定 - Elasticsearch インスタンス| Alibaba Cloud ドキュメントセンター