私はElasticSearchをテストするスイートのパフォーマンスを改善しようとしています。ElasticSearchのアップデートは即時ではありません.ElasticSearchがインデックスの更新を完了するのをどのように待つのですか?
Elasticsearchは更新直後にインデックスを更新しないため、テストに時間がかかります。たとえば、アサーションエラーを発生させずに次のコードを実行します。
from elasticsearch import Elasticsearch
elasticsearch = Elasticsearch('es.test')
# Asumming that this is a clean and empty elasticsearch instance
elasticsearch.update(
index='blog',
doc_type=,'blog'
id=1,
body={
....
}
)
results = elasticsearch.search()
assert not results
# results are not populated
現在出て、この問題に対する解決策はElasticSearchにそれがインデックスのアップデートするためにいくつかの時間を与えるために、コードにtime.sleep
コールをドロップされて一緒にハッキング。
from time import sleep
from elasticsearch import Elasticsearch
elasticsearch = Elasticsearch('es.test')
# Asumming that this is a clean and empty elasticsearch instance
elasticsearch.update(
index='blog',
doc_type=,'blog'
id=1,
body={
....
}
)
# Don't want to use sleep functions
sleep(1)
results = elasticsearch.search()
assert len(results) == 1
# results are now populated
明らかにそれが失敗むしろがちだとして、これは、素晴らしいではありませんElasticSearchはそれがインデックスのアップデートに時間が秒以上を要する仮にあれば、それがどのように可能性は低いにも関わらず、テストは失敗します。このようなテストを100回実行すると、非常に遅くなります。
私はこの問題を解決するために、pending cluster jobsにクエリを実行して、実行するタスクが残っているかどうかを確認しました。しかし、これはうまくいかず、このコードはアサーションエラーなしで実行されます。 ElasticSearchはそれのインデックスを更新終了するのを
from elasticsearch import Elasticsearch
elasticsearch = Elasticsearch('es.test')
# Asumming that this is a clean and empty elasticsearch instance
elasticsearch.update(
index='blog',
doc_type=,'blog'
id=1,
body={
....
}
)
# Query if there are any pending tasks
while elasticsearch.cluster.pending_tasks()['tasks']:
pass
results = elasticsearch.search()
assert not results
# results are not populated
だから、基本的には、戻って私の元の質問に、ElasticSearchの更新は即時 ではありません、あなたはどのように待機していますか?バージョン5.0.0のよう
のためのおかげを待ちたくない場合にも、elasticsearch.Refresh(「ブログ」)を呼び出すことができます。本当に遅い受け入れのためのお詫び。 – Rollo