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 exact matching regression.

Kishore Nallan13d4c14889ed
diff --git a/src/index.cpp b/src/index.cpp
index 9e05a46e..9d2af0f4 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -638,8 +638,8 @@ void Index::index_field_in_memory(const field& afield, std::vector<index_record>
art_tree *t = tree_it->second;
for(auto& token_to_doc: token_to_doc_offsets) {
- std::vector<art_document>& documents = token_to_doc.second;
const std::string& token = token_to_doc.first;
+ std::vector<art_document>& documents = token_to_doc.second;
const auto *key = (const unsigned char *) token.c_str();
int key_len = (int) token.length() + 1; // for the terminating \0 char
@@ -1483,9 +1483,8 @@ void Index::do_filtering(uint32_t*& filter_ids, uint32_t& filter_ids_length,
strt_ids_size = result_id_vec.size();
}
- if((a_filter.comparators[0] == EQUALS || a_filter.comparators[0] == NOT_EQUALS) && f.is_facet()) {
- // need to do exact match (unlike CONTAINS) by using the facet index
- // field being a facet is already enforced upstream
+ if(a_filter.comparators[0] == EQUALS || a_filter.comparators[0] == NOT_EQUALS) {
+ // need to do exact match (unlike CONTAINS)
uint32_t* exact_strt_ids = new uint32_t[strt_ids_size];
size_t exact_strt_size = 0;
@@ -1497,7 +1496,7 @@ void Index::do_filtering(uint32_t*& filter_ids, uint32_t& filter_ids_length,
strt_ids_size = exact_strt_size;
}
- if(a_filter.comparators[0] == NOT_EQUALS && f.is_facet()) {
+ if(a_filter.comparators[0] == NOT_EQUALS) {
// exclude records from existing IDs (from previous filters or ALL records)
// upstream will guarantee that NOT_EQUALS is placed right at the end of filters list
if(ids == nullptr) {
diff --git a/src/posting_list.cpp b/src/posting_list.cpp
index 83787abd..526848ec 100644
--- a/src/posting_list.cpp
+++ b/src/posting_list.cpp
@@ -987,6 +987,7 @@ void posting_list_t::get_exact_matches(std::vector<iterator_t>& its, const bool
if(its.size() == 1) {
for(size_t i = 0; i < num_ids; i++) {
uint32_t id = ids[i];
+ its[0].skip_to(id);
if(is_single_token_verbatim_match(its[0], field_is_array)) {
exact_ids[exact_id_index++] = id;
}
diff --git a/test/collection_filtering_test.cpp b/test/collection_filtering_test.cpp
index 02866b76..216fb3ec 100644
--- a/test/collection_filtering_test.cpp
+++ b/test/collection_filtering_test.cpp
@@ -1963,8 +1963,54 @@ TEST_F(CollectionFilteringTest, FilteringWithTokenSeparators) {
collectionManager.drop_collection("coll2");
}
+TEST_F(CollectionFilteringTest, ExactFilteringSingleQueryTerm) {
+ std::vector<field> fields = {field("name", field_types::STRING, false),
+ field("tags", field_types::STRING_ARRAY, false)};
+
+ Collection* coll1 = collectionManager.create_collection(
+ "coll1", 1, fields, "", 0, "", {}, {"."}
+ ).get();
+
+ nlohmann::json doc1;
+ doc1["id"] = "0";
+ doc1["name"] = "AT&T GoPhone";
+ doc1["tags"] = {"AT&T GoPhone"};
+
+ nlohmann::json doc2;
+ doc2["id"] = "1";
+ doc2["name"] = "AT&T";
+ doc2["tags"] = {"AT&T"};
+
+ ASSERT_TRUE(coll1->add(doc1.dump()).ok());
+ ASSERT_TRUE(coll1->add(doc2.dump()).ok());
+
+ auto results = coll1->search("*", {},"name:=AT&T", {}, {}, {0}, 10,
+ 1, FREQUENCY, {false}).get();
+ ASSERT_EQ(1, results["hits"].size());
+ ASSERT_EQ("1", results["hits"][0]["document"]["id"].get<std::string>());
+
+ results = coll1->search("*", {},"tags:=AT&T", {}, {}, {0}, 10,
+ 1, FREQUENCY, {false}).get();
+ ASSERT_EQ(1, results["hits"].size());
+ ASSERT_EQ("1", results["hits"][0]["document"]["id"].get<std::string>());
+
+ nlohmann::json doc3;
+ doc3["id"] = "2";
+ doc3["name"] = "Phone";
+ doc3["tags"] = {"Samsung Phone", "Phone"};
+
+ ASSERT_TRUE(coll1->add(doc3.dump()).ok());
+
+ results = coll1->search("*", {},"tags:=Phone", {}, {}, {0}, 10,
+ 1, FREQUENCY, {false}).get();
+ ASSERT_EQ(1, results["hits"].size());
+ ASSERT_EQ("2", results["hits"][0]["document"]["id"].get<std::string>());
+
+ collectionManager.drop_collection("coll1");
+}
+
TEST_F(CollectionFilteringTest, ExactFilteringRepeatingTokensSingularField) {
- std::vector<field> fields = {field("name", field_types::STRING, true)};
+ std::vector<field> fields = {field("name", field_types::STRING, false)};
Collection* coll1 = collectionManager.create_collection(
"coll1", 1, fields, "", 0, "", {}, {"."}
@@ -2013,7 +2059,7 @@ TEST_F(CollectionFilteringTest, ExactFilteringRepeatingTokensSingularField) {
}
TEST_F(CollectionFilteringTest, ExactFilteringRepeatingTokensArrayField) {
- std::vector<field> fields = {field("name", field_types::STRING_ARRAY, true)};
+ std::vector<field> fields = {field("name", field_types::STRING_ARRAY, false)};
Collection* coll1 = collectionManager.create_collection(
"coll1", 1, fields, "", 0, "", {}, {"."}