elastisearch-model更新至7.0破坏测试

时间:2019-12-18 16:21:19

标签: ruby-on-rails elasticsearch

在将elasticsearch-model和elastisearch-rails gems更新为7.0后,导致现有测试失败。

示例测试

    context 'conditional indexes' do
      it 'a new record should be indexed' do
        new_dept = FactoryBot.create :department
        Department.__elasticsearch__.refresh_index!
        sleep 2 # let the callbacks work
        # clean query usually done in controller
        query = new_dept.name.gsub(%r{\{|\}|\[|\]|\\|\/|\^|\~|\:|\!|\"|\'}, '')
        expect(Department.search(query).records.count).to eq 1
      end

上面的测试正常工作,直到更新,现在我们在Elasticsearch测试中看到这种类型的错误。

     Elasticsearch::Transport::Transport::Errors::InternalServerError:
       [500] {"error":{"root_cause":[{"type":"class_cast_exception","reason":"class java.lang.String cannot be cast to class java.util.Map (java.lang.String and java.util.Map are in module java.base of loader 'bootstrap')"}],"type":"class_cast_exception","reason":"class java.lang.String cannot be cast to class java.util.Map (java.lang.String and java.util.Map are in module java.base of loader 'bootstrap')"},"status":500}

部门模型

class Department < ApplicationRecord
  # validation
  validates :name,
            presence: true,
            length: { within: 5..50 },
            uniqueness: { case_sensitive: false }

  has_many :faculties, -> { order(:last_name, :first_name) }

  # active status
  enum status: %i[enabled disabled]

  # search
  include Searchable

  # scopes
  scope :visible, -> { where(status: 'enabled') }
  scope :order_name, -> { order(:name) }

  # Elastic Search Index settings.
  # These are set in the model to index only specific information.   
  settings index: { number_of_shards: 1 } do
    mappings dynamic: 'false' do
      indexes :name, type: :text
    end
  end

  # Elastic Search Settings
  # @description
  # indexed json, this will help with search rankings.
  # rake environment elasticsearch:import:model CLASS='Department' SCOPE="visible" FORCE=y
  def as_indexed_json(_options)
    as_json(
      only: %i[id name],
      include: {
        faculties: { only: :name }
      }
    )
  end
end

在Rails控制台中测试

systems = Department.create(name: "Test Department");
systems.__elasticsearch__.create_index!

返回以下内容

NoMethodError: undefined method `create_index!' for #<Elasticsearch::Model::Proxy::InstanceMethodsProxy:0x00007f12301ddf28>
from /usr/local/bundle/gems/elasticsearch-model-7.0.0/lib/elasticsearch/model/proxy.rb:121:in `method_missing'

任何帮助将不胜感激。 如果有人需要查看完整的项目,可以在github上的https://github.com/wvulibraries/TLC-Directory

上找到它。

1 个答案:

答案 0 :(得分:0)

将我的Elasticsearch Docker容器升级到7.5.0可以纠正我所看到的问题。