0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【備忘録】【エラー解決】npm run startでnode serverを立ち上げたら400系エラー発生

Last updated at Posted at 2021-08-28

下記コマンドでnodeのSSRサーバーを立ち上げる。

ターミナル
% npm install
% npm run start

上記コマンドの結果、http://localhost:8080/ が開くようにはなった。
しかし、画面上で400系のエラーが発生。。

原因

同時に立ち上げているelasticsearchにまだデータが入っていないことだと判明。

インデックスにドキュメントを入れる

インデックスに、検索対象にしたいドキュメントを入れていく。
(Elasticsearchでは、インデックスに入っているデータのことを"ドキュメント"と呼ぶ。)

①データ管理はrailsサーバー側で行っているため、rails consoleを立ち上げる。consoleの中で、下記コマンドを打ってindexを作成。

コンソール
[1] pry(main)> Product.__elasticsearch__.create_index! force: true
Elasticsearch::Transport::Transport::Errors::BadRequest: [400] {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Custom Analyzer [japanese] failed to find tokenizer under name [kuromoji_tokenizer]"}],"type":"illegal_argument_exception","reason":"Custom Analyzer [japanese] failed to find tokenizer under name [kuromoji_tokenizer]"},"status":400}
from /Users/●●/project/●●web_server/vendor/bundle/ruby/2.6.0/gems/elasticsearch-transport-7.5.0/lib/elasticsearch/transport/transport/base.rb:205:in `__raise_transport_error'

エラーが発生。indexが作成できない。。

原因

エラー文に書いてある通りで、kuromoji_tokenizer がないことが原因だった。
kuromoji_tokenizer は デフォルトでは elasticsearch にインストールされていないため、手動で入れる必要がある。

kuromoji_tokenizerのインストール

注意点

私の場合はelasticsearchのバージョンが7.10.2なので、それに合わせて下記の通りkuromoji_tokenizerも7.10.2をインストールしています。
そのため下記コマンドはご自身のバージョンに合わせて、適宜変更してください。

ターミナル
% /usr/local/Cellar/elasticsearch/7.10.2/bin/elasticsearch-plugin install https://artifacts.elastic.co/downloads/elasticsearch-plugins/analysis-kuromoji/analysis-kuromoji-7.10.2.zip
結果
warning: no-jdk distributions that do not bundle a JDK are deprecated and will be removed in a future release
-> Installing https://artifacts.elastic.co/downloads/elasticsearch-plugins/analysis-kuromoji/analysis-kuromoji-7.10.2.zip
-> Downloading https://artifacts.elastic.co/downloads/elasticsearch-plugins/analysis-kuromoji/analysis-kuromoji-7.10.2.zip
[=================================================] 100%
-> Installed analysis-kuromoji

インストール状況の確認

ターミナル
% elasticsearch-plugin list
結果
analysis-kuromoji

インストール成功。

ではもう一度rails consoleを立ち上げて、index作成コマンドを実行してみる。

コンソール
% rails c

[1] pry(main)> Product.__elasticsearch__.create_index! force: true
=> {"acknowledged"=>true, "shards_acknowledged"=>true, "index"=>"●●_products_development"}

成功!
indexが作成できたため、続いてindexの中にドキュメントを登録するコマンドを実行する。

コンソール
% rails c

[2] pry(main)> Product.__elasticsearch__.import
   (1.1ms)  SET NAMES utf8,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),  @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
[...省略...]
  SubCategory Load (0.3ms)  SELECT `sub_categories`.* FROM `sub_categories` WHERE `sub_categories`.`id` = 9 LIMIT 1
=> 0

無事にドキュメントが登録できた。

もし、kuromoji_tokenizerをインストールしたのにもかかわらず同じエラーが発生した場合は、Homebrewをアップグレードしてみてください。

ターミナル
% brew upgrade

私の場合は上記で上手くいきました。

それでも上手くいかない場合はこちらの記事を確認してみてください。

最後にelasticsearch、npm run startを再起動し、無事に400系エラーが解消して環境構築用の画面が正しく表示されました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?