Performs a nearest neighbor search.