【问题标题】:Callback after update index on elastic search using chewy使用耐嚼的弹性搜索更新索引后的回调
【发布时间】:2020-11-10 03:11:42
【问题描述】:

假设我有以下代码:

class User < ActiveRecord::Base
  update_index('users#user') { self } 
                                      

  after_commit :send_socket_event, on: %i[create update]
  

  def send_socket_event(self):
     SockeClient.send(self)
  end
end

socket 事件被发送到客户端,然后客户端创建一个 http 请求以从对弹性搜索进行查询的端点获取新的用户信息。我的问题是响应是在 ES 上的索引更新之前完成的,因此端点响应中的信息尚未更新。

我想知道Chewy::Index 类是否有类似回调after index 之类的东西。

【问题讨论】:

    标签: ruby-on-rails elasticsearch chewy-gem


    【解决方案1】:

    我没用过chewy,但在很大程度上用过searchkick

    所以通过使用 searchkick,我会使用 reindex 和 remove_from_index 回调来触发我想要的任何东西。

    例如 - 在下面的代码 sn-p 中,我创建了一个模块,然后将它包含在我想要使用这些方法的任何模型中,我在重新索引完成后检查某些内容或某些内容从索引中删除

        ##check a model attribute or call function, here I am just checking some attribute.
        after_save :reindex , if: Proc.new { |vendor| vendor.accepted? }
        after_save :remove_from_index , if: Proc.new { |vendor| !vendor.accepted? }
    
        ####where remove_from_index was just a simple method in a module, so I just include this module in any model
        def remove_from_index
          self.class.name.classify.constantize.searchkick_index.remove(self)
        end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-12
      • 1970-01-01
      • 2019-02-15
      • 1970-01-01
      • 2012-05-07
      • 2016-04-20
      相关资源
      最近更新 更多