Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

During import return_id should work for failures.

Kishore Nallanbf8cc0b91e60
diff --git a/src/collection.cpp b/src/collection.cpp
index 079368dd..b2be4cbb 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -897,6 +897,9 @@ void Collection::batch_index(std::vector<index_record>& index_records, std::vect
res["embedding_error"] = index_record.embedding_res;
}
res["code"] = index_record.indexed.code();
+ if (return_id && index_record.doc.contains("id")) {
+ res["id"] = index_record.doc["id"];
+ }
}
json_out[index_record.position] = res.dump(-1, ' ', false,
diff --git a/test/collection_test.cpp b/test/collection_test.cpp
index 48656ee9..2d6610a6 100644
--- a/test/collection_test.cpp
+++ b/test/collection_test.cpp
@@ -1192,7 +1192,9 @@ TEST_F(CollectionTest, ImportDocumentsUpsert) {
R"({"id": "5", "points": 60, "cast":["Logan Lerman","Alexandra Daddario"],"starring":"Ron Perlman","starring_facet":"Ron Perlman","title":"Percy Jackson: Sea of Monsters"})",
R"({"id": "24", "starring": "John", "cast": ["John Kim"], "points": 11})"}; // missing fields
- import_response = coll_mul_fields->add_many(more_records, document, UPSERT);
+ bool return_id = true;
+ import_response = coll_mul_fields->add_many(more_records, document, UPSERT, "",
+ DIRTY_VALUES::COERCE_OR_REJECT, false, return_id);
ASSERT_FALSE(import_response["success"].get<bool>());
ASSERT_EQ(2, import_response["num_imported"].get<int>());
@@ -1203,6 +1205,11 @@ TEST_F(CollectionTest, ImportDocumentsUpsert) {
ASSERT_STREQ("Field `points` has been declared as a default sorting field, but is not found in the document.", import_results[1]["error"].get<std::string>().c_str());
ASSERT_STREQ("Field `title` has been declared in the schema, but is not found in the document.", import_results[3]["error"].get<std::string>().c_str());
+ ASSERT_EQ("1", import_results[0]["id"].get<std::string>());
+ ASSERT_EQ("90", import_results[1]["id"].get<std::string>());
+ ASSERT_EQ("5", import_results[2]["id"].get<std::string>());
+ ASSERT_EQ("24", import_results[3]["id"].get<std::string>());
+
// try to duplicate records without upsert option
more_records = {R"({"id": "1", "title": "Wake up, Harry"})",