optimize search logic