Newer
Older
# 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)
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
[:name, :description, :author, :object_type, :source, :tags]
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?
filter << { terms: { object_type: @search.object_types.map(&:to_i) } } unless @search.object_types.blank?
filter << { terms: { languages: @search.languages } } unless @search.languages.blank?

Israel Barreto Sant'Anna
committed
filter << { terms: { state: validate_object } }
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: :date } }
when 'publicationdesc' then { published_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 } }

Israel Barreto Sant'Anna
committed
return [::LearningObject.states[:published]] unless !@user.nil? && @user.is_admin?
::LearningObject.states.values