使用ruby,如何使用elasticsearch索引postgres数据库



我使用的是sinatra和postgres。我想索引一个postgres数据库。

ruby代码为:

get '/elastic_data' do
client = Elasticsearch::Client.new log: true
candidates.select(:id, :first, :last, :email, :industry).map do |row|
@first = row[:first]
puts @first
@industry = row[:industry]
puts @industry
result = {first:@first, industry:@industry}.to_json
client.index  index: 'people', type: 'py', id: 1, body: result
end
end

当我运行此命令时,在终端中我得到了格式良好的json,如下所示:

Eric
Legal
2014-06-11 18:05:00 +0100: PUT http://localhost:9200/people/py/1 [status:200, request:0.004s, query:n/a]
2014-06-11 18:05:00 +0100: > {"first":"Eric","industry":"Legal"}
2014-06-11 18:05:00 +0100: < {"ok":true,"_index":"people","_type":"py","_id":"1","_version":10143}
Kewu
Legal
2014-06-11 18:05:00 +0100: PUT http://localhost:9200/people/py/1 [status:200, request:0.002s, query:n/a]
2014-06-11 18:05:00 +0100: > {"first":"Kewu","industry":"Legal"}
2014-06-11 18:05:00 +0100: < {"ok":true,"_index":"people","_type":"py","_id":"1","_version":10144}

然后我得到一个错误信息,它是这样的:

NoMethodError at /elastic_data
undefined method `bytesize' for #<Hash:0x9228f40>

2的问题:

  1. 出了什么问题,我怎么解决它?
  2. 是否有一个"更新"命令与elasticsearch ruby gem,以便我可以更新索引,每次有一个新的记录,或者我必须重新索引数据库,每次我做一个搜索?

非常感谢您的帮助。

你正在尝试做的是高度非正统的——试图用相同的数据同步两个不同的存储库是脆弱的,容易出错的,而且是一个令人头疼的问题。

从片段中显示的数据类型来看,我甚至没有看到用例,为什么不简单地为postgres表添加一个索引呢?Postgres甚至支持全文搜索,所以即使你真的很想使用elasticsearch(毕竟,这是它的主要功能之一),对于大多数用例,Postgres也会这样做。

在远程情况下,你绝对需要elasticsearch的特性,而postgres没有提供,我建议将这个表完全移植到elasticsearch ,而不是试图保持两个存储库同步…

最新更新