且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何处理弹性搜索索引延迟

更新时间:2023-02-08 19:23:19

即使您可以强制ES自行刷新,您已经正确注意到可能会损害性能。一个解决这个问题的方法和人们经常做的(我自己包括的)是给出一个实时的错觉。最后,它只是一个UX挑战,而不是真正的技术限制。



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



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



另一种方法是在UI上提供一个视觉提示/线索,在后台发生了一些事情,并且很快就会期待更新。 >

最后,这一切都归结为用户的惊喜,但是给他们足够的线索,发现了什么,发生了什么,还有什么他们应该会发生的事情。 p>

Here's my scenario:

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

Near real-time search in elasticsearch.

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

...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.

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.

Thanks!

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.

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.

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.

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.