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

This commit is contained in:
Kishore Nallan 2022-08-02 14:32:21 +05:30
parent be2487b1cc
commit 37fe78f16d
2 changed files with 38 additions and 2 deletions

View File

@ -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);
}

View File

@ -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());
}