使用Sunspot查询具有不同属性的多个模型 [英] Querying multiple models with different attributes using Sunspot

查看:80
本文介绍了使用Sunspot查询具有不同属性的多个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Sunspot在Rails项目中索引和搜索多个模型,我需要基于模型与Department的HABTM关联来限制结果.这是因为用户可能无权查看所有部门的记录,因此不应返回这些部门的结果.

I'm using Sunspot to index and search several models in a Rails project and I need to limit results based on the models' HABTM associations with a Department model. This is because users may not have permission to see records in all departments so results from those departments shouldn't be returned.

以下是两个模型的重要组成部分:

Here are the important parts of two of the models:

class Message < ActiveRecord::Base
  has_many :comments, dependent: :destroy
  has_and_belongs_to_many :departments

  searchable do
    text :title, :body
    text :comments do
      comments.map(&:body)
    end
    date :created_at
    integer :department_ids, using: :department_ids, references: Department, multiple: true
  end
end

class Document < ActiveRecord::Base
  has_and_belongs_to_many :departments

  searchable do
    text :name
    date :created_at
    integer :department_ids, using: :department_ids, references: Department, multiple: true
  end
end

这是搜索控制器代码:

class SearchController < ApplicationController
  def index
    # These arrays are created here for the sake of this example
    document_permitted_departments = [1, 2, 3]
    message_permitted_departments = [3, 4]

    search = Sunspot.search Document, Message do
      # This obviously doesn't work
      with(:department_ids, document_permitted_departments)
      with(:department_ids, message_permitted_departments)
      fulltext params[:q]
      paginate page: params[:page], per_page: SEARCH_RESULTS_PER_PAGE
      order_by :created_at, :desc
    end
    @results = search.results
    @number_of_results = search.total

    respond_to do |format|
      format.js
      format.html
    end
  end
end

问题在于用户可能能够阅读部门A和部门B中的文档,但他们只能看到部门B中的消息.

The problem is that a user may be able to read documents in Department A and Department B but they should only see messages in Department B.

在多模型搜索中,是否可以将with范围应用于特定模型?还是有其他方法让我错过了?

Is there a way to apply the with scope to a specific model in a multi-model search? Or is there another way of doing this that I'm missing?

推荐答案

经过更多的谷歌搜索和反复试验后,我终于弄清楚了.这是我最后得到的代码的注释版本:

After a lot more Googling and some trial and error I finally figured it out. Here's a heavily annotated version of the code I ended up with:

class SearchController < ApplicationController
  before_filter :authenticate_user!

  def index
    # These arrays are created here for the sake of this example
    # Push 0 on to the end because empty arrays break the `with :department_ids` scopes below
    document_permitted_departments = [1, 2, 3].push(0)
    message_permitted_departments = [3, 4].push(0)

    search = Sunspot.search Document, Message do
      any_of do # Return anything that matches any of the scopes in this block
        all_of do # Return only those results that match these scopes
          with :class, Document # This limits scopes in this block to Document results
          with :department_ids, document_permitted_departments
        end

        all_of do # Return only those results that match these scopes
          with :class, Message # This limits scopes in this block to Message results
          with :department_ids, message_permitted_departments
        end
      end

      fulltext params[:q]
      paginate page: params[:page], per_page: SEARCH_RESULTS_PER_PAGE
      order_by :created_at, :desc
    end
    @results = search.results
    @number_of_results = search.total

    respond_to do |format|
      format.js # index.js.erb
      format.html # index.html.erb
    end
  end
end

这篇关于使用Sunspot查询具有不同属性的多个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆