rebuild search query generation to be much more solid