Ruby on rails 轮胎模型回调速度慢
我有一个表格,显示了通过轮胎从elasticsearch获得的数据。该表以json格式异步检索数据。 因此,如果我试图更改一个条目,表仍然显示该条目的“旧”状态(保存到索引/表后直接重定向操作)。删除或添加条目时也是如此 但这只是偶尔发生的事。我发现,当我在检索数据之前向索引操作添加“sleep(0.3)”时,它可以工作 我的模型:Ruby on rails 轮胎模型回调速度慢,ruby-on-rails,
elasticsearch,tire,Ruby On Rails,
elasticsearch,Tire,我有一个表格,显示了通过轮胎从elasticsearch获得的数据。该表以json格式异步检索数据。 因此,如果我试图更改一个条目,表仍然显示该条目的“旧”状态(保存到索引/表后直接重定向操作)。删除或添加条目时也是如此 但这只是偶尔发生的事。我发现,当我在检索数据之前向索引操作添加“sleep(0.3)”时,它可以工作 我的模型: # encoding: utf-8 class Group include Mongoid::Document include Mongoid::Times
# encoding: utf-8
class Group
include Mongoid::Document
include Mongoid::Timestamps
include Tire::Model::Search
include Tire::Model::Callbacks
# Relations
has_and_belongs_to_many :users, index: true
has_many :group_rights, dependent: :destroy
accepts_nested_attributes_for :group_rights, allow_destroy: true, autosave: true
###
# Validates
validates :name, presence: true
validates :description, presence: true
###
# Mongoid Fields
field :name, type: String
field :description, type: String
###
# Elasticsearch
index_name "#{Tire::Model::Search.index_prefix}groups" # Indexname /initializers/tire.rb
mapping do
indexes :_id, :index => :not_analyzed
indexes :name
indexes :description
end
def to_indexed_json
to_json
end
###
# Methods
###
end
我想这和轮胎回收有关。但是为什么这么慢?是否有更好的方法来相应地更新索引
我将Elasticsearch 0.90与debian Squence上的openjdk-6一起使用
谢谢,
PatrickElasticsearch不会立即将更新写入其索引,它的刷新间隔默认为1秒,因此,如果写入和读取发生在1秒以内,则这可能是问题的原因。您可以降低刷新间隔(我不建议这样做),或者在使用
Tire.index("whatever").refresh
非常感谢。我不知道。我已将手动刷新添加到控制器中。不过要小心,
刷新
并非没有成本。最好是将其留给Elasticsearch——除非您确实需要立即传播更改,例如在集成测试中。如果您正在使用,您可能希望使用Model.searchkick\u index.refresh