Fix: Enhance V3 Search with 'contains', 'startswith', 'endswith' operators and improve error reporting.

This commit is contained in:
Scott Idem
2026-01-02 20:42:19 -05:00
parent f865b1cfb7
commit f5ab2118ad
5 changed files with 56 additions and 11 deletions

View File

@@ -2140,7 +2140,13 @@ def sql_search_qry_part(
"like": "LIKE",
"in": "IN",
"is_null": "IS NULL",
"is_not_null": "IS NOT NULL"
"is_not_null": "IS NOT NULL",
"contains": "LIKE",
"icontains": "LIKE",
"startswith": "LIKE",
"istartswith": "LIKE",
"endswith": "LIKE",
"iendswith": "LIKE"
}
def process_node(query_node, current_depth: int) -> str:
@@ -2188,17 +2194,30 @@ def sql_search_qry_part(
if searchable_fields is not None and f.field not in searchable_fields:
raise HTTPException(status_code=400, detail=f"Searching on field '{f.field}' is not permitted.")
sql_op = operator_map.get(f.op.lower())
op_lower = f.op.lower()
sql_op = operator_map.get(op_lower)
if not sql_op:
raise ValueError(f"Unsupported search operator: {f.op}")
raise HTTPException(status_code=400, detail=f"Unsupported search operator: {f.op}")
filter_data = {}
if f.op.lower() in ['is_null', 'is_not_null']:
if op_lower in ['is_null', 'is_not_null']:
clause = f"`{f.field}` {sql_op}"
elif f.op.lower() == 'in':
elif op_lower == 'in':
p_name = get_param_name()
clause = f"`{f.field}` IN (:{p_name})"
filter_data[p_name] = f.value
elif op_lower in ['contains', 'icontains']:
p_name = get_param_name()
clause = f"`{f.field}` LIKE :{p_name}"
filter_data[p_name] = f"%{f.value}%"
elif op_lower in ['startswith', 'istartswith']:
p_name = get_param_name()
clause = f"`{f.field}` LIKE :{p_name}"
filter_data[p_name] = f"{f.value}%"
elif op_lower in ['endswith', 'iendswith']:
p_name = get_param_name()
clause = f"`{f.field}` LIKE :{p_name}"
filter_data[p_name] = f"%{f.value}"
else:
p_name = get_param_name()
clause = f"`{f.field}` {sql_op} :{p_name}"