2013-05-20 53 views
0

我有一个表显示来自elasticsearch通过轮胎的数据。该表以json格式检索异步数据。 因此,如果我尝试更改条目,表格仍然显示条目的“旧”状态(保存到索引/表格后直接重定向动作)。同样,当我删除或添加一个条目。轮胎ActiveModel回调慢

但是这种情况有时会发生。我发现,当我在检索数据之前向索引动作添加“睡眠(0.3)”时,它会起作用。

我的模型:

# 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挤压openjdk-6。

感谢, 帕特里克

回答

0

Elasticsearch不更新写入它的直接指标,它有一个刷新间隔,默认为1秒,所以如果写和读都发生在不到1秒,随后可能成为你的问题的原因。你可以降低刷新间隔(我不建议这一点),或只是调用刷新你写后Elasticsearch控制器与

Tire.index("whatever").refresh 
+0

谢谢!我不知道。我已将手动刷新添加到控制器。 – patrickkeller

+0

但要小心,刷新不是没有代价的。最好的办法是把它留给Elasticsearch - 除非你真的需要这些变化立即传播,比如在一个集成测试中。 – karmi

+0

如果您使用[searchkick](https://github.com/ankane/searchkick),您可能需要使用'Model.searchkick_index.refresh' – zevstatiev