From 37fe78f16dd65170e55902f9ee92c90ce17f31d8 Mon Sep 17 00:00:00 2001 From: Kishore Nallan Date: Tue, 2 Aug 2022 14:32:21 +0530 Subject: [PATCH] If "-" is present in symbols to index, don't use as exclusion op. --- src/collection.cpp | 7 ++++-- test/collection_specific_more_test.cpp | 33 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) 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 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 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()); +}