From 4fd8e9388085abb9208b66f520f0e147bf3e5e7c Mon Sep 17 00:00:00 2001 From: Haitham AbuGhaida Date: Thu, 5 Dec 2024 10:43:36 +0100 Subject: [PATCH] fix: remove parentheses and curly braces from FTS5 search queries (#213) Resolves FTS5 syntax errors that occur when searching for terms containing () or {} characters --- bw2data/search/indices.py | 9 ++++++++- tests/search.py | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/bw2data/search/indices.py b/bw2data/search/indices.py index 411a3d7d..62019b54 100644 --- a/bw2data/search/indices.py +++ b/bw2data/search/indices.py @@ -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, diff --git a/tests/search.py b/tests/search.py index 7cb08536..204020e8 100644 --- a/tests/search.py +++ b/tests/search.py @@ -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": "", + } + ]