我正在尝试将 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}
我知道当我尝试导入时我应该创建一个弹性搜索索引,但我得到了这个非法参数异常,我对此感到困惑
这就是我所做的设置:
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