2016-11-18 1 views
3

私は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のよう

答えて

6

、elasticsearchオプションがあります:インデックス、更新、削除、およびバルクのAPIの上

?refresh=wait_for 

を。このように、ElasticSearchで結果が表示されるまで、要求は応答を受け取りません。 (Yay!)

詳細については、https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-refresh.htmlを参照してください。

編集:この機能は、すでに最新のPython elasticsearch APIの一部であると思われる: https://elasticsearch-py.readthedocs.io/en/master/api.html#elasticsearch.Elasticsearch.index

はにあなたのelasticsearch.updateを変更

elasticsearch.update(
    index='blog', 
    doc_type='blog' 
    id=1, 
    refresh='wait_for', 
    body={ 
     .... 
    } 
) 

、あなたは任意の睡眠を必要はありませんかポーリング。

+0

のためのおかげを待ちたくない場合にも、elasticsearch.Refresh(「ブログ」)を呼び出すことができます。本当に遅い受け入れのためのお詫び。 – Rollo

0

私のために動作するようです:

els.indices.refresh(index) 
els.cluster.health(wait_for_no_relocating_shards=True,wait_for_active_shards='all') 
関連する問題