【问题标题】:using ruby, how to index a postgres database with elasticsearch?使用 ruby​​,如何使用 elasticsearch 索引 postgres 数据库?
【发布时间】:2014-08-01 20:30:18
【问题描述】:

我正在使用 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 是否有“更新”命令,以便我可以在每次有新记录时更新索引,还是每次搜索时都必须重新索引数据库?

非常感谢您的帮助。

【问题讨论】:

    标签: ruby postgresql elasticsearch sinatra


    【解决方案1】:

    您正在尝试做的是高度非正统的 - 尝试使用相同的数据同步两个不同的存储库是脆弱的、容易出错的并且是一个令人头疼的秘诀。

    从您的 sn-ps 中显示的数据类型来看,我什至看不到用例,为什么不简单地将 add an index 写入您的 postgres 表? Postgres 甚至支持full text search,因此即使您真的很想为此使用elasticsearch(毕竟-这是它的主要功能之一),对于大多数用例来说,postgres 都可以。

    在您绝对需要 postgres不提供的elasticsearch功能的远程情况下,我建议将此表移植到elasticsearch 完全,而不是尝试保留这两个存储库同步...

    【讨论】:

    • 很公平,感谢您的观察。然而,我正在使用的 Sequel ORM,虽然它支持“full_text_search”,但它只使用一个单词而不是查询元素的短语。这是限制性的,所以我决定(似乎错误地)尝试使用弹性搜索。我考虑过的另一种方法是上传一个 csv 文件,然后对其进行索引。那么问题可能是相同的,我如何生成一个不会返回错误的正确 json 文档。你对此有看法吗?感谢您的宝贵时间。
    • 抱歉,不确定您所说的“堆栈跟踪”是什么意思。您想从终端获取全部信息吗?是这个意思吗?
    • 是的,整个错误消息,以及发生错误的来源列表
    猜你喜欢
    • 2013-06-23
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 2022-10-07
    • 2015-01-22
    • 2014-11-02
    • 1970-01-01
    • 2014-02-20
    相关资源
    最近更新 更多