Запрос нескольких моделей с разными атрибутами с помощью Sunspot

Я использую Sunspot для индексации и поиска нескольких моделей в проекте Rails, и мне нужно ограничить результаты на основе ассоциаций моделей HABTM с моделью Department. Это связано с тем, что у пользователей может не быть разрешения на просмотр записей во всех отделах, поэтому результаты из этих отделов не должны возвращаться.

Вот важные части двух моделей:

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 к конкретной модели при поиске по нескольким моделям? Или есть другой способ сделать это, который мне не хватает?


person Simon    schedule 16.02.2014    source источник


Ответы (2)


После долгих поисков в Google и некоторых проб и ошибок я наконец понял это. Вот сильно аннотированная версия кода, который у меня получился:

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
person Simon    schedule 18.02.2014
comment
Это хороший пример. Спасибо. - person dps; 31.03.2015
comment
Мне было бы интересно, как сделать подсветку в разных именах полей и разных моделях, используя этот синтаксис :-) - person dps; 01.04.2015

Добавляя к Саймону ответ,

Я нашел еще один сценарий. Если вы хотите применить условие в одной модели и сопоставить другую только с полным текстом.

Моя первая попытка была такой

  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

Это давало мне только сообщение, но не тег, каким-то образом статус:: опубликовано применяется к тегу, и для тега нет результатов. Я пробовал и другие варианты.

Наконец я нашел одно решение.

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

Это решение сработало. Может быть, у этого может быть другое решение. И все же я доволен этим.

person Abibullah Rahamathulah    schedule 12.07.2016