forked from elastic/elasticsearch-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_manager.rb
60 lines (52 loc) · 2.01 KB
/
index_manager.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
require 'open-uri'
class IndexManager
def self.create_index(options={})
client = Artist.gateway.client
index_name = Artist.index_name
client.indices.delete index: index_name rescue nil if options[:force]
settings = Artist.settings.to_hash.merge(Album.settings.to_hash)
mappings = Artist.mappings.to_hash.merge(Album.mappings.to_hash)
client.indices.create index: index_name,
body: {
settings: settings.to_hash,
mappings: mappings.to_hash }
end
def self.import_from_yaml(source, options={})
create_index force: true if options[:force]
input = open(source)
artists = YAML.load_documents input
artists.each do |artist|
Artist.create artist.update(
'album_count' => artist['releases'].size,
'members_combined' => artist['members'].join(', '),
'suggest_name' => {
'input' => artist['namevariations'].unshift(artist['name']),
'output' => artist['name'],
'payload' => { 'url' => "/artists/#{artist['id']}" }
},
'suggest_member' => {
'input' => artist['members'],
'output' => artist['name'],
'payload' => { 'url' => "/artists/#{artist['id']}" }
}
)
artist['releases'].each do |album|
album.update(
'suggest_title' => {
'input' => album['title'],
'output' => album['title'],
'payload' => { 'url' => "/artists/#{artist['id']}#album_#{album['id']}" }
},
'suggest_track' => {
'input' => album['tracklist'].map { |d| d['title'] },
'output' => album['title'],
'payload' => { 'url' => "/artists/#{artist['id']}#album_#{album['id']}" }
}
)
album['notes'] = album['notes'].to_s.gsub(/<.+?>/, '').gsub(/ {2,}/, '')
album['released'] = nil if album['released'] < 1
Album.create album, id: album['id'], parent: artist['id']
end
end
end
end