如何等待Azure搜索完成对文档的索引编制?用于集成测试 [英] How to wait for Azure Search to finish indexing document? For integration testing purpose

查看:79
本文介绍了如何等待Azure搜索完成对文档的索引编制?用于集成测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一套自动化集成测试。每个测试都会在查询数据并验证预期结果之前将数据推送到Azure搜索索引中。

I'm building a suite of automated integration tests. Each test push data into the Azure Search index before querying it and verifying the expected results.

索引在服务中异步发生,并且在索引调用返回后不能立即获得数据

测试在大多数情况下当然执行得太快。

The indexation happens asynchronously in the service and data aren't immediatly available after the indexing call returns successfully.
The test execute of course too rapidly most of the time.

我一直尝试查询文档,直到找到为止:

I've tried querying the document until it's found:

// Wait for the indexed document to become available
while (await _index.Documents.SearchAsync("DocumentId").Results.SingleOrDefault() == null) { }

但奇怪的是,后面的搜索查询通常找不到任何东西:

But oddly enough, a search query just behind won't generally find anything:

// Works 10% of the time, even after the above loop
await _index.Documents.SearchAsync(query.Text);

使用任意暂停可以工作,但是不能保证,我希望测试能够尽快执行

Using an arbitrary pause works, but it's not guaranteed and I'd like the tests to execute as fast as possible.

Thread.Sleep(3000);






Azure搜索文档:


Azure Search documentation:


索引在Azure搜索服务中异步发生,因此示例应用程序需要等待一小段时间,以确保文档可用于搜索。 通常只有在演示,测试和示例应用程序中才需要这样的延迟。

任何解决方案都不会影响测试性能?

Aren't there any solution without scarifying tests performance?

推荐答案

如果您的服务有多个搜索单元,则无法确定何时文档已被完全索引。这是一个明智的决定,是要使用索引/查询来提高索引的性能,而不要使用强一致性保证。

If your service has multiple search units, there is no way to determine when a document has been fully indexed. This is a deliberate decision to favor increased indexing/query performance over strong consistency guarantees.

如果您要对单个单元搜索服务进行测试,则该方法(保持检查对于存在查询而不是查找的文档)应该可以。

If you're running tests against a single unit search service, the approach (keep checking for document existence with a query rather than a lookup) should work.

请注意,在免费层搜索服务上,这将起作用,因为托管在多个共享资源上,不算作一个单元。您会看到与专用的多单元服务相同的短暂矛盾之处。

Note that on a free tier search service this will not work as it's hosted on multiple shared resources and does not count as a single unit. You'll see the same brief inconsistency that you would with a dedicated multi-unit service

否则,一种可能的改进是使用重试以及更短的睡眠时间。

Otherwise, one possible improvement would be to use retries along with a smaller sleep time.

这篇关于如何等待Azure搜索完成对文档的索引编制?用于集成测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆