如何处理 Elasticsearch 索引延迟 [英] How to deal with Elasticsearch index delay

查看:110
本文介绍了如何处理 Elasticsearch 索引延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的场景:

我有一个包含用户列表的页面.我通过我的 Web 界面创建一个新用户并将其保存到服务器.服务器在elasticsearch中索引文档并成功返回.然后我被重定向到不包含新用户的列表页面,因为在 elasticsearch 中搜索文档可能需要长达 1 秒的时间

I have a page that contains a list of users. I create a new user through my web interface and save it to the server. The server indexes the document in elasticsearch and returns successfully. I am then redirected to the list page which doesn't contain the new user because it can take up to 1-second for documents to become available for search in elasticsearch

elasticsearch 中的近实时搜索.

elasticsearch 指南说你可以手动刷新索引,但说不要在生产中这样做.

The elasticsearch guide says you can manually refresh the index, but says not to do it in production.

...不要在每次为生产中的文档编制索引时都进行手动刷新;它会损害你的表现.相反,您的应用程序需要了解 Elasticsearch 近乎实时的特性,并为之做好准备.

...don’t do a manual refresh every time you index a document in production; it will hurt your performance. Instead, your application needs to be aware of the near real-time nature of Elasticsearch and make allowances for it.

我想知道其他人是如何解决这个问题的?我希望有一个事件或我可以听的东西告诉我文档何时可供搜索,但似乎没有类似的东西.简单地等待 1 秒似乎是合理的,但这似乎是一个坏主意,因为它可能花费的时间比这少得多.

I'm wondering how other people get around this? I wish there was an event or something I could listen for that would tell me when the document was available for search but there doesn't appear to be anything like that. Simply waiting for 1-second is plausible but it seems like a bad idea because it presumably could take much less time than that.

谢谢!

推荐答案

尽管您可以强制 ES 自行刷新,但您已经正确地注意到它可能会损害性能.解决此问题的一种解决方案以及人们(包括我自己)经常做的事情是提供实时幻觉.归根结底,这只是一个 UX 挑战,而不是真正的技术限制.

Even though you can force ES to refresh itself, you've correctly noticed that it might hurt performance. One solution around this and what people often do (myself included) is to give an illusion of real-time. In the end, it's merely a UX challenge and not really a technical limitation.

当重定向到用户列表时,您可以人为地将您刚刚创建的新记录包含到用户列表中,就好像该记录是由 ES 本身返回的一样.没有什么能阻止你这样做.当你决定刷新页面时,新的用户记录会被 ES 正确返回,没有人关心这条记录来自哪里,那一刻用户关心的是他想看到新记录他刚刚被创造出来,只是因为我们习惯于按顺序思考.

When redirecting to the list of users, you could artificially include the new record that you've just created into the list of users as if that record had been returned by ES itself. Nothing prevents you from doing that. And by the time you decide to refresh the page, the new user record would be correctly returned by ES and no one cares where that record is coming from, all the user cares about at that moment is that he wants to see the new record that he's just created, simply because we're used to think sequentially.

实现此目的的另一种方法是重新加载一个空的用户列表框架,然后通过 Ajax 或其他异步方式检索用户列表并显示它.

Another way to achieve this is by reloading an empty user list skeleton and then via Ajax or some other asynchronous way, retrieve the list of users and display it.

另一种方法是在 UI 上提供视觉提示/线索,表明后台正在发生某些事情并且很快就会有更新.

Yet another way is to provide a visual hint/clue on the UI that something is happening in the background and that an update is to be expected very shortly.

归根结底,这一切都归结为不是让用户感到惊讶,而是为他们提供足够的线索,让他们了解发生了什么、正在发生什么以及他们仍然应该期待发生什么.

In the end, it all boils down to not surprise users but to give them enough clues as to what has happened, what is happening and what they should still expect to happen.

更新:

为了完整起见,这个答案早于 ES5,它引入了一种方法来确保索引调用不会返回,直到在搜索索引时文档可见或返回错误代码.通过在索引数据时使用 ?refresh=wait_for,您可以确定当 ES 响应时,新数据将被索引.

Just for completeness' sake, this answer predates ES5, which introduced a way to make sure that the indexing call would not return until the document is either visible when searching the index or return an error code. By using ?refresh=wait_for when indexing your data you can be certain that when ES responds, the new data will be indexed.

这篇关于如何处理 Elasticsearch 索引延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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