【发布时间】:2016-06-19 04:09:52
【问题描述】:
我在我的一个 Ruby on Rails 项目中使用 Elasticsearch - Bonsai。 所以,到目前为止,事情进展得很顺利。但是,当我们向最终用户推出这个应用程序并且人们开始进来的那一刻,我们注意到单个弹性搜索查询需要 5-7 秒才能响应(对我们来说真的很糟糕的体验)——虽然,我们有 8 -2x Web Dynos 到位。
因此,我们决定将 Bonsai 附加组件升级到 Bonsai 10 并添加 NewRelic 附加组件(以保持关注单个查询需要多长时间才能响应)
以下是我们的环境设置:
Ruby: 2.2.4
Rails: 4.2.0
elasticsearch: 1.0.15
elasticsearch-model: 0.1.8
所以,我们再次将数据导入 Elasticsearch,这是我们的 ElasticSearch 集群运行状况:
pry(main)> Article.__elasticsearch__.client.cluster.health
=> {"cluster_name"=>"elasticsearch",
"status"=>"green",
"timed_out"=>false,
"number_of_nodes"=>3,
"number_of_data_nodes"=>3,
"active_primary_shards"=>1,
"active_shards"=>2,
"relocating_shards"=>0,
"initializing_shards"=>0,
"unassigned_shards"=>0,
"delayed_unassigned_shards"=>0,
"number_of_pending_tasks"=>0,
"number_of_in_flight_fetch"=>0}
以下是NewRelic的ES调用数据
这表明有很大的理由担心。
我的模型article.rb如下:
class Article < ActiveRecord::Base
include Elasticsearch::Model
after_commit on: [:create] do
begin
__elasticsearch__.index_document
rescue Exception => ex
logger.error "ElasticSearch after_commit error on create: #{ex.message}"
end
end
after_commit on: [:update] do
begin
Elasticsearch::Model.client.exists?(index: 'articles', type: 'article', id: self.id) ? __elasticsearch__.update_document : __elasticsearch__.index_document
rescue Exception => ex
logger.error "ElasticSearch after_commit error on update: #{ex.message}"
end
end
after_commit on: [:destroy] do
begin
__elasticsearch__.delete_document
rescue Exception => ex
logger.error "ElasticSearch after_commit error on delete: #{ex.message}"
end
end
def as_indexed_json(options={})
as_json({
only: [ :id, :article_number, :user_id, :article_type, :comments, :posts, :replies, :status, :fb_share, :google_share, :author, :contributor_id, :created_at, :updated_at ],
include: {
posts: { only: [ :id, :article_id, :post ] },
}
})
end
end
现在,如果我查看 Heroku 的 BONSAI 10 计划,它给了我 20 个分片,但在集群的当前状态下,它只使用 1 个活动的主分片和 2 个活动分片。我突然想到了几个问题:
- 将分片数量增加到 20 是否会有所帮助?
- 可以缓存 ES 查询 -- 你也建议这样做吗? -- 它有什么优点和缺点吗?
请帮助我找到可以减少时间并提高 ES 工作效率的方法。
更新
这是我创建的小代码 sn-p https://jsfiddle.net/puneetpandey/wpbohqrh/2/(作为参考),以准确说明为什么我需要如此多地调用 ElasticSearch
在上面的示例中,我显示了几个计数(在每个复选框元素的前面)。为了显示这些计数,我需要通过点击 ES 来获取我得到的数字
好的,所以在阅读了 cmets 并在这里找到了一篇好文章后:How to config elasticsearch cluster on one server to get the best performace on search 我想我已经足够重新构建了
最好的,
普奈特
【问题讨论】:
标签: ruby-on-rails heroku elasticsearch elasticsearch-model bonsai-elasticsearch