gpt4 book ai didi

ruby-on-rails - 为Rails模型创建Elasticsearch索引时如何修复 “Illegal Argument Exception”?

转载 作者:行者123 更新时间:2023-12-02 22:47:52 25 4
gpt4 key购买 nike

我正在尝试将Elasticsearch集成到我的Rails应用程序中。当我尝试对模型进行导入时,问题就来了。 视频。__elasticsearch __。import。

因此,在Rails控制台中,我运行了 Video .__ elasticsearch __。import 。我收到此错误:
myflix_development不存在要导入到其中。使用create_index!或: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)在我的视频模型中包含了elasticsearch
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

最佳答案

您可以通过设置elasticsearch-model来明确定义document_type传递给Elasticsearch的文档类型。例如:

class Video < ActiveRecord::Base
include Elasticsearch::Model
index_name ["myflix", Rails.env].join("_")
document_type "video"
...
end

您使用的名称是任意的。只要不是 _doc,就不应在v7及更高版本上遇到此错误。

关于ruby-on-rails - 为Rails模型创建Elasticsearch索引时如何修复 “Illegal Argument Exception”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56503930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com