【问题标题】:how to use postgresql condition with solr sunspot rails如何在 solr sunspot rails 中使用 postgresql 条件
【发布时间】:2014-01-02 16:34:40
【问题描述】:

我有一个带有状态的帖子列表,这意味着

status 1 - active
status 0 - removed
status 2 - pending

我的 solr 搜索索引所有内容,所以如果我搜索它会显示所有帖子,而与状态代码无关。我只需要status 1的帖子

我试过这样的

@search = Post.where(status: 1).search do
      fulltext params[:search] do
        minimum_match 1
      end
      paginate page: params[:page], per_page: 15
    end
    @posts = @search.results
    respond_to do |format|
      format.json { render nothing: true }
      format.html
    end

我该怎么做。谁能帮忙。

【问题讨论】:

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


    【解决方案1】:

    您可以在 ActiveRecord 的 where 子句中使用 @search.result_ids,而不是 @search.results

    @posts = Post.where(:id => @search.result_ids).where(status: 1)
    

    这实际上不是一个很好的选择,因为如果您的搜索包含不具有您想要的状态的结果,那么分页将无法与您的数据库很好地匹配。

    最好在模型的 searchable 块中索引 status 字段,并指示 Solr 在搜索中过滤该列。

    class Post
      searchable do
        # other fields...
        integer :status
      end
    end
    
    @search = Post.search do
      # other search options
      with :status, 1
    end
    
    @posts = @search.results
    

    【讨论】:

    • 我有一个管理面板将状态更改为 1。但更改后我需要重新索引以获得结果。有没有办法自动索引它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多