轨道轮胎弹性搜索奇怪的错误 [英] rails tire elasticsearch weird error
问题描述
我在数据库中用一个汽车记录 mercedes benz
索引了一个 Car
模型.如果我搜索 benz
这个词,我会得到一个错误:
I have indexed a Car
model with one car record mercedes benz
in the database. If I search for the word benz
I get an error:
ActiveRecord::RecordNotFound in CarsController#index
Couldn't find all Cars with IDs (1, 3) (found 1 results, but was looking for 2)
如果我搜索 hello
我得到:
If I search for hello
I get:
Couldn't find Car with id=2
其他随机搜索词可以返回准确的结果.
Other random search terms work returning accurate results.
所以它基本上是由随机搜索词产生的随机错误.这可能是什么原因?
So it's basically random errors generated by random search terms. What could be the cause of this?
控制器:
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 选项从数据库加载搜索结果.数据库中似乎缺少 activerecord,但 elasticsearch 索引包含相同的文档.
This happens because, you are using the load => true option to load the search results from database. The activerecord seems to be missing in DB, but the elasticsearch index contains the same document.
恕我直言,重新索引并不总是解决方案.
Reindex is not always the solution IMHO.
最好的解决办法是在db中删除文档时删除.您可以为此使用 after_destroy 回调.
The best solution is to delete the document when it is deleted in db. You can use the after_destroy callback for this.
Tire remove api 用于从索引中删除单个文档.
Tire remove api is used to remove a single document from index.
Tire.index('my-index-name').remove('my-document-type', 'my-document-id')
参考:https://github.com/karmi/tire/issues/43
这篇关于轨道轮胎弹性搜索奇怪的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!