【问题标题】:Sunspot Rails - Prevent Indexing on SaveSunspot Rails - 保存时防止索引
【发布时间】:2013-09-11 04:02:31
【问题描述】:

默认情况下,sunspot solr gem 向 solr 服务器发出索引命令,作为保存回调的一部分。这种行为在我的大多数应用程序中是可以接受的,但是在其中的某些部分(尤其是用于批量处理的 rake 任务中)我想在不与 solr 服务器进行任何交互的情况下保存我的模型实例。我如何做到这一点?

【问题讨论】:

    标签: ruby-on-rails solr sunspot sunspot-rails sunspot-solr


    【解决方案1】:

    要禁用它,请将auto_commit_after_request: false 添加到您的sunspot.yml

    【讨论】:

    • 不,这是不对的。我已经有了 auto_commit_after_request: false,但这控制了 solr 服务器是否在请求之后提交。即使设置为 false,应用程序仍会向 solr 服务器发出请求以索引模型实例。它只是没有立即提交。
    【解决方案2】:

    根据docs,我认为您应该将auto_index: false 添加到可搜索块中,即:

    class Foo < ActiveRecord::Base
      searchable auto_index: false do
        # your search fields here
      end
    end
    

    那么您可以使用以下方法手动重新索引记录:

    # On a class itself
    Person.reindex
    Sunspot.commit # or commit(true) for a soft commit (Solr4)
    
    # On mixed objects
    Sunspot.index [post1, item2]
    Sunspot.index person3
    Sunspot.commit # or commit(true) for a soft commit (Solr4)
    
    # With autocommit
    Sunspot.index! [post1, item2, person3]
    

    【讨论】:

      猜你喜欢
      • 2013-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多