diff --git a/src/index.cpp b/src/index.cpp index 78fa2a16..126e579f 100644 --- a/src/index.cpp +++ b/src/index.cpp @@ -2517,6 +2517,10 @@ void Index::search(std::vector& field_query_tokens, const std::v if (!vector_query.field_name.empty()) { auto k = std::max(vector_query.k, per_page * page); + if(vector_query.query_doc_given) { + // since we will omit the query doc from results + k++; + } VectorFilterFunctor filterFunctor(filter_ids, filter_ids_length); auto& field_vector_index = vector_index.at(vector_query.field_name); diff --git a/test/collection_vector_search_test.cpp b/test/collection_vector_search_test.cpp index 9ae9a9fb..2e207bbd 100644 --- a/test/collection_vector_search_test.cpp +++ b/test/collection_vector_search_test.cpp @@ -159,6 +159,17 @@ TEST_F(CollectionVectorTest, BasicVectorQuerying) { ASSERT_STREQ("0", results["hits"][0]["document"]["id"].get().c_str()); ASSERT_STREQ("2", results["hits"][1]["document"]["id"].get().c_str()); + // `k` value should work correctly + results = coll1->search("*", {}, "", {}, {}, {0}, 1, 1, FREQUENCY, {true}, Index::DROP_TOKENS_THRESHOLD, + spp::sparse_hash_set(), + spp::sparse_hash_set(), 10, "", 30, 5, + "", 10, {}, {}, {}, 0, + "", "", {}, 1000, true, false, true, "", false, 6000 * 1000, 4, 7, fallback, + 4, {off}, 32767, 32767, 2, + false, true, "vec:([], id: 1, k: 1)").get(); + + ASSERT_EQ(1, results["hits"].size()); + // when `id` does not exist, return appropriate error res_op = coll1->search("*", {}, "", {}, {}, {0}, 10, 1, FREQUENCY, {true}, Index::DROP_TOKENS_THRESHOLD, spp::sparse_hash_set(),