Skip to content

Commit

Permalink
fix: remove parentheses and curly braces from FTS5 search queries (#213)
Browse files Browse the repository at this point in the history
Resolves FTS5 syntax errors that occur when searching for terms
containing () or {} characters
  • Loading branch information
Haitham-ghaida authored Dec 5, 2024
1 parent f68d01e commit 4fd8e93
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
9 changes: 8 additions & 1 deletion bw2data/search/indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,14 @@ def search(self, string, limit=None, weights=None, mask=None, filter=None):
if string == "*":
query = BW2Schema
else:
query = BW2Schema.search_bm25(string.replace(",", ""), weights=weights)
query = BW2Schema.search_bm25(
string.replace(",", "")
.replace("(", "")
.replace(")", "")
.replace("{", "")
.replace("}", ""),
weights=weights,
)
return list(
query.select(
BW2Schema.name,
Expand Down
20 changes: 20 additions & 0 deletions tests/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,23 @@ def test_search_single_char():
"synonyms": "",
}
]


@bw2test
def test_search_with_parentheses():
"""Test that searching with parentheses works correctly"""
im = IndexManager("foo")
im.add_dataset({"database": "foo", "code": "bar", "name": "beam dried (u=10%) planed"})
with Searcher("foo") as s:
assert s.search("dried (u=10%)", proxy=False) == [
{
"comment": "",
"product": "",
"name": "beam dried (u=10%) planed",
"database": "foo",
"location": "",
"code": "bar",
"categories": "",
"synonyms": "",
}
]

0 comments on commit 4fd8e93

Please sign in to comment.