10up / ElasticPress

A fast and flexible search and query engine for WordPress.
https://elasticpress.io
GNU General Public License v2.0
1.25k stars 313 forks source link

When using protected content the admin may show outdated information under some scenarios #1415

Open dustinrue opened 5 years ago

dustinrue commented 5 years ago

Describe the bug With protected content enabled interactions in the admin can result in inconsistent information.

Steps to Reproduce

  1. On a small site with minimal plugins
  2. Create a post and save it
  3. In the post listing, click on trash to put the item in the trash
  4. Note that when the page refreshes the item is still shown in the listing
  5. Refresh the listing, note that the item has now no longer in the list

Expected behavior Clicking trash would result in the post no longer being listed

Environment information

Cause and potential solution This issue occurs because the request to update the post in Elasticsearch doesn't wait for Elasticsearch to commit the change, that is Elasticsearch returns immediately. The change isn't reflected in search results until the next refresh interval has fired which defaults to 1 second. When the WordPress admin loads again it does a search against an old copy of the index and gets outdated results back.

The potential solution is to add ?refresh=wait_for to the index request for the affected document(s). For performance I wouldn't recommend issuing a refresh command manually after indexing the document, only when making the index request. Manually issuing a refresh will just result in a doubled up refresh (because ES is already doing scheduled refreshes anyway) potentially causing more delay than necessary.

I also wouldn't use wait_for unless the command came from the admin page while users are making changes they would expect to be fresh on the next page load.

It maybe useful to allow for disable waiting in the event ES is configured with a longer than usual refresh interval.

felipeelia commented 2 years ago

Just tested this and $path = apply_filters( 'ep_bulk_index_request_path', $index . '/_bulk?refresh=wait_for', $body, $type ); really fixes the problem.

felipeelia commented 2 years ago

Let's test with more posts at once.