铁路轮胎弹性研究奇怪的错误
我在数据库中索引了Car
带有一辆汽车记录mercedes benz
的Car
模型。 如果我搜索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
其他随机搜索术语可以返回准确的结果。
所以它基本上是随机搜索术语产生的随机错误。 可能是什么原因造成的?
控制器:
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回调。
轮胎删除api用于从索引中删除单个文档。
Tire.index('my-index-name').remove('my-document-type', 'my-document-id')
参考: https : //github.com/karmi/tire/issues/43