Rails __elasticsearch __。create_index! “根映射定义具有不受支持的参数(mapper_parsing_exception)”

时间:2019-12-02 14:31:19

标签: ruby-on-rails elasticsearch elasticsearch-rails elasticsearch-model

在使用Business.__elasticsearch__.create_index!时,我在使用 elasticsearch-rails 时遇到了麻烦:

  

{“错误”:{“ root_cause”:[{“类型”:“ mapper_parsing_exception”,“原因”:“根映射定义具有不受支持的参数:[业务:{动态=真,属性= {id = {类型= integer}}}]“}]]”“类型”:“ mapper_parsing_exception”,“原因”:“无法解析映射[_doc]:根映射定义具有不受支持的参数:[业务:{dynamic = true,properties = {id = {type = integer}}}]“,” caused_by“:{” type“:” mapper_parsing_exception“,” reason“:”根映射定义具有不受支持的参数:[业务:{dynamic = true,properties = {id = { type = integer}}}]“}},”状态“:400}

该请求的后面是:

  

PUT http://localhost:9200/development_businesses [状态:400,请求:0.081s,查询:不适用]   {“ settings”:{“ index”:{“ number_of_shards”:1}},“ mappings”:{“ business”:{“ dynamic”:“ true”,“ properties”:{“ id”:{“ type” :“整数”}}}}}

我的模型代码:

`
after_save :reindex_model
Elasticsearch::Model.client = Elasticsearch::Client.new url: ENV['BONSAI_URL'], log: true
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
index_name [Rails.env, model_name.collection.gsub('///', '-')].join('_')
document_type self.name.downcase
`

我已经定义了映射:

`
settings index: { number_of_shards: 1 } do
    mappings dynamic: 'true' do
        indexes :id, type: 'integer'
    end
end
`

2 个答案:

答案 0 :(得分:2)

在创建映射时删除零件{"business":{"dynamic":"true"}}。尝试像下面这样对我来说很好-

PUT /development_businesses/
{
  "settings": {
    "index": {
      "number_of_shards": 1
    }
  },
  "mappings": {
      "properties": {
        "id": {
          "type": "integer"
        }
      }
  }
}

答案 1 :(得分:1)

从ES 7开始,已删除映射类型。您可以阅读更多详细信息here

如果您使用的是Ruby On Rails,则意味着您可能需要从模型或关注点中删除document_type

作为映射类型的替代方法,一种解决方案是对每种文档类型使用索引。

之前:

module Searchable
  extend ActiveSupport::Concern

  included do
    include Elasticsearch::Model
    include Elasticsearch::Model::Callbacks
    index_name [Rails.env, Rails.application.class.module_parent_name.underscore].join('_')
    document_type self.name.downcase
  end
end

之后:

module Searchable
  extend ActiveSupport::Concern

  included do
    include Elasticsearch::Model
    include Elasticsearch::Model::Callbacks
    index_name [Rails.env, Rails.application.class.module_parent_name.underscore, self.name.downcase].join('_')
  end
end