Each Search Result is nq * topk two-dimensional structure, as the illustration below. The result indicates that the user input nq vectors and wanted to obtain the topk similar vectors to these nq vectors respectively.
For each query, the topk hit results are in descending order of score. The larger the score, the more similar the hit result is to the vector to be queried. The hit results of different queries are independent of each other.
Therefore, we will only discuss how Proxy merges the results for one query result. For nq query results, we can loop through nq or process them in parallel.
This algorithm is originated from the merging phase of merge sort. The common point of the two is that the results have been sorted when merging, and the difference is that merge sort merges two-way results, proxy reduces merges multiple results.
In contrast, in merge sort, two pointers are used to record the offsets of the two-way results, and proxy reduces uses multiple pointers `locs` to record the offsets of the `k-way` results.
In our specific situation, n indicates that there are 4 results to be merged, `multiple_results` is an array of four `topk`, and each `choiceOffset` in `locs` records the offset of each way being merged.
The `score_this_way` corresponding to this offset records the maximum value of the current way, so when you take a larger `score`, you only need to pick one of the four maximum values.
This ensures that the result we take each time is the largest among the remaining results.