Extract logic for building the search query in private methods.