3

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

{"error":{"root_cause":[{"type":"mapper_parsing_exception","re​​ason":"根映射定义有不受支持的参数:[business : {dynamic=true, properties={id={type=integer} }}]"}],"type":"mapper_parsing_exception","re​​ason":"无法解析映射 [_doc]:根映射定义有不受支持的参数:[business : {dynamic=true, properties={id={type =integer}}}]","caused_by":{"type":"mapper_parsing_exception","re​​ason":"根映射定义有不受支持的参数:[business : {dynamic=true, properties={id={type=integer }}}]"}},"状态":400}

该请求的背后是:

PUT http://localhost:9200/development_businesses [status:400, request:0.081s, query:N/A] {"settings":{"index":{"number_of_shards":1}},"mappings":{ “业务”:{“动态”:“真”,“属性”:{“id”:{“类型”:“整数”}}}}}

我的型号代码:

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

2 回答 2

4

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

PUT /development_businesses/
{
  "settings": {
    "index": {
      "number_of_shards": 1
    }
  },
  "mappings": {
      "properties": {
        "id": {
          "type": "integer"
        }
      }
  }
}
于 2019-12-02T14:50:36.617 回答
2

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

如果您使用的是 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
于 2020-05-03T14:06:32.130 回答