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

Handle increment for creation.

Kishore Nallana9da41dd0e83
diff --git a/include/index.h b/include/index.h
index 38fdc576..138c774d 100644
--- a/include/index.h
+++ b/include/index.h
@@ -526,7 +526,7 @@ private:
static void compute_facet_stats(facet &a_facet, uint64_t raw_value, const std::string & field_type);
static void handle_doc_ops(const tsl::htrie_map<char, field>& search_schema,
- nlohmann::json& update_doc, const nlohmann::json& old_doc, nlohmann::json& new_doc);
+ nlohmann::json& update_doc, const nlohmann::json& old_doc);
static void get_doc_changes(const index_operation_t op, const tsl::htrie_map<char, field>& search_schema,
nlohmann::json &update_doc, const nlohmann::json &old_doc, nlohmann::json &new_doc,
diff --git a/src/index.cpp b/src/index.cpp
index b337bb4b..66a1470d 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -455,6 +455,7 @@ void Index::validate_and_preprocess(Index *index, std::vector<index_record>& ite
scrub_reindex_doc(search_schema, index_rec.doc, index_rec.del_doc, index_rec.old_doc);
embed_fields(index_rec.new_doc, embedding_fields, search_schema);
} else {
+ handle_doc_ops(search_schema, index_rec.doc, index_rec.old_doc);
embed_fields(index_rec.doc, embedding_fields, search_schema);
}
@@ -6130,7 +6131,7 @@ void Index::refresh_schemas(const std::vector<field>& new_fields, const std::vec
}
void Index::handle_doc_ops(const tsl::htrie_map<char, field>& search_schema,
- nlohmann::json& update_doc, const nlohmann::json& old_doc, nlohmann::json& new_doc) {
+ nlohmann::json& update_doc, const nlohmann::json& old_doc) {
/*
{
@@ -6154,7 +6155,6 @@ void Index::handle_doc_ops(const tsl::htrie_map<char, field>& search_schema,
}
auto updated_value = existing_value + item.value().get<int32>();
- new_doc[item.key()] = updated_value;
update_doc[item.key()] = updated_value;
}
}
@@ -6174,9 +6174,10 @@ void Index::get_doc_changes(const index_operation_t op, const tsl::htrie_map<cha
} else {
new_doc = old_doc;
- handle_doc_ops(search_schema, update_doc, old_doc, new_doc);
+ handle_doc_ops(search_schema, update_doc, old_doc);
new_doc.merge_patch(update_doc);
+
if(old_doc.contains(".flat")) {
new_doc[".flat"] = old_doc[".flat"];
for(auto& fl: update_doc[".flat"]) {
diff --git a/test/collection_operations_test.cpp b/test/collection_operations_test.cpp
index aaf22c46..fc355f1a 100644
--- a/test/collection_operations_test.cpp
+++ b/test/collection_operations_test.cpp
@@ -101,4 +101,43 @@ TEST_F(CollectionOperationsTest, IncrementInt32Value) {
ASSERT_EQ("0", res["hits"][0]["document"]["id"].get<std::string>());
ASSERT_EQ("The Sherlock Holmes", res["hits"][0]["document"]["title"].get<std::string>());
ASSERT_EQ(101, res["hits"][0]["document"]["points"].get<size_t>());
-}
\ No newline at end of file
+}
+
+TEST_F(CollectionOperationsTest, IncrementInt32ValueCreationViaOptionalField) {
+ nlohmann::json schema = R"({
+ "name": "coll1",
+ "fields": [
+ {"name": "title", "type": "string"},
+ {"name": "points", "type": "int32", "optional": true}
+ ]
+ })"_json;
+
+ Collection *coll = collectionManager.create_collection(schema).get();
+
+ nlohmann::json doc;
+ doc["id"] = "0";
+ doc["title"] = "Sherlock Holmes";
+ doc["$operations"] = R"({"increment": {"points": 1}})"_json;
+ ASSERT_TRUE(coll->add(doc.dump(), EMPLACE).ok());
+
+ auto res = coll->search("*", {"title"}, "points:1", {}, {}, {0}, 3, 1, FREQUENCY, {false}).get();
+ ASSERT_EQ(1, res["hits"].size());
+ ASSERT_EQ(3, res["hits"][0]["document"].size());
+ ASSERT_EQ("0", res["hits"][0]["document"]["id"].get<std::string>());
+ ASSERT_EQ("Sherlock Holmes", res["hits"][0]["document"]["title"].get<std::string>());
+ ASSERT_EQ(1, res["hits"][0]["document"]["points"].get<size_t>());
+
+ // try same with CREATE action
+ doc.clear();
+ doc["id"] = "1";
+ doc["title"] = "Harry Potter";
+ doc["$operations"] = R"({"increment": {"points": 10}})"_json;
+ ASSERT_TRUE(coll->add(doc.dump(), CREATE).ok());
+
+ res = coll->search("*", {"title"}, "points:10", {}, {}, {0}, 3, 1, FREQUENCY, {false}).get();
+ ASSERT_EQ(1, res["hits"].size());
+ ASSERT_EQ(3, res["hits"][0]["document"].size());
+ ASSERT_EQ("1", res["hits"][0]["document"]["id"].get<std::string>());
+ ASSERT_EQ("Harry Potter", res["hits"][0]["document"]["title"].get<std::string>());
+ ASSERT_EQ(10, res["hits"][0]["document"]["points"].get<size_t>());
+}