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

add test for skip_to_rev() and bug fix for single null record

krunal338f30f51b9d
diff --git a/include/posting_list.h b/include/posting_list.h
index 6da4345d..5103bae5 100644
--- a/include/posting_list.h
+++ b/include/posting_list.h
@@ -105,6 +105,7 @@ public:
void set_index(uint32_t index);
[[nodiscard]] uint32_t id() const;
[[nodiscard]] uint32_t last_block_id() const;
+ [[nodiscard]] uint32_t first_block_id() const;
[[nodiscard]] inline uint32_t index() const;
[[nodiscard]] inline block_t* block() const;
[[nodiscard]] uint32_t get_field_id() const;
diff --git a/src/posting_list.cpp b/src/posting_list.cpp
index 038435ab..ed90a778 100644
--- a/src/posting_list.cpp
+++ b/src/posting_list.cpp
@@ -1660,10 +1660,10 @@ posting_list_t::iterator_t::iterator_t(const std::map<last_id_t, block_t*>* id_b
ids = curr_block->ids.uncompress();
offset_index = curr_block->offset_index.uncompress();
offsets = curr_block->offsets.uncompress();
- }
- if(reverse) {
- curr_index = curr_block->ids.getLength()-1;
+ if(reverse) {
+ curr_index = curr_block->ids.getLength()-1;
+ }
}
}
@@ -1700,6 +1700,15 @@ uint32_t posting_list_t::iterator_t::last_block_id() const {
return ids[size - 1];
}
+uint32_t posting_list_t::iterator_t::first_block_id() const {
+ auto size = curr_block->size();
+ if(size == 0) {
+ return 0;
+ }
+
+ return ids[0];
+}
+
uint32_t posting_list_t::iterator_t::id() const {
return ids[curr_index];
}
@@ -1747,7 +1756,7 @@ void posting_list_t::iterator_t::skip_to(uint32_t id) {
void posting_list_t::iterator_t::skip_to_rev(uint32_t id) {
// first look to skip within current block
- if(id >= this->last_block_id()) {
+ if(id >= this->first_block_id()) {
while(curr_index > 0 && this->id() > id) {
curr_index--;
}
diff --git a/test/collection_grouping_test.cpp b/test/collection_grouping_test.cpp
index 23815f75..c14a2608 100644
--- a/test/collection_grouping_test.cpp
+++ b/test/collection_grouping_test.cpp
@@ -885,4 +885,105 @@ TEST_F(CollectionGroupingTest, GroupSortingWithoutGroupingFields) {
ASSERT_EQ(res.ok(), false);
ASSERT_EQ(res.error(), "group_by parameters should not be empty when using sort_by group_found");
+}
+
+TEST_F(CollectionGroupingTest, SkipToReverseGroupBy) {
+ std::vector<field> fields = {
+ field("brand", field_types::STRING, true, true),
+ };
+
+ Collection* coll2 = collectionManager.get_collection("coll2").get();
+ if(coll2 == nullptr) {
+ coll2 = collectionManager.create_collection("coll2", 1, fields).get();
+ }
+
+ nlohmann::json doc;
+
+ doc["id"] = "0";
+ doc["brand"] = nullptr;
+ ASSERT_TRUE(coll2->add(doc.dump()).ok());
+
+ auto res = coll2->search("*", {}, "", {}, {}, {0}, 10, 1, FREQUENCY,
+ {true}, 10,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 5,
+ "", 10,
+ {}, {}, {"brand"}, 2,
+ "<mark>", "</mark>", {3,3}, 1000, true, false, true, "", false, 6000 * 1000, 4, 7, fallback,
+ 4, {off}, 0, 0, 0, 2, false, "", true, 0, max_score,
+ 100, 0, 0, HASH, 30000, 2, "", {}, {}, right_to_left, true, false).get();
+
+ ASSERT_EQ(1, res["grouped_hits"].size());
+
+ ASSERT_EQ(0, res["grouped_hits"][0]["group_key"].size());
+ ASSERT_EQ(1, res["grouped_hits"][0]["hits"].size());
+ ASSERT_EQ("0", res["grouped_hits"][0]["hits"][0]["document"]["id"].get<std::string>());
+
+ doc["id"] = "1";
+ doc["brand"] = "adidas";
+ ASSERT_TRUE(coll2->add(doc.dump()).ok());
+
+ doc["id"] = "2";
+ doc["brand"] = "puma";
+ ASSERT_TRUE(coll2->add(doc.dump()).ok());
+
+ doc["id"] = "3";
+ doc["brand"] = nullptr;
+ ASSERT_TRUE(coll2->add(doc.dump()).ok());
+
+ doc["id"] = "4";
+ doc["brand"] = "nike";
+ ASSERT_TRUE(coll2->add(doc.dump()).ok());
+
+
+ res = coll2->search("*", {}, "", {}, {}, {0}, 10, 1, FREQUENCY,
+ {true}, 10,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 5,
+ "", 10,
+ {}, {}, {"brand"}, 2,
+ "<mark>", "</mark>", {3,3}, 1000, true, false, true, "", false, 6000 * 1000, 4, 7, fallback,
+ 4, {off}, 0, 0, 0, 2, false, "", true, 0, max_score,
+ 100, 0, 0, HASH, 30000, 2, "", {}, {}, right_to_left, true, false).get();
+
+ ASSERT_EQ(5, res["grouped_hits"].size());
+
+ ASSERT_EQ("nike", res["grouped_hits"][0]["group_key"][0].get<std::string>());
+ ASSERT_EQ(1, res["grouped_hits"][0]["hits"].size());
+
+ ASSERT_EQ(0, res["grouped_hits"][1]["group_key"].size());
+ ASSERT_EQ(1, res["grouped_hits"][1]["hits"].size());
+ ASSERT_EQ("3", res["grouped_hits"][1]["hits"][0]["document"]["id"].get<std::string>());
+
+ ASSERT_EQ("puma", res["grouped_hits"][2]["group_key"][0].get<std::string>());
+ ASSERT_EQ(1, res["grouped_hits"][2]["hits"].size());
+
+ ASSERT_EQ("adidas", res["grouped_hits"][3]["group_key"][0].get<std::string>());
+ ASSERT_EQ(1, res["grouped_hits"][3]["hits"].size());
+
+ ASSERT_EQ(0, res["grouped_hits"][4]["group_key"].size());
+ ASSERT_EQ(1, res["grouped_hits"][4]["hits"].size());
+ ASSERT_EQ("0", res["grouped_hits"][4]["hits"][0]["document"]["id"].get<std::string>());
+
+ res = coll2->search("*", {}, "", {}, {}, {0}, 10, 1, FREQUENCY,
+ {true}, 10,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 5,
+ "", 10,
+ {}, {}, {"brand"}, 2,
+ "<mark>", "</mark>", {3,3}, 1000, true, false, true, "", false, 6000 * 1000, 4, 7, fallback,
+ 4, {off}, 0, 0, 0, 2, false, "", true, 0, max_score,
+ 100, 0, 0, HASH, 30000, 2, "", {}, {}, right_to_left, true, true).get();
+
+ ASSERT_EQ(4, res["grouped_hits"].size());
+
+ ASSERT_EQ("nike", res["grouped_hits"][0]["group_key"][0].get<std::string>());
+ ASSERT_EQ(1, res["grouped_hits"][0]["hits"].size());
+
+ ASSERT_EQ(0, res["grouped_hits"][1]["group_key"].size());
+ ASSERT_EQ(2, res["grouped_hits"][1]["hits"].size());
+ ASSERT_EQ("3", res["grouped_hits"][1]["hits"][0]["document"]["id"].get<std::string>());
+
+ ASSERT_EQ("puma", res["grouped_hits"][2]["group_key"][0].get<std::string>());
+ ASSERT_EQ(1, res["grouped_hits"][2]["hits"].size());
}
\ No newline at end of file