forked from elastic/elasticsearch-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuggester.rb
45 lines (42 loc) · 1.01 KB
/
suggester.rb
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
44
45
class Suggester
attr_reader :response
def initialize(params={})
@term = params[:term]
end
def response
@response ||= begin
Elasticsearch::Persistence.client.suggest \
index: Artist.index_name,
body: {
artists: {
text: @term,
completion: { field: 'suggest_name', size: 25 }
},
members: {
text: @term,
completion: { field: 'suggest_member', size: 25 }
},
albums: {
text: @term,
completion: { field: 'suggest_title', size: 25 }
},
tracks: {
text: @term,
completion: { field: 'suggest_track', size: 25 }
}
}
end
end
def as_json(options={})
response
.except('_shards')
.reduce([]) do |sum,d|
# category = { d.first => d.second.first['options'] }
item = { :label => d.first.titleize, :value => d.second.first['options'] }
sum << item
end
.reject do |d|
d[:value].empty?
end
end
end