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

If "-" is present in symbols to index, don't use as exclusion op.

Kishore Nallan37fe78f16dd6
diff --git a/src/collection.cpp b/src/collection.cpp
index 93f72334..4cc94ae7 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -1784,12 +1784,15 @@ void Collection::parse_search_query(const std::string &query, std::vector<std::s
bool phrase_search_op_prior = false;
std::vector<std::string> phrase;
+ auto symbols_to_index_has_minus =
+ std::find(symbols_to_index.begin(), symbols_to_index.end(), '-') != symbols_to_index.end();
+
for(auto& token: tokens) {
bool end_of_phrase = false;
- if(token == "-") {
+ if(token == "-" && !symbols_to_index_has_minus) {
continue;
- } else if(token[0] == '-') {
+ } else if(token[0] == '-' && !symbols_to_index_has_minus) {
exclude_operator_prior = true;
token = token.substr(1);
}
diff --git a/test/collection_specific_more_test.cpp b/test/collection_specific_more_test.cpp
index 2b7d067e..eaa91a2d 100644
--- a/test/collection_specific_more_test.cpp
+++ b/test/collection_specific_more_test.cpp
@@ -958,3 +958,36 @@ TEST_F(CollectionSpecificMoreTest, FieldWeightNormalization) {
collectionManager.drop_collection("coll1");
}
+
+TEST_F(CollectionSpecificMoreTest, SearchingForMinusCharacter) {
+ // when the minus character is part of symbols_to_index it should not be used as exclusion operator
+ std::vector<field> fields = {field("name", field_types::STRING, false),
+ field("points", field_types::INT32, false),};
+
+ Collection* coll1 = collectionManager.create_collection(
+ "coll1", 1, fields, "points", 0, "", {"-"}, {}
+ ).get();
+
+ nlohmann::json doc1;
+ doc1["name"] = "y = -x + 3 + 2 * x";
+ doc1["points"] = 100;
+
+ ASSERT_TRUE(coll1->add(doc1.dump()).ok());
+
+ doc1["name"] = "foo bar";
+ ASSERT_TRUE(coll1->add(doc1.dump()).ok());
+
+ auto results = coll1->search("-x + 3", {"name"},
+ "", {}, {}, {0}, 10,
+ 1, FREQUENCY, {true},
+ 0).get();
+
+ ASSERT_EQ(1, results["hits"].size());
+
+ results = coll1->search("-", {"name"},
+ "", {}, {}, {0}, 10,
+ 1, FREQUENCY, {true},
+ 0).get();
+
+ ASSERT_EQ(1, results["hits"].size());
+}