Rails__elasticsearch__.create_index! "Root mapping definition has unsupported parameters(mapper_parsing_exception)"

Rails __elasticsearch__.create_index! "Root mapping definition has unsupported parameters(mapper_parsing_exception)"

我在使用 elasticsearch-rails 时遇到问题,当我使用 Business.__elasticsearch__.create_index! 时出现错误:

{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"Root mapping definition has unsupported parameters: [business : {dynamic=true, properties={id={type=integer}}}]"}],"type":"mapper_parsing_exception","reason":"Failed to parse mapping [_doc]: Root mapping definition has unsupported parameters: [business : {dynamic=true, properties={id={type=integer}}}]","caused_by":{"type":"mapper_parsing_exception","reason":"Root mapping definition has unsupported parameters: [business : {dynamic=true, properties={id={type=integer}}}]"}},"status":400}

该请求的背后是:

PUT http://localhost:9200/development_businesses [status:400, request:0.081s, query:N/A] {"settings":{"index":{"number_of_shards":1}},"mappings":{"business":{"dynamic":"true","properties":{"id":{"type":"integer"}}}}}

我的型号代码:

`
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
`

在创建映射时删除部分 {"business":{"dynamic":"true"}}。像下面这样对我来说效果很好-

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

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

如果您在 Rails 上使用 Ruby,这意味着您可能需要从您的模型或问题中删除 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