![](/img/trans.png)
[英]ElasticSearch updates are not immediate, how do you wait for ElasticSearch to finish updating it's index?
[英]Elasticsearch reindexing: How do you direct updates to the new index while it is being built?
我了解使用别名重新索引以避免停机,如下所述: 是否有更聪明的方法来对Elasticsearch进行索引?
但是仍然存在一个问题:说重新索引需要一个小时,而原始数据库却在不断变化。 我需要任何更新才能同时访问两个索引。
有什么办法吗?
如果不是这样,我希望更新到新索引,而查询仍然从旧索引进行。 但是至少在Tyre中,我还没有找到使用不同索引进行读写的方法。 能做到吗?
您无法从Elasticsearch同时更新两个索引。 您可以自己处理,也可以向Elasticsearch提出2个索引请求。
就是说,您可以在这里使用别名 ,尽管我很确定您可以使用Tire来搜索多个索引(但我不知道Tyre)
您有一个旧索引1
将所有内容推送到index2在index1 index2的顶部添加一个别名索引
索引编制完成后,删除index1
为了即使在使用新的用户生成内容更新搜索系统时也允许零停机时间索引更改,您可以使用以下策略:
为指向ES索引的读写操作定义别名。 更新模型后,查找model_write别名并使用它写入所有跟踪的索引,该索引将包括当前活动的索引以及在后台构建的任何索引。
class User < ActiveRecord::Base
def self.index_for_search(user_id)
Timeout::timeout(5) do
user = User.find_by_id(user_id)
write_alias = Tire::Alias.find("users_write")
if write_alias
write_alias.indices.each do |index_name|
index = Tire::Index.new(index_name)
if user
index.store user
else
index.remove 'user', user_id
end
end
else
raise "Cannot index without existence of 'users_write' alias."
end
end
end
end
现在,当您要进行完整的索引重建(或初始索引创建)时,添加一个新索引,将其添加到别名,然后开始构建它,知道任何活动的用户都将同时将他们的数据添加到这两个索引中。 继续从旧索引读取,直到建立新索引,然后切换读取别名。
class SearchHelper
def self.set_alias_to_index(alias_name, index_name, clear_aliases = true)
tire_alias = Tire::Alias.find(alias_name)
if tire_alias
tire_alias.indices.clear if clear_aliases
tire_alias.indices.add index_name
else
tire_alias = Tire::Alias.new(:name => alias_name)
tire_alias.index index_name
end
tire_alias.save
end
end
def self.reindex_users_index(options = {})
finished = false
read_alias_name = "users"
write_alias_name = "users_write"
new_index_name = "#{read_alias_name}_#{Time.now.to_i}"
# Make new index for re-indexing.
index = Tire::Index.new(new_index_name)
index.create :settings => analyzer_configuration,
:mappings => { :user => user_mapping }
index.refresh
# Add the new index to the write alias so that any system changes while we're re-indexing will be reflected.
SearchHelper.set_alias_to_index(write_alias_name, new_index_name, false)
# Reindex all users.
User.find_in_batches do |batch|
index.import batch.map { |m| m.to_elasticsearch_json }
end
index.refresh
finished = true
# Update the read and write aliases to only point at the newly re-indexed data.
SearchHelper.set_alias_to_index read_alias_name, new_index_name
SearchHelper.set_alias_to_index write_alias_name, new_index_name
ensure
index.delete if defined?(index) && !finished
end
可以在以下位置找到描述此策略的帖子: http : //www.mavengineering.com/blog/2014/02/12/seamless-elasticsearch-reindexing/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.