Document search algorithm