fix query when searching by field other than username.
refactor