我将elasticsearch gem添加到Heroku上托管的现有rails应用中。
在生产中运行MyModel.import
会引发错误
MultiJson::ParseError: unexpected character at line 1, column 1 [parse.c:690]
由于我的开发和测试环境运行良好,我认为这是由MyModel中的数据引起的,可能是编码问题。但由于此表非常大,我无法追踪问题。
MyModel.import
调用的JSON数据表示?根据以下建议,我尝试运行一些命令来追踪错误。
MyModel.all.each(&:as_json)
MyModel.all.each(&:as_indexed_json)
MyModel.all.each { |r| r.__elasticsearch__.as_indexed_json }
MyModel.all.each { |r| MultiJson.load r.to_json }
都正常运行而没有错误。但是它们似乎不是返回json,而是返回记录数组。这通常吗?
[#<MyModel id: 1, ......
在有限数量的记录上运行import
也会导致奇怪的结果。
# a single record returns no error
MyModel.import query: -> { where(id: 1) }
=> 0
# multiple records return the error
MyModel.import query: -> { where(id: 1..2) }
MultiJson::ParseError: unexpected character at line 1, column 1 [parse.c:690]
完整堆栈跟踪
MultiJson::ParseError: unexpected character at line 1, column 1 [parse.c:690]
from /app/vendor/bundle/ruby/2.4.0/gems/multi_json-1.11.3/lib/multi_json/adapters/oj.rb:15:in `load'
from /app/vendor/bundle/ruby/2.4.0/gems/multi_json-1.11.3/lib/multi_json/adapters/oj.rb:15:in `load'
from /app/vendor/bundle/ruby/2.4.0/gems/multi_json-1.11.3/lib/multi_json/adapter.rb:20:in `load'
from /app/vendor/bundle/ruby/2.4.0/gems/multi_json-1.11.3/lib/multi_json.rb:119:in `load'
from /app/vendor/bundle/ruby/2.4.0/gems/elasticsearch-transport-5.0.4/lib/elasticsearch/transport/transport/serializer/multi_json.rb:24:in `load'
from /app/vendor/bundle/ruby/2.4.0/gems/elasticsearch-transport-5.0.4/lib/elasticsearch/transport/transport/base.rb:322:in `perform_request'
from /app/vendor/bundle/ruby/2.4.0/gems/elasticsearch-transport-5.0.4/lib/elasticsearch/transport/transport/http/faraday.rb:20:in `perform_request'
from /app/vendor/bundle/ruby/2.4.0/gems/elasticsearch-transport-5.0.4/lib/elasticsearch/transport/client.rb:131:in `perform_request'
from /app/vendor/bundle/ruby/2.4.0/gems/elasticsearch-api-5.0.4/lib/elasticsearch/api/actions/bulk.rb:95:in `bulk'
from /app/vendor/bundle/ruby/2.4.0/gems/elasticsearch-model-5.0.1/lib/elasticsearch/model/importing.rb:123:in `block in import'
from /app/vendor/bundle/ruby/2.4.0/gems/elasticsearch-model-5.0.1/lib/elasticsearch/model/adapters/active_record.rb:106:in `block in __find_in_batches'
from /app/vendor/bundle/ruby/2.4.0/gems/activerecord-5.0.6/lib/active_record/relation/batches.rb:121:in `block in find_in_batches'
from /app/vendor/bundle/ruby/2.4.0/gems/activerecord-5.0.6/lib/active_record/relation/batches.rb:214:in `block in in_batches'
from /app/vendor/bundle/ruby/2.4.0/gems/activerecord-5.0.6/lib/active_record/relation/batches.rb:198:in `loop'
from /app/vendor/bundle/ruby/2.4.0/gems/activerecord-5.0.6/lib/active_record/relation/batches.rb:198:in `in_batches'
from /app/vendor/bundle/ruby/2.4.0/gems/activerecord-5.0.6/lib/active_record/relation/batches.rb:120:in `find_in_batches'
from /app/vendor/bundle/ruby/2.4.0/gems/elasticsearch-model-5.0.1/lib/elasticsearch/model/adapters/active_record.rb:105:in `__find_in_batches'
from /app/vendor/bundle/ruby/2.4.0/gems/elasticsearch-model-5.0.1/lib/elasticsearch/model/importing.rb:122:in `import'
from /app/vendor/bundle/ruby/2.4.0/gems/elasticsearch-model-5.0.1/lib/elasticsearch/model.rb:118:in `import'
from (irb):14
from /app/vendor/bundle/ruby/2.4.0/gems/railties-5.0.6/lib/rails/commands/console.rb:65:in `start'
from /app/vendor/bundle/ruby/2.4.0/gems/railties-5.0.6/lib/rails/commands/console_helper.rb:9:in `start'
from /app/vendor/bundle/ruby/2.4.0/gems/railties-5.0.6/lib/rails/commands/commands_tasks.rb:78:in `console'
from /app/vendor/bundle/ruby/2.4.0/gems/railties-5.0.6/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /app/vendor/bundle/ruby/2.4.0/gems/railties-5.0.6/lib/rails/commands.rb:18:in `<top (required)>'
from bin/rails:9:in `require'
from bin/rails:9:in `<main>'
这被证明是一个令人沮丧的调试问题。我很感激任何进一步的建议或想法。