FIX: race conditions in search menu (PR #9262)

Race conditions could lead the previous query search term to be used in the next query. This commit also attempts to simplify code.

GitHub

Very nice, thank you.