![](/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.