当我使用rake db:seed命令时,我有一个带有大量数据的csv文件 控制台显示..
deleting old index
creating new index
starting import
20
refresh index
看起来每件事都很好但是表格甚至没有创建
我的seed.rb是
require 'csv'
puts "deleting old index"
Article.index.delete
puts "creating new index"
Article.create_elasticsearch_index
mapping = {
"id" => "id",
"artikel_nr" => "artikelnummer",
"ean_nr" => "eannummer",
"bezeichnung_1" => "bezeichnung",
"bezeichnung_2" => "bezeichnung_zusatz",
"match_code" => "matchcode",
"verkauf_me" => "mengeneinheit",
"gewicht" => "gewicht",
"lieferant_nr" => "hersteller_nummer",
"bestell_nr" => "hersteller_artikelnummer",
"produkt_gruppe" => "gruppe",
"artikel_gruppe" => "gruppe_nummer",
"produkt_hauptgruppe" => "hauptgruppe",
"rt_1" => "langtext",
"masse" => "dimension",
"einzelpreis_vk" => "listenpreis_netto"
}
count = 0
buffer = []
puts "starting import"
hersteller = Hash[CSV.read("db/red1.csv", col_sep: ',', row_sep: :auto, headers: true).map {|row| [row["lieferant_nr"], row["beschreibung"]]}]
CSV.foreach("db/red1.csv", col_sep: ',', row_sep: :auto, headers: true) do |row|
# map keys
hash = Hash[row.map {|k, v| mapping[k] ? [mapping[k], v && v.strip.gsub("\u00A0", "")] : nil}.compact]
# ignore NULL values
hash.reject! {|k, v| v == "NULL"}
# get hersteller names
hash["hersteller"] = hersteller[hash["hersteller_nummer"]].strip.gsub("\u00A0", "") if hash["hersteller_nummer"].present?
# set hierarchie
hash["hierarchie"] = if hash["hauptgruppe"].present? && hash["gruppe"].present?
"|" + hash["hauptgruppe"].strip + "|" + hash["gruppe"].strip
else
"|"
end
# TODO: validate
# enqueue hash
buffer << hash
count += 1
if count % 1000 == 0
Article.index.import buffer
buffer = []
puts count
end
end
Article.index.import buffer unless buffer.empty?
puts count
puts "refresh index"
Article.index.refresh
我的文章,rb看起来像
article.rb
property :artikelnummer, type: 'string', index: 'not_analyzed'
property :eannummer, type: 'string', index: 'not_analyzed'
property :bezeichnung, type: 'multi_field', fields: {
bezeichnung: {type: 'string'},
ngram: {:type => 'string', :index_analyzer => 'ngram_index_analyzer', :search_analyzer => 'ngram_search_analyzer'},
suggest: {:type => 'string', :analyzer => 'suggest_analyzer'}
}
property :bezeichnung_zusatz, type: 'multi_field', fields: {
bezeichnung_zusatz: {type: 'string'},
ngram: {:type => 'string', :index_analyzer => 'ngram_index_analyzer', :search_analyzer => 'ngram_search_analyzer'}
}
property :matchcode, type: 'string', index: 'not_analyzed'
property :mengeneinheit, type: 'string', include_in_all: false
property :gewicht, type: 'float', include_in_all: false
property :hersteller, type: 'multi_field', fields: {
hersteller: {type: 'string'},
unchanged: {type: 'string', :index => 'not_analyzed'},
ngram: {:type => 'string', :index_analyzer => 'ngram_index_analyzer', :search_analyzer => 'ngram_search_analyzer'}
}
property :hersteller_nummer, type: 'string', index: 'not_analyzed', include_in_all: false
property :hersteller_artikelnummer, type: 'string', index: 'not_analyzed'
property :gruppe, type: 'multi_field', fields: {
gruppe: {type: 'string'},
ngram: {:type => 'string', :index_analyzer => 'ngram_index_analyzer', :search_analyzer => 'ngram_search_analyzer'}
}
这个结构我必须用来创建表