我试图将Elasticsearch集成到我的rails应用程序中。当我试图对我的模型做一个导入时,问题就来了。Video.__elasticsearch__.import.
所以,在rails控制台中,我运行了Video.__elasticsearch__.import.我得到了这个错误:myflix_development不存在要导入到其中。使用create_index!或“强制”选项来创建它.
然后运行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)将宝石包括在我的宝石档案中:
gem 'elasticsearch-model'
gem 'elasticsearch-rails'2)包括一个初始化器:app/config/initializers/弹性搜索. an。
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
end3)在我的视频模型中包含了elasticsearch
class Video < ActiveRecord::Base
include Elasticsearch::Model
index_name ["myflix", Rails.env].join("_")
...
end4) 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发布于 2019-07-19 07:44:22
您可以通过设置elasticsearch-model来显式定义document_type传递给Elasticsearch的文档类型。例如:
class Video < ActiveRecord::Base
include Elasticsearch::Model
index_name ["myflix", Rails.env].join("_")
document_type "video"
...
end你用的名字是任意的。只要不是_doc,您就不应该在v7和up上遇到这个错误。
发布于 2019-06-08 16:12:08
从您的错误来看,我认为您使用的是elasticsearch 7。在索引查询中指定了_doc类型,但类型是自es7以来被弃用。
您可以尝试更新您的elasticsearch库以匹配es7,或者按照错误消息中的建议,您可以在映射中使用参数include_type_name。
https://stackoverflow.com/questions/56503930
复制相似问题