# 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 LearningObject < Model def search fields = [:license, :tags, :subjects, :educational_stages, :taggings, :publisher, :language, :attachments, :reviews] ::LearningObject.search(body: mount_query, includes: fields, order: order_hash, page: @search.page, per_page: @search.results_per_page) end def autocomplete params = { where: { state: validate_object }, fields: ['name^10', 'description', 'author'] } result = ::LearningObject.search(@search.query, autocomplete_params.merge(params)) thumbnail = proc { |obj| obj.default_thumbnail } type = proc { |obj| obj.object_type.try(:name) } autocomplete_render(result, type, thumbnail) end private def search_fields [:name, :description, :author, :object_type, :source] end def mount_filter filter = [] filter << { in: { tags: @search.tags } } unless @search.tags.blank? filter << { in: { subjects: @search.subjects } } unless @search.subjects.blank? filter << { in: { educational_stages: @search.educational_stages } } unless @search.educational_stages.blank? filter << { in: { object_type: @search.object_types.map(&:to_i) } } unless @search.object_types.blank? filter << { terms: { state: validate_object } } filter end def order_hash case @search.order when 'author' then { author: { order: :asc, unmapped_type: :string } } when 'publicationasc' then { published_at: { order: :asc, unmapped_type: :timestamp } } when 'publicationdesc' then { published_at: { order: :desc, unmapped_type: :timestamp } } 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" end end def validate_object return [::LearningObject.states[:published]] unless !@user.nil? && @user.is_admin? ::LearningObject.states.values end end end