由于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
目前我们针对这个问题的临时解决方案是在代码中加入time.sleep
调用,以给ElasticSearch一些时间来更新其索引。
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更新其索引所需时间超过一秒钟,尽管这很不可能,测试将失败。而且当您运行像这样的数百个测试时,速度非常慢。
我试图解决这个问题,通过查询待处理集群任务,以查看是否有剩余任务需要完成。但是这并不起作用,代码将在没有断言错误的情况下运行。
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 的更新不是立即执行的,你怎样等待 ElasticSearch 完成索引更新呢?