Skip to content
Snippets Groups Projects
collection.rb 2.55 KiB
Newer Older
bfs15's avatar
bfs15 committed

# Copyright (C) 2015 Centro de Computacao Cientifica e Software Livre
# Departamento de Informatica - Universidade Federal do Parana
#
# This file is part of portalmec.
#
# portalmec is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# portalmec is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with portalmec.  If not, see <http://www.gnu.org/licenses/>.

module SearchService
  class Collection < Model
    def search
      ::Collection.search(body: mount_query)
    end

    def autocomplete
      params = {  where: { privacy: 'public' },
                  fields: ['name^10', 'description', 'owner'] }
      result = ::Collection.search(@search.query, autocomplete_params.merge(params))

      thumbnail = proc { |_obj| '/assets/icons/collection' }
      type = proc { |_obj| 'Collection' }
      autocomplete_render(result, type, thumbnail)
    end

    private

    def search_fields
      [:name, :description, :author]
    end

    def mount_filter
      filter = []
      filter << { term: { privacy: 'public' } }
      filter << { terms: { tags: @search.tags } } unless @search.tags.blank?
      filter << { terms: { subjects: @search.subjects } } unless @search.subjects.blank?
      filter << { terms: { educational_stages: @search.educational_stages } } unless @search.educational_stages.blank?
    def order_hash
      case @search.order
      when 'author' then { owner: { order: :asc, unmapped_type: :string } }
      when 'publicationasc' then { created_at: { order: :asc, unmapped_type: :date } }
      when 'publicationdesc' then { created_at: { order: :desc, unmapped_type: :date } }
      when 'title' then { name: { order: :asc, unmapped_type: :string } }
      when 'likes' then { likes: { order: :desc } }
      when 'downloads' then { downloads: { order: :desc } }
      when 'review_average' then { review_average: { order: :desc } }
      else
        { score: {order: :desc} } if @search.query == '*'
        "_score"

    def show_empty
      return [true,false] if !@user.nil? && @user.is_admin?
      [false]
    end