Refactor the quick search code.

Remove the duplicate code and wrap it in a function so it can be
customised for the page it's being used on.