Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
module SearchService
class LearningObject < Model
def search
::LearningObject.search(@search.query, where: where_hash, 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 where_hash
hash = {}
hash[:tags] = @search.tags unless @search.tags.blank?
hash[:object_type] = @search.types unless @search.types.blank?
hash[:source] = @search.sources unless @search.sources.blank?
hash[:state] = validate_object
hash.blank? ? nil : hash
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 } }
else { score: { order: :desc, unmapped_type: :integer } }
end
end
def validate_object
return 'published' unless !@user.nil? && @user.is_admin?
%w(published suspended draft)
end
end
end