首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用红宝石,如何用elasticsearch索引postgres数据库?

使用红宝石,如何用elasticsearch索引postgres数据库?
EN

Stack Overflow用户
提问于 2014-06-11 17:12:53
回答 1查看 315关注 0票数 0

我用的是辛纳特拉和波斯特格斯。我想索引postgres数据库。

红宝石代码是:

代码语言:javascript
运行
复制
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,如下所示:

代码语言:javascript
运行
复制
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}

然后我得到一个错误消息,就是:

代码语言:javascript
运行
复制
NoMethodError at /elastic_data
undefined method `bytesize' for #<Hash:0x9228f40>

2个问题:

  1. 出什么问题了,我该怎么解决呢?
  2. 是否有一个带有elasticsearch的“更新”命令,以便每次有新记录时都可以更新索引,还是每次搜索时都必须重新索引数据库?

非常感谢您的帮助。

EN

回答 1

Stack Overflow用户

发布于 2014-06-11 17:26:06

你想要做的是非常不正统的--试图用相同的数据同步两个不同的存储库是很脆弱的,容易出错,而且会让人头疼。

从片段中显示的数据类型来看,我甚至没有看到用例,为什么不简单地将添加索引放到postgres表中呢?Postgres甚至支持全文搜索,因此即使您有使用elasticsearch的真正诱惑(毕竟--这是它的主要特性之一),对于大多数用例,postgres也可以。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24168747

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档