使用Sunspot查询具有不同属性的多个模型

时间:2014-02-16 12:21:45

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

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

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

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部门的信息。

有没有办法将with范围应用于多模型搜索中的特定模型?还是有其他方法可以解决这个问题吗?

2 个答案:

答案 0 :(得分:9)

经过更多的谷歌搜索和一些反复试验后,我终于弄明白了。这是我最终得到的代码的大量注释版本:

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

答案 1 :(得分:0)

添加到Simon回答,

我又找到了一个场景。您希望在一个模型中应用条件并仅使用全文匹配其他模型。

我的第一次尝试就是这个

  def search_all(options)
    Sunspot.search(Post, Tag) do
      any_of do
        all_of do
          with :class, Post
          with(:status, :published)
        end
      end

      fulltext options[:search]
      group(:class) { limit 30 }
    end
  end

这只给了我Post但不是Tag,不知何故状态::发布应用于Tag并且Tag没有结果。我也尝试了其他一些变化。

最后我想出了一个解决方案。

Sunspot.search(Post, Tag) do
  any_of do
    all_of do
      with :class, Post
      with(:status, :published)
    end

    # This might look weiered to put all_of without any filter, However
    # without the next all_of block, status: :published is applied in tags and no results appear for tags.
    # Meaning any_of is ignored if there is one nested block and Post all_of is applied for entire filter.
    all_of do
      with :class, Tag
    end
  end

  fulltext options[:search]
  group(:class) { limit 30 }
end

此解决方案有效。可能是这可能有其他解决方案。我仍然对此很满意。