简体   繁体   English

为 Rails 模型创建 Elasticsearch 索引时如何修复“非法参数异常”?

[英]How to fix "Illegal Argument Exception" when creating an Elasticsearch index for a Rails model?

I'm trying to integrate Elasticsearch in my rails application.我正在尝试将 Elasticsearch 集成到我的 Rails 应用程序中。 The problem comes when I try to do an import on my model.当我尝试对我的模型进行导入时,问题就出现了。 Video.__elasticsearch__.import.视频.__elasticsearch__.import。

So, in rails console, I ran Video.__elasticsearch__.import .所以,在 rails 控制台中,我运行Video.__elasticsearch__.import I get this error: myflix_development does not exist to be imported into.我收到此错误: myflix_development 不存在可导入。 Use create_index!使用 create_index! or the :force option to create it.或 :force 选项来创建它。

I then ran Video.__elasticsearch__.create_index!然后我运行Video.__elasticsearch__.create_index! and Video.__elasticsearch__.create_index!(force: true) and they both returned the same error of illegal argument exception: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}

I understand that I am supposed to create an elasticsearch index when I try to do an import but I'm getting this illegal argument exception which I am puzzled about我知道我应该在尝试导入时创建一个 elasticsearch 索引,但我收到了这个非法参数异常,我对此感到困惑

This is what I've done to set things up:这是我所做的设置:

1) Included the gems in my gemfile: 1) 在我的 gemfile 中包含 gem:

gem 'elasticsearch-model'
gem 'elasticsearch-rails'

2) Included an initializer: app/config/initializers/elasticsearch.rb 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) Included elasticsearch in my Video model 3) 在我的视频模型中包含 elasticsearch

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

4) Gemfile.lock 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

Any help will be appreciated!任何帮助将不胜感激!

Edit 1) Attempted to do a manual mapping in my model编辑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

You can explicitly define the document type that elasticsearch-model passes to Elasticsearch by setting document_type .您可以通过设置document_type显式定义elasticsearch-model传递给 Elasticsearch 的文档类型。 For instance:例如:

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

What name you use is arbitrary.您使用的名称是任意的。 As long as it's not _doc , you shouldn't run into this error on v7 and up.只要它不是_doc ,你就不应该在 v7 及更高版本上遇到这个错误。

From the error you have, I think you are using elasticsearch 7. The type _doc is specified in your index query but types are deprecated since es7 .根据您的错误,我认为您使用的是_doc 7。类型_doc在您的索引查询中指定,但类型自 es7 以来弃用

You can try to update your elasticsearch library to match es7 or as suggested in the error message, you can use the parameter include_type_name in the mapping.您可以尝试更新您的 elasticsearch 库以匹配 es7 或按照错误消息中的建议,您可以在映射中使用参数include_type_name

The "illegal argument exception" is getting occurred because ElasticSearch engine is failing to analyze a reserved keyword.由于 ElasticSearch 引擎无法分析保留关键字,因此发生“非法参数异常”。 Properties is considered the root field inside the mapping section.Reform the request as below by excluding the "_doc" keyword :属性被认为是映射部分内的根字段。通过排除“_doc”关键字来如下修改请求:

PUT {INDEX_NAME} {"settings":{},"mappings":{"properties":{}}}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建索引的Elasticsearch问题(Rails) - Elasticsearch issue with creating index (Rails) 创建一个无数据库的Rails模型,该模型将用作Elasticsearch中索引数据的视图 - Creating a no-database rails model that would serve as a view to index data in elasticsearch 如何修复 Elasticsearch “无效索引和/或索引错误” - How to fix Elasticsearch “Invalid index and/or indexing error” 如何在创建Rails模型时设置默认值? - How to set default value when creating a Rails model? 使用 rails 创建 model 时如何为列指定排序规则 - How to specify a collation for a column when creating a model with rails Elasticsearch 6.2.4 [400] {“错误”:{“ root_cause”:[{“类型”:“ illegal_argument_exception”,“原因”:“文本为空(可能是HTTP / 0.9)”}]] - Elasticsearch 6.2.4 [400] {“error”:{“root_cause”:[{“type”:“illegal_argument_exception”,“reason”:“text is empty (possibly HTTP/0.9)”}] 带有字段的 Rails ElasticSearch 模型 - Rails ElasticSearch model with fields Rails 3 + Daemons gem:查询 model 时出现异常 - Rails 3 + Daemons gem: Exception when querying model Elasticsearch rails / Elasticsearch模型搜索模型关联 - Elasticsearch rails/ Elasticsearch model search model association 运行测试时 ElasticSearch Rails resource_already_exists_exception - ElasticSearch Rails resource_already_exists_exception when running tests
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM