mirror of
https://github.com/typesense/typesense.git
synced 2025-05-18 12:42:50 +08:00
If "-" is present in symbols to index, don't use as exclusion op.
This commit is contained in:
parent
be2487b1cc
commit
37fe78f16d
@ -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);
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user