diff --git a/src/pq_flash_index.cpp b/src/pq_flash_index.cpp index 7e2bda4e2..8e1b6e985 100644 --- a/src/pq_flash_index.cpp +++ b/src/pq_flash_index.cpp @@ -1700,7 +1700,9 @@ void PQFlashIndex::cached_beam_search(const T *query1, const uint64_t } // copy k_search values - for (uint64_t i = 0; i < k_search; i++) + uint64_t ret_count = full_retset.size() > k_search + ? k_search : full_retset.size(); + for (uint64_t i = 0; i < ret_count; i++) { indices[i] = full_retset[i].id; auto key = (uint32_t)indices[i];