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

Handle single hyphen token in query.

Kishore Nallanf1b4b8c2a23f
diff --git a/src/collection.cpp b/src/collection.cpp
index 627454a8..a3c51725 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -1343,7 +1343,9 @@ void Collection::parse_search_query(const std::string &query, std::vector<std::s
bool exclude_operator_prior = false;
for(auto& token: tokens) {
- if(token[0] == '-') {
+ if(token == "-") {
+ continue;
+ } else if(token[0] == '-') {
exclude_operator_prior = true;
token = token.substr(1);
}
diff --git a/test/collection_specific_test.cpp b/test/collection_specific_test.cpp
index 6ea594c4..8063fa4d 100644
--- a/test/collection_specific_test.cpp
+++ b/test/collection_specific_test.cpp
@@ -1960,3 +1960,27 @@ TEST_F(CollectionSpecificTest, DroppedTokensShouldNotBeUsedForPrefixSearch) {
collectionManager.drop_collection("coll1");
}
+
+TEST_F(CollectionSpecificTest, SingleHyphenInQueryNotToBeTreatedAsExclusion) {
+ std::vector<field> fields = {field("title", field_types::STRING, false),
+ field("points", field_types::INT32, false),};
+
+ Collection* coll1 = collectionManager.create_collection("coll1", 1, fields, "points").get();
+
+ nlohmann::json doc1;
+ doc1["id"] = "0";
+ doc1["title"] = "Saturday Short - Thrive (with Audio Descriptions + Open Captions)";
+ doc1["points"] = 100;
+
+ ASSERT_TRUE(coll1->add(doc1.dump()).ok());
+
+ auto results = coll1->search("Saturday Short - Thrive (with Audio Descriptions + Open Captions)", {"title"},
+ "", {}, {}, {2}, 10,
+ 1, FREQUENCY, {true},
+ 10, spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "title", 20, {}, {}, {}, 0,
+ "<mark>", "</mark>", {}, 1000, true).get();
+
+ ASSERT_EQ(1, results["hits"].size());
+ collectionManager.drop_collection("coll1");
+}