-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathelasticsearch_recipe_loader.rb
81 lines (67 loc) · 2.18 KB
/
elasticsearch_recipe_loader.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
require 'HTTParty'
require 'Pry'
require 'csv'
require 'json'
class ElasticsearchRecipeLoader
RECIPES_PATH = './recipes2.csv'
ELASTICSEARCH_IP = 'http://127.0.0.1:9200'
ELASTICSEARCH_INDEX = '/recipes-en/recipe/'
def self.load_data
put_index
recipes = CSV.read(RECIPES_PATH,
headers: :first_row,
encoding: 'iso-8859-1:utf-8',
:row_sep => :auto,
:col_sep => ';'
)
parsed_recipes = parse_csv(recipes)
parsed_recipes.each do |recipe|
begin
response = post_recipe(recipe)
rescue StandardError=>e
puts "\tError: #{e}"
else
puts "\t Success: #{response}"
end
end
end
private
def self.parse_csv(recipes)
parsed_recipes = []
if recipes
recipes.each do |row|
recipe = {}
recipe[:name] = row['recipe name']
recipe[:ingredients] = row[' ingredients'].strip.gsub(/\"/, '').gsub(/, /,',').split(',')
recipe[:preparation] = row[' preparation'].strip.gsub(/\"/, '').gsub(/\u008CÁ/,"\u2103")
parsed_recipes << recipe
end
end
parsed_recipes
end
def self.post_recipe(recipe_hash)
puts "Posting recipe to #{ELASTICSEARCH_IP + ELASTICSEARCH_INDEX}"
HTTParty.post(ELASTICSEARCH_IP + ELASTICSEARCH_INDEX,
:body => recipe_hash.to_json,
:headers => { 'Content-Type' => 'application/json' }
)
end
def self.put_index
puts "PUT index #{ELASTICSEARCH_IP}"
HTTParty.put(ELASTICSEARCH_IP + '/recipes-en',
:body => { "mappings" => {
"ingredients" => {
"properties" => {
"ingredients" => {
"type" => "string",
"analyzer" => "english"
}
}
}
}
}.to_json,
:headers => { 'Content-Type' => 'application/json' }
)
end
end
ElasticsearchRecipeLoader.load_data