1

我正在尝试将 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
4

3 回答 3

4

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

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

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

于 2019-07-19T07:44:22.720 回答
0

由于 ElasticSearch 引擎未能分析保留关键字,因此发生“非法参数异常”。属性被认为是映射部分内的根字段。通过排除“_doc”关键字将请求修改如下:

PUT {INDEX_NAME} {"settings":{},"mappings":{"properties":{}}}}
于 2019-11-19T13:49:40.700 回答
0

根据您遇到的错误,我认为您使用的是 elasticsearch 7。类型_doc是在索引查询中指定的,但类型自 es7 以来已被弃用

您可以尝试更新您的 elasticsearch 库以匹配 es7 或按照错误消息中的建议,您可以include_type_name在映射中使用该参数。

于 2019-06-08T16:12:08.340 回答