SearchEntities API call refactored for performance

1. If, for example, the $ids array already contained 10 of 11
results the two searchEntities() calls requested 11 instead of 1
additional results.

2. Rewrote the code to get rid of the nested loop. Probably not a
performance problem since the payload of the nested loop didn't do
much. However, I feel better not having nested loops. I also think
the code makes more sense now.

Related to (but not a fix for) bug 44746.

Bug: 44746
Change-Id: Ia6e2945d954b874c99504398092fa27436bb99e2
Merge "SearchEntities API call refactored for performance"