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 queries with hyphens not matching indexed text.

Kishore Nallan3efe062d6349
diff --git a/src/collection.cpp b/src/collection.cpp
index da1c59da..a69f6ce4 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -1310,6 +1310,10 @@ void Collection::parse_search_query(const std::string &query, std::vector<std::s
if(token[0] == '-') {
exclude_operator_prior = true;
token = token.substr(1);
+ } else {
+ // remove hyphens (added earlier for supporting negation syntax)
+ // to maintain parity with indexing which currently removing special symbols
+ token.erase(std::remove(token.begin(), token.end(), '-'), token.end());
}
if(exclude_operator_prior) {
diff --git a/test/collection_specific_test.cpp b/test/collection_specific_test.cpp
new file mode 100644
index 00000000..fc9b3706
--- /dev/null
+++ b/test/collection_specific_test.cpp
@@ -0,0 +1,58 @@
+#include <gtest/gtest.h>
+#include <string>
+#include <vector>
+#include <fstream>
+#include <algorithm>
+#include <collection_manager.h>
+#include "collection.h"
+
+class CollectionSpecificTest : public ::testing::Test {
+protected:
+ Store *store;
+ CollectionManager & collectionManager = CollectionManager::get_instance();
+
+ std::vector<std::string> query_fields;
+ std::vector<sort_by> sort_fields;
+
+ void setupCollection() {
+ std::string state_dir_path = "/tmp/typesense_test/collection_filtering";
+ LOG(INFO) << "Truncating and creating: " << state_dir_path;
+ system(("rm -rf "+state_dir_path+" && mkdir -p "+state_dir_path).c_str());
+
+ store = new Store(state_dir_path);
+ collectionManager.init(store, 1.0, "auth_key");
+ collectionManager.load(8, 1000);
+ }
+
+ virtual void SetUp() {
+ setupCollection();
+ }
+
+ virtual void TearDown() {
+ collectionManager.dispose();
+ delete store;
+ }
+};
+
+TEST_F(CollectionSpecificTest, SearchTextWithHyphen) {
+ 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 doc;
+ doc["id"] = "0";
+ doc["title"] = "open-access-may-become-mandatory-for-nih-funded-research";
+ doc["points"] = 100;
+
+ ASSERT_TRUE(coll1->add(doc.dump()).ok());
+
+ auto results = coll1->search("open-access-may-become-mandatory-for-nih-funded-research",
+ {"title"}, "", {}, {}, {0}, 3, 1, FREQUENCY, {true}, 5).get();
+
+ ASSERT_EQ(1, results["found"].get<size_t>());
+ ASSERT_EQ(1, results["hits"].size());
+
+ ASSERT_STREQ("0", results["hits"][0]["document"]["id"].get<std::string>().c_str());
+ collectionManager.drop_collection("coll1");
+}
\ No newline at end of file