【备忘录】【错误解决】当使用npm run start启动node服务器时,出现了400系列错误

使用以下命令启动Node的SSR服务器。

% npm install
% npm run start

执行以上的命令后,打开了http://localhost:8080/网页。然而,屏幕上出现了400系的错误。

the reason

刚刚发现,我们启动的elasticsearch还没有数据。

将文档放入索引中。

把想要搜索的文档放入索引中。
(在Elasticsearch中,索引中的数据被称为“文档”。)

由于数据管理是在Rails服务器端进行的,因此需要启动Rails console。在控制台中,输入以下命令来创建索引。

[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'

出现错误,无法创建索引。

造成的因素

正如错误消息所写的那样,缺少 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控制台,并尝试执行创建索引的命令。

% 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系列错误,并正确显示了环境设置界面。

bannerAds