elasticsearch,sinatra,Ruby,Postgresql,elasticsearch,Sinatra" /> elasticsearch,sinatra,Ruby,Postgresql,elasticsearch,Sinatra" />

Ruby 如何从postgresdb中选择数据并插入elasticsearch索引?

Ruby 如何从postgresdb中选择数据并插入elasticsearch索引?,ruby,postgresql,elasticsearch,sinatra,Ruby,Postgresql,elasticsearch,Sinatra,使用sinatra、postgres和elasticsearch gem,我想从数据库中选择数据,动态地将其转换为json,然后将其放入elasticsearch索引,即使用刚刚选择的记录创建索引 ruby代码是: get '/send_data_to_elastic' do result = DB[:candidates].map do |r| { :first => r[:first], :last => r[:last], :email => r[:email],

使用sinatra、postgres和elasticsearch gem,我想从数据库中选择数据,动态地将其转换为json,然后将其放入elasticsearch索引,即使用刚刚选择的记录创建索引

ruby代码是:

get '/send_data_to_elastic' do
 result = DB[:candidates].map do |r|
  { :first => r[:first], :last => r[:last], :email => r[:email], :company => r[:company] }
end
 client = Elasticsearch::Client.new log: true
 #client.delete  index: 'mycandidates', type: 'mykind', id: 3
 #client.index  index: 'candidates', type: 'mykind', id: 12, body: {data:'data'}
 client.index  index: 'candidates', type: 'mykind', id: 12, body: result.to_json
end
这不起作用,我得到以下错误:

Elasticsearch::Transport::Transport::Errors::BadRequest at /send_data_to_elastic
[400] {"error":"MapperParsingException[failed to parse]; nested: ElasticSearchParseException[Failed to derive xcontent from (offset=0, length=4): [110, 117, 108, 108]]; ","status":400}

有什么想法吗,伙计们?我是在尝试不可能的事情,还是走错了方向?像往常一样,所有的帮助都得到了感激

我认为这与ruby有关。检查如何在ruby中创建JSON文档。在这之后,应该没有任何东西阻止您在ElasticSearch中对此进行索引。