ruby-on-rails - rails 轮胎 elasticsearch 奇怪的错误

标签 ruby-on-rails ruby-on-rails-3

我在数据库中索引了一个 Car 模型,其中有一辆汽车记录 mercedes benz。如果我搜索单词 benz,我会得到一个错误:

ActiveRecord::RecordNotFound in CarsController#index

Couldn't find all Cars with IDs (1, 3) (found 1 results, but was looking for 2)

如果我搜索 hello,我会得到:

Couldn't find Car with id=2

其他随机搜索词可以返回准确的结果。

所以这基本上是随机搜索词产生的随机错误。这可能是什么原因造成的?

Controller :

def index
 if params[:query].present?
    @cars = Car.search(params)
 else
    @cars = Car.paginate(:page => params[:page], :per_page => 10)
 end    
end

型号:

  def self.search(params)
   tire.search(load: true, page: params[:page], per_page: 10) do |s|
    s.query { string params[:query]} if params[:query].present?
   end
  end

最佳答案

发生这种情况是因为您正在使用 load => true 选项从数据库加载搜索结果。 DB中好像没有activerecord,但是elasticsearch索引里面有同样的文档。

恕我直言,Reindex 并不总是解决方案。

最好的解决办法是在db中删除文档的时候再删除。您可以为此使用 after_destroy 回调。

Tire remove api 用于从索引中删除单个文档。

Tire.index('my-index-name').remove('my-document-type', 'my-document-id')

引用:https://github.com/karmi/tire/issues/43

关于ruby-on-rails - rails 轮胎 elasticsearch 奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15026612/

相关文章:

ruby-on-rails - Sphinx 守护进程返回错误 : index product_core: INTERNAL ERROR: incoming-schema mismatch. 仅在登台服务器上

css - 更改导航丸的事件颜色

ruby-on-rails - 无法重新安装设备

javascript - 无法在 RedHat 中安装/识别 Javascript 运行时(适用于 Rails 3.1)

ruby-on-rails - 在模型中访问 c​​urrent_user;必须是一种更好的日志记录和身份验证方法

ruby-on-rails - 让 find_by in rails 返回多个结果

ruby-on-rails - Heroku:PG::错误:错误:关系的权限被拒绝

ruby-on-rails - 注册用户然后访问带有系统测试的帐户页面?

ruby-on-rails - 将嵌入代码保存到 Rails 模型后,嵌入视频在首次加载时无法在 Chrome 中呈现

ruby-on-rails-3 - 如何处理和错误以及将错误发送给response_with