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 open quotes search query bug

krunal7126aa80244e
diff --git a/include/string_utils.h b/include/string_utils.h
index 74cfc537..11f16967 100644
--- a/include/string_utils.h
+++ b/include/string_utils.h
@@ -335,4 +335,6 @@ struct StringUtils {
static Option<bool> tokenize_filter_query(const std::string& filter_query, std::queue<std::string>& tokens);
static Option<bool> split_include_fields(const std::string& include_fields, std::vector<std::string>& tokens);
+
+ static size_t get_occurence_count(const std::string& str, char symbol);
};
diff --git a/src/collection.cpp b/src/collection.cpp
index abef4b13..62e003f3 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -3257,6 +3257,7 @@ void Collection::parse_search_query(const std::string &query, std::vector<std::s
bool exclude_operator_prior = false;
bool phrase_search_op_prior = false;
std::vector<std::string> phrase;
+ bool is_open_quotes = StringUtils::get_occurence_count(query, '"') & 1;
auto symbols_to_index_has_minus =
std::find(symbols_to_index.begin(), symbols_to_index.end(), '-') != symbols_to_index.end();
@@ -3271,7 +3272,7 @@ void Collection::parse_search_query(const std::string &query, std::vector<std::s
token = token.substr(1);
}
- if(token[0] == '"' && token.size() > 1) {
+ if(!is_open_quotes && token[0] == '"' && token.size() > 1) {
phrase_search_op_prior = true;
token = token.substr(1);
}
diff --git a/src/string_utils.cpp b/src/string_utils.cpp
index a9296cd5..109c5d09 100644
--- a/src/string_utils.cpp
+++ b/src/string_utils.cpp
@@ -594,3 +594,7 @@ size_t StringUtils::split_facet(const std::string &s, std::vector<std::string> &
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> utf8conv;
return utf8conv.from_bytes(bytes).size();
}*/
+
+size_t StringUtils::get_occurence_count(const std::string &str, char symbol) {
+ return std::count(str.begin(), str.end(), symbol);
+}
\ No newline at end of file
diff --git a/test/collection_specific_more_test.cpp b/test/collection_specific_more_test.cpp
index 51b5a58c..05361729 100644
--- a/test/collection_specific_more_test.cpp
+++ b/test/collection_specific_more_test.cpp
@@ -2179,6 +2179,10 @@ TEST_F(CollectionSpecificMoreTest, PhraseMatchAcrossArrayElements) {
auto res = coll1->search(R"("state of the art)", {"texts"}, "", {}, {}, {0}, 10, 1,
FREQUENCY, {true}, 10, spp::sparse_hash_set<std::string>()).get();
+ ASSERT_EQ(1, res["hits"].size());
+
+ res = coll1->search(R"("state of the art")", {"texts"}, "", {}, {}, {0}, 10, 1,
+ FREQUENCY, {true}, 10, spp::sparse_hash_set<std::string>()).get();
ASSERT_EQ(0, res["hits"].size());
}
diff --git a/test/collection_specific_test.cpp b/test/collection_specific_test.cpp
index 38764660..388eb883 100644
--- a/test/collection_specific_test.cpp
+++ b/test/collection_specific_test.cpp
@@ -2996,3 +2996,59 @@ TEST_F(CollectionSpecificTest, DontHighlightPunctuation) {
collectionManager.drop_collection("coll1");
}
+
+TEST_F(CollectionSpecificTest, ExactMatchWithoutClosingSymbol) {
+ std::vector<field> fields = {field("title", field_types::STRING, false),};
+
+ Collection* coll1 = collectionManager.create_collection("coll1", 1, fields).get();
+
+ std::vector<std::vector<std::string>> records = {
+ {"Hampi"},
+ {"Mahabalipuram"},
+ {"Taj Mahal"},
+ {"Mysore Palace"}
+ };
+
+ for(size_t i=0; i<records.size(); i++) {
+ nlohmann::json doc;
+
+ doc["id"] = std::to_string(i);
+ doc["title"] = records[i][0];
+
+ ASSERT_TRUE(coll1->add(doc.dump()).ok());
+ }
+
+ std::map<std::string, std::string> req_params = {
+ {"collection", "coll1"},
+ {"q", "\"Hamp"},
+ {"query_by", "title"},
+ };
+ nlohmann::json embedded_params;
+ std::string json_res;
+ auto now_ts = std::chrono::duration_cast<std::chrono::microseconds>(
+ std::chrono::system_clock::now().time_since_epoch()).count();
+
+ auto search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);
+
+ nlohmann::json result = nlohmann::json::parse(json_res);
+ ASSERT_EQ(1, result["hits"].size());
+ ASSERT_EQ("0", result["hits"][0]["document"]["id"]);
+ ASSERT_EQ("Hampi", result["hits"][0]["document"]["title"]);
+
+ req_params = {
+ {"collection", "coll1"},
+ {"q", "\"Mah"},
+ {"query_by", "title"},
+ };
+ now_ts = std::chrono::duration_cast<std::chrono::microseconds>(
+ std::chrono::system_clock::now().time_since_epoch()).count();
+
+ search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);
+
+ result = nlohmann::json::parse(json_res);
+ ASSERT_EQ(2, result["hits"].size());
+ ASSERT_EQ("2", result["hits"][0]["document"]["id"]);
+ ASSERT_EQ("Taj Mahal", result["hits"][0]["document"]["title"]);
+ ASSERT_EQ("1", result["hits"][1]["document"]["id"]);
+ ASSERT_EQ("Mahabalipuram", result["hits"][1]["document"]["title"]);
+}
\ No newline at end of file