Skip to content

Commit 1b10ddd

Browse files
committed
Update Suggestor to use repository objects to query
1 parent d080b00 commit 1b10ddd

File tree

1 file changed

+25
-61
lines changed

1 file changed

+25
-61
lines changed

app/models/suggester.rb

Lines changed: 25 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,78 +5,42 @@ def initialize(params={})
55
@term = params[:term]
66
end
77

8-
def response
9-
@response ||= begin
10-
Elasticsearch::Persistence.client.search \
11-
index: Album.index_name,
12-
body: {
13-
suggest: {
14-
suggest_albums: {
15-
prefix: @term,
16-
completion: { field: 'suggest.album_title', size: 25 }
17-
},
18-
suggest_members: {
19-
prefix: @term,
20-
completion: { field: 'suggest.artist_members', size: 25 }
21-
},
22-
suggest_artists: {
23-
prefix: @term,
24-
completion: { field: 'suggest.artist_name', size: 25 }
25-
}
26-
},
27-
_source: ['suggest.*']
28-
}
8+
def execute!(*repositories)
9+
@responses ||= []
10+
repositories.each do |repository|
11+
@responses << begin
12+
repository.client.search(index: repository.index_name,
13+
body: repository.suggest_body(@term))
14+
end
2915
end
3016
end
3117

3218
def as_json(options={})
33-
return [] unless response['suggest']
3419
json = []
35-
36-
if response['suggest']['suggest_artists']
37-
artists = response['suggest']['suggest_artists'].inject({}) do |matches, suggestion|
38-
suggestion['options'].each do |option|
39-
matches[option['_source']['suggest']['artist_name']['input'][1]] = option['text']
20+
@responses.each do |response|
21+
results = {}
22+
next unless response['suggest']
23+
['artist_names', 'artist_members', 'album_title', 'album_tracklist'].each do |result_field|
24+
25+
if suggestion = response['suggest'][result_field]
26+
results.merge!(suggestion.inject({}) do |matches, suggestion|
27+
suggestion['options'].each do |option|
28+
matches[option['text']] = option['_source']['artist_id'] || option['_id']
29+
end
30+
matches
31+
end)
4032
end
41-
matches
4233
end
43-
end
44-
45-
if response['suggest']['suggest_members']
46-
artists.merge!(response['suggest']['suggest_members'].inject({}) do |matches, suggestion|
47-
suggestion['options'].each do |option|
48-
matches[option['_source']['suggest']['artist_name']['input'][1]] = option['_source']['suggest']['artist_name']['input'][0]
49-
end
50-
matches
51-
end)
52-
end
5334

54-
if !artists.empty?
55-
json << { label: 'Bands',
56-
value: artists.map do |id, name|
57-
{ text: name,
58-
url: "artists/#{id}" }
59-
end }
35+
if !results.empty?
36+
json << { label: 'Matches',
37+
value: results.map do |matching_text, artist_id|
38+
{ text: matching_text,
39+
url: "artists/#{artist_id}" }
40+
end }
6041

61-
end
62-
63-
if response['suggest']['suggest_albums']
64-
albums = response['suggest']['suggest_albums'].inject({}) do |matches, suggestion|
65-
suggestion['options'].each do |option|
66-
matches[option['_source']['suggest']['artist_name']['input'][1]] = option['text']
67-
end
68-
matches
6942
end
7043
end
71-
72-
if !albums.empty?
73-
json << { label: 'Albums',
74-
value: albums.map do |id, name|
75-
{ text: name,
76-
url: "artists/#{id}" }
77-
end }
78-
79-
end
8044
json
8145
end
8246
end

0 commit comments

Comments
 (0)