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

Fix counter increment for query aggregation.

Kishore Nallanbbf67e1979f2
diff --git a/src/index.cpp b/src/index.cpp
index 4a843349..4d81126a 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -434,6 +434,8 @@ void Index::validate_and_preprocess(Index *index, std::vector<index_record>& ite
continue;
}
+ handle_doc_ops(search_schema, index_rec.doc, index_rec.old_doc);
+
if(do_validation) {
Option<uint32_t> validation_op = validator_t::validate_index_in_memory(index_rec.doc, index_rec.seq_id,
default_sorting_field,
@@ -471,7 +473,6 @@ void Index::validate_and_preprocess(Index *index, std::vector<index_record>& ite
}
}
} else {
- handle_doc_ops(search_schema, index_rec.doc, index_rec.old_doc);
if(generate_embeddings) {
records_to_embed.push_back(&index_rec);
}
@@ -6260,7 +6261,6 @@ void Index::get_doc_changes(const index_operation_t op, const tsl::htrie_map<cha
}
}
} else {
- handle_doc_ops(search_schema, update_doc, old_doc);
new_doc = old_doc;
new_doc.merge_patch(update_doc);
diff --git a/test/collection_specific_more_test.cpp b/test/collection_specific_more_test.cpp
index fbc78e22..83181283 100644
--- a/test/collection_specific_more_test.cpp
+++ b/test/collection_specific_more_test.cpp
@@ -2123,6 +2123,76 @@ TEST_F(CollectionSpecificMoreTest, WeightTakingPrecendeceOverMatch) {
ASSERT_EQ(2, res["hits"][1]["text_match_info"]["tokens_matched"].get<size_t>());
}
+TEST_F(CollectionSpecificMoreTest, IncrementingCount) {
+ nlohmann::json schema = R"({
+ "name": "coll1",
+ "fields": [
+ {"name": "title", "type": "string"},
+ {"name": "count", "type": "int32"}
+ ]
+ })"_json;
+
+ Collection* coll1 = collectionManager.create_collection(schema).get();
+
+ // brand new document: create + upsert + emplace should work
+
+ nlohmann::json doc;
+ doc["id"] = "0";
+ doc["title"] = "Foo";
+ doc["$operations"]["increment"]["count"] = 1;
+ ASSERT_TRUE(coll1->add(doc.dump(), CREATE).ok());
+
+ doc.clear();
+ doc["id"] = "1";
+ doc["title"] = "Bar";
+ doc["$operations"]["increment"]["count"] = 1;
+ ASSERT_TRUE(coll1->add(doc.dump(), EMPLACE).ok());
+
+ doc.clear();
+ doc["id"] = "2";
+ doc["title"] = "Taz";
+ doc["$operations"]["increment"]["count"] = 1;
+ ASSERT_TRUE(coll1->add(doc.dump(), UPSERT).ok());
+
+ auto res = coll1->search("*", {}, "", {}, {}, {2}, 10, 1, FREQUENCY, {true}, 5,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10).get();
+
+ ASSERT_EQ(3, res["hits"].size());
+ ASSERT_EQ(1, res["hits"][0]["document"]["count"].get<size_t>());
+ ASSERT_EQ(1, res["hits"][1]["document"]["count"].get<size_t>());
+ ASSERT_EQ(1, res["hits"][2]["document"]["count"].get<size_t>());
+
+ // should support updates
+
+ doc.clear();
+ doc["id"] = "0";
+ doc["title"] = "Foo";
+ doc["$operations"]["increment"]["count"] = 3;
+ ASSERT_TRUE(coll1->add(doc.dump(), UPSERT).ok());
+
+ doc.clear();
+ doc["id"] = "1";
+ doc["title"] = "Bar";
+ doc["$operations"]["increment"]["count"] = 3;
+ ASSERT_TRUE(coll1->add(doc.dump(), EMPLACE).ok());
+
+ doc.clear();
+ doc["id"] = "2";
+ doc["title"] = "Bar";
+ doc["$operations"]["increment"]["count"] = 3;
+ ASSERT_TRUE(coll1->add(doc.dump(), UPDATE).ok());
+
+ res = coll1->search("*", {}, "", {}, {}, {2}, 10, 1, FREQUENCY, {true}, 5,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10).get();
+
+ ASSERT_EQ(3, res["hits"].size());
+ ASSERT_EQ(4, res["hits"][0]["document"]["count"].get<size_t>());
+ ASSERT_EQ(4, res["hits"][1]["document"]["count"].get<size_t>());
+ ASSERT_EQ(4, res["hits"][2]["document"]["count"].get<size_t>());
+}
+
TEST_F(CollectionSpecificMoreTest, HighlightOnFieldNameWithDot) {
nlohmann::json schema = R"({
"name": "coll1",