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 an edge case in exact filtering in array.

Kishore Nallan1ea74b33bff7
diff --git a/src/posting_list.cpp b/src/posting_list.cpp
index 02c3c488..f3966cb5 100644
--- a/src/posting_list.cpp
+++ b/src/posting_list.cpp
@@ -1161,6 +1161,7 @@ void posting_list_t::get_exact_matches(std::vector<iterator_t>& its, const bool
int prev_pos = -1;
bool has_atleast_one_last_token = false;
bool found_matching_index = false;
+ size_t num_matching_index = 0;
while(start_offset_index < end_offset_index) {
int pos = offsets[start_offset_index];
@@ -1184,12 +1185,14 @@ void posting_list_t::get_exact_matches(std::vector<iterator_t>& its, const bool
start_offset_index++; // skip current value which is the array index or flag for last index
prev_pos = -1;
+ found_matching_index = false;
continue;
}
if(pos == (j + 1)) {
// we have found a matching index
found_matching_index = true;
+ num_matching_index++;
}
prev_pos = pos;
@@ -1201,7 +1204,7 @@ void posting_list_t::get_exact_matches(std::vector<iterator_t>& its, const bool
break;
}
- if(!found_matching_index) {
+ if(num_matching_index == 0) {
// not even a single matching index found: can never be an exact match
premature_exit = true;
break;
diff --git a/src/typesense_server_utils.cpp b/src/typesense_server_utils.cpp
index 3cde441d..ac1800ba 100644
--- a/src/typesense_server_utils.cpp
+++ b/src/typesense_server_utils.cpp
@@ -1,3 +1,4 @@
+#include <cstdlib>
#include <curl/curl.h>
#include <gflags/gflags.h>
#include <dlfcn.h>
diff --git a/test/collection_specific_more_test.cpp b/test/collection_specific_more_test.cpp
index abe03519..64ad5be6 100644
--- a/test/collection_specific_more_test.cpp
+++ b/test/collection_specific_more_test.cpp
@@ -578,4 +578,34 @@ TEST_F(CollectionSpecificMoreTest, PositionalTokenRankingWithArray) {
ASSERT_EQ(2, results["hits"].size());
ASSERT_EQ("0", results["hits"][0]["document"]["id"].get<std::string>());
ASSERT_EQ("1", results["hits"][1]["document"]["id"].get<std::string>());
-}
\ No newline at end of file
+}
+
+TEST_F(CollectionSpecificMoreTest, ExactFilteringOnArray) {
+ Collection *coll1;
+ std::vector<field> fields = {field("tags", field_types::STRING_ARRAY, false),
+ field("points", field_types::INT32, false)};
+
+ coll1 = collectionManager.get_collection("coll1").get();
+ if(coll1 == nullptr) {
+ coll1 = collectionManager.create_collection("coll1", 1, fields, "points").get();
+ }
+
+ nlohmann::json doc1;
+ doc1["tags"] = {"§ 23",
+ "§ 34d EStG",
+ "§ 23 Satz EStG"};
+ doc1["points"] = 100;
+
+ coll1->add(doc1.dump());
+
+ auto results = coll1->search("*", {"tags"}, "tags:=§ 23 EStG", {}, {}, {0}, 100, 1, MAX_SCORE, {true},
+ Index::DROP_TOKENS_THRESHOLD,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 5,
+ "", 10, {}, {}, {}, 0,
+ "<mark>", "</mark>", {}, 1000, true, false, true, "", false, 6000 * 1000, 4, 7, true,
+ 4, {off}, 32767, 32767, 2,
+ false, false).get();
+
+ ASSERT_EQ(0, results["hits"].size());
+}