【发布时间】:2019-11-19 21:54:55
【问题描述】:
我正在尝试将 Elasticsearch 集成到我的 rails 应用程序中。当我尝试对我的模型进行导入时,问题就出现了。 视频.__elasticsearch__.import.
所以,在 Rails 控制台中,我运行了 Video.__elasticsearch__.import。我收到此错误: myflix_development 不存在可导入。使用创建索引!或 :force 选项来创建它。
然后我运行 Video.__elasticsearch__.create_index! 和 Video.__elasticsearch__.create_index!(force: true) 并且它们都返回了相同的非法参数异常错误:
PUT http://localhost:9200/myflix_development [status:400, request:0.027s, query:N/A]
2019-06-08 11:18:29 +0800: > {"settings":{},"mappings":{"_doc":{"properties":{}}}}
2019-06-08 11:18:29 +0800: < {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"The mapping definition cannot be nested under a type [_doc] unless include_type_name is set to true."}],"type":"illegal_argument_exception","reason":"The mapping definition cannot be nested under a type [_doc] unless include_type_name is set to true."},"status":400}
我知道我应该在尝试导入时创建一个 elasticsearch 索引,但我遇到了这个非法参数异常,我对此感到困惑
这是我所做的设置:
1) 在我的 gemfile 中包含 gem:
gem 'elasticsearch-model'
gem 'elasticsearch-rails'
2) 包含一个初始化程序:app/config/initializers/elasticsearch.rb
Elasticsearch::Model.client =
if Rails.env.staging? || Rails.env.production?
Elasticsearch::Client.new url: ENV['SEARCHBOX_URL']
elsif Rails.env.development?
Elasticsearch::Client.new log: true
else
Elasticsearch::Client.new
end
3) 在我的视频模型中包含弹性搜索
class Video < ActiveRecord::Base
include Elasticsearch::Model
index_name ["myflix", Rails.env].join("_")
...
end
4) Gemfile.lock
elasticsearch (7.1.0)
elasticsearch-api (= 7.1.0)
elasticsearch-transport (= 7.1.0)
elasticsearch-api (7.1.0)
multi_json
elasticsearch-model (6.0.0)
activesupport (> 3)
elasticsearch (> 1)
hashie
elasticsearch-rails (6.0.0)
elasticsearch-transport (7.1.0)
faraday
multi_json
任何帮助将不胜感激!
编辑 1) 尝试在我的模型中进行手动映射
class Video < ActiveRecord::Base
include Elasticsearch::Model
settings index: { number_of_shards: 1 } do
mappings dynamic: 'false' do
indexes :title, type: 'text'
indexes :description, type: 'text'
end
end
...
end
【问题讨论】:
标签: ruby-on-rails elasticsearch