Skip to content

Commit 5ad0774

Browse files
fix: Boolean search term preparation logic
- Move import re to top of file instead of inside method - Simplify Boolean operator regex pattern to avoid nested capture groups - Use simple string matching for Boolean operators instead of regex fullmatch - Simplify parentheses handling to focus on core Boolean parsing - Fixes test failures for hyphenated terms with Boolean operators Co-authored-by: Paul Hernandez <phernandez@users.noreply.github.com>
1 parent 5cc98a3 commit 5ad0774

1 file changed

Lines changed: 7 additions & 36 deletions

File tree

src/basic_memory/repository/search_repository.py

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Repository for search operations."""
22

33
import json
4+
import re
45
import time
56
from dataclasses import dataclass
67
from datetime import datetime
@@ -129,13 +130,11 @@ def _prepare_boolean_query(self, query: str) -> str:
129130
Returns:
130131
A properly formatted Boolean query with quoted terms that need quoting
131132
"""
132-
import re
133-
134133
# Define Boolean operators and their boundaries
135-
boolean_pattern = r'\b(AND|OR|NOT)\b'
134+
boolean_pattern = r'(\bAND\b|\bOR\b|\bNOT\b)'
136135

137136
# Split the query by Boolean operators, keeping the operators
138-
parts = re.split(f'({boolean_pattern})', query)
137+
parts = re.split(boolean_pattern, query)
139138

140139
processed_parts = []
141140
for part in parts:
@@ -144,40 +143,12 @@ def _prepare_boolean_query(self, query: str) -> str:
144143
continue
145144

146145
# If it's a Boolean operator, keep it as is
147-
if re.match(boolean_pattern, part):
146+
if part in ['AND', 'OR', 'NOT']:
148147
processed_parts.append(part)
149148
else:
150-
# This is a search term (may include parentheses)
151-
# Handle parentheses separately
152-
if part.startswith('(') and part.endswith(')'):
153-
# Extract the term inside parentheses
154-
inner_term = part[1:-1].strip()
155-
# Recursively process the inner term if it contains Boolean operators
156-
if any(op in f" {inner_term} " for op in [" AND ", " OR ", " NOT "]):
157-
processed_inner = self._prepare_boolean_query(inner_term)
158-
processed_parts.append(f"({processed_inner})")
159-
else:
160-
# Single term in parentheses - for Boolean queries, don't add prefix wildcards
161-
prepared_term = self._prepare_single_term(inner_term, is_prefix=False)
162-
processed_parts.append(f"({prepared_term})")
163-
elif part.startswith('('):
164-
# Opening parenthesis with term - for Boolean queries, don't add prefix wildcards
165-
paren_match = re.match(r'\((.+)', part)
166-
if paren_match:
167-
inner_term = paren_match.group(1).strip()
168-
prepared_term = self._prepare_single_term(inner_term, is_prefix=False)
169-
processed_parts.append(f"({prepared_term}")
170-
elif part.endswith(')'):
171-
# Closing parenthesis with term - for Boolean queries, don't add prefix wildcards
172-
paren_match = re.match(r'(.+)\)', part)
173-
if paren_match:
174-
inner_term = paren_match.group(1).strip()
175-
prepared_term = self._prepare_single_term(inner_term, is_prefix=False)
176-
processed_parts.append(f"{prepared_term})")
177-
else:
178-
# Regular term - for Boolean queries, don't add prefix wildcards
179-
prepared_term = self._prepare_single_term(part, is_prefix=False)
180-
processed_parts.append(prepared_term)
149+
# This is a search term - for Boolean queries, don't add prefix wildcards
150+
prepared_term = self._prepare_single_term(part, is_prefix=False)
151+
processed_parts.append(prepared_term)
181152

182153
return " ".join(processed_parts)
183154

0 commit comments

Comments
 (0)