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 edge case in less than numerical comparison.

kishorencb191a740164c
diff --git a/include/num_tree.h b/include/num_tree.h
index 63041c3f..c5e25414 100644
--- a/include/num_tree.h
+++ b/include/num_tree.h
@@ -68,22 +68,23 @@ public:
delete[] val_ids;
}
} else if(comparator == GREATER_THAN || comparator == GREATER_THAN_EQUALS) {
- auto it = int64map.lower_bound(value); // iter values will be >= value
+ // iter entries will be >= value, or end() if all entries are before value
+ auto iter_ge_value = int64map.lower_bound(value);
- if(it == int64map.end()) {
+ if(iter_ge_value == int64map.end()) {
return ;
}
- if(comparator == GREATER_THAN && it->first == value) {
- it++;
+ if(comparator == GREATER_THAN && iter_ge_value->first == value) {
+ iter_ge_value++;
}
std::vector<uint32_t> consolidated_ids;
- while(it != int64map.end()) {
- for(size_t i = 0; i < it->second->getLength(); i++) {
- consolidated_ids.push_back(it->second->at(i));
+ while(iter_ge_value != int64map.end()) {
+ for(size_t i = 0; i < iter_ge_value->second->getLength(); i++) {
+ consolidated_ids.push_back(iter_ge_value->second->at(i));
}
- it++;
+ iter_ge_value++;
}
std::sort(consolidated_ids.begin(), consolidated_ids.end());
@@ -96,39 +97,24 @@ public:
*ids = out;
} else if(comparator == LESS_THAN || comparator == LESS_THAN_EQUALS) {
- auto max_iter = int64map.lower_bound(value); // iter values will be >= value
-
- if(comparator == LESS_THAN) {
- if(max_iter == int64map.end()) {
- max_iter--;
- }
-
- else if(max_iter == int64map.begin() && max_iter->first != value) {
- return ;
- }
-
- else if(max_iter != int64map.begin() && max_iter->first >= value) {
- max_iter--;
- }
- } else {
- if(max_iter == int64map.end()) {
- max_iter--;
- }
- }
+ // iter entries will be >= value, or end() if all entries are before value
+ auto iter_ge_value = int64map.lower_bound(value);
std::vector<uint32_t> consolidated_ids;
auto it = int64map.begin();
- while(true) {
+ while(it != iter_ge_value) {
for(size_t i = 0; i < it->second->getLength(); i++) {
consolidated_ids.push_back(it->second->at(i));
}
+ it++;
+ }
- if(it == max_iter) {
- break;
+ // for LESS_THAN_EQUALS, check if last iter entry is equal to value
+ if(it != int64map.end() && comparator == LESS_THAN_EQUALS && it->first == value) {
+ for(size_t i = 0; i < it->second->getLength(); i++) {
+ consolidated_ids.push_back(it->second->at(i));
}
-
- it++;
}
std::sort(consolidated_ids.begin(), consolidated_ids.end());
diff --git a/test/collection_filtering_test.cpp b/test/collection_filtering_test.cpp
index 0ab29065..427c3cb8 100644
--- a/test/collection_filtering_test.cpp
+++ b/test/collection_filtering_test.cpp
@@ -834,12 +834,111 @@ TEST_F(CollectionFilteringTest, FilteringWithPrefixSearch) {
{}, {}, 0, 10, 1, FREQUENCY, true);
auto results = res_op.get();
- LOG(INFO) << results;
ASSERT_EQ(1, results["found"].get<size_t>());
ASSERT_EQ(1, results["hits"].size());
ASSERT_STREQ("23", results["hits"][0]["document"]["id"].get<std::string>().c_str());
+ collectionManager.drop_collection("coll1");
+}
+
+TEST_F(CollectionFilteringTest, NumericalFilteringWithAnd) {
+ Collection *coll1;
+
+ std::vector<field> fields = {field("company_name", field_types::STRING, false),
+ field("num_employees", field_types::INT32, false),};
+
+ coll1 = collectionManager.get_collection("coll1").get();
+ if(coll1 == nullptr) {
+ coll1 = collectionManager.create_collection("coll1", 1, fields, "num_employees").get();
+ }
+
+ std::vector<std::vector<std::string>> records = {
+ {"123", "Company 1", "50"},
+ {"125", "Company 2", "150"},
+ {"127", "Company 3", "250"},
+ {"129", "Stark Industries 4", "500"},
+ };
+
+ for(size_t i=0; i<records.size(); i++) {
+ nlohmann::json doc;
+
+ doc["id"] = records[i][0];
+ doc["company_name"] = records[i][1];
+ doc["num_employees"] = std::stoi(records[i][2]);
+
+ ASSERT_TRUE(coll1->add(doc.dump()).ok());
+ }
+
+ std::vector<sort_by> sort_fields = { sort_by("num_employees", "ASC") };
+
+ auto results = coll1->search("*",
+ {}, "num_employees:>=100 && num_employees:<=300",
+ {}, sort_fields, 0, 10, 1, FREQUENCY, true).get();
+
+ ASSERT_EQ(2, results["found"].get<size_t>());
+ ASSERT_EQ(2, results["hits"].size());
+
+ ASSERT_STREQ("125", results["hits"][0]["document"]["id"].get<std::string>().c_str());
+ ASSERT_STREQ("127", results["hits"][1]["document"]["id"].get<std::string>().c_str());
+
+ // when filter number is well below all values
+ results = coll1->search("*",
+ {}, "num_employees:>=100 && num_employees:<=10",
+ {}, sort_fields, 0, 10, 1, FREQUENCY, true).get();
+
+ ASSERT_EQ(0, results["found"].get<size_t>());
+
+ // check boundaries
+ results = coll1->search("*",
+ {}, "num_employees:>=150 && num_employees:<=250",
+ {}, sort_fields, 0, 10, 1, FREQUENCY, true).get();
+
+ ASSERT_EQ(2, results["found"].get<size_t>());
+ ASSERT_STREQ("125", results["hits"][0]["document"]["id"].get<std::string>().c_str());
+ ASSERT_STREQ("127", results["hits"][1]["document"]["id"].get<std::string>().c_str());
+
+ results = coll1->search("*",
+ {}, "num_employees:>150 && num_employees:<250",
+ {}, sort_fields, 0, 10, 1, FREQUENCY, true).get();
+
+ ASSERT_EQ(0, results["found"].get<size_t>());
+
+
+ results = coll1->search("*",
+ {}, "num_employees:>50 && num_employees:<250",
+ {}, sort_fields, 0, 10, 1, FREQUENCY, true).get();
+
+ ASSERT_EQ(1, results["found"].get<size_t>());
+ ASSERT_STREQ("125", results["hits"][0]["document"]["id"].get<std::string>().c_str());
+
+ // extreme boundaries
+
+ results = coll1->search("*",
+ {}, "num_employees:>50 && num_employees:<=500",
+ {}, sort_fields, 0, 10, 1, FREQUENCY, true).get();
+
+ ASSERT_EQ(3, results["found"].get<size_t>());
+ ASSERT_STREQ("125", results["hits"][0]["document"]["id"].get<std::string>().c_str());
+ ASSERT_STREQ("127", results["hits"][1]["document"]["id"].get<std::string>().c_str());
+ ASSERT_STREQ("129", results["hits"][2]["document"]["id"].get<std::string>().c_str());
+
+ results = coll1->search("*",
+ {}, "num_employees:>=50 && num_employees:<500",
+ {}, sort_fields, 0, 10, 1, FREQUENCY, true).get();
+
+ ASSERT_EQ(3, results["found"].get<size_t>());
+ ASSERT_STREQ("123", results["hits"][0]["document"]["id"].get<std::string>().c_str());
+ ASSERT_STREQ("125", results["hits"][1]["document"]["id"].get<std::string>().c_str());
+ ASSERT_STREQ("127", results["hits"][2]["document"]["id"].get<std::string>().c_str());
+
+ // no match
+ results = coll1->search("*",
+ {}, "num_employees:>3000 && num_employees:<10",
+ {}, sort_fields, 0, 10, 1, FREQUENCY, true).get();
+
+ ASSERT_EQ(0, results["found"].get<size_t>());
+
collectionManager.drop_collection("coll1");
}
\ No newline at end of file