import re from typing import List FILLER_PHRASES = [ r"\btickets?\s+related\s+to\s+", r"\btickets?\s+about\s+", r"\btickets?\s+for\s+", r"\btickets?\s+regarding\s+", r"\bfind\s+(me\s+)?tickets?\s*(for|about|related\s+to|regarding)?\b", r"\bshow\s+(me\s+)?tickets?\s*(for|about|related\s+to|regarding)?\b", r"\blist\s+tickets?\s*(for|about|related\s+to|regarding)?\b", r"\bsearch\s+(for\s+)?tickets?\b", r"\bhow\s+(do\s+i|to|can\s+i)\s+", r"\bwhat\s+(is|are)\s+", r"\b(is\s+there|are\s+there)\s+any\b", r"\brelated\s+to\b", r"\brelated\s+tickets?\b", r"\btickets?\b", r"\bshow\s+me\b", r"\bfind\b", ] def refine_search_query(q: str) -> str: """ Cleans natural language queries to extract core keywords. Removes common filler phrases like 'tickets related to' or 'how to find'. """ if not q: return "" # Preserve original for short or specific queries (e.g. "Ticket #123") if len(q.split()) <= 1 or re.search(r'#\d+', q): return q.strip() clean_q = q.lower().strip() # 1. Strip common NL filler phrases for phrase in FILLER_PHRASES: # We replace with a space to avoid joining words accidentally clean_q = re.sub(phrase, " ", clean_q, flags=re.IGNORECASE) # 2. Normalize whitespace clean_q = re.sub(r'\s+', ' ', clean_q).strip() # Fallback to original if we stripped everything or query became too short if not clean_q or len(clean_q) < 2: return q.strip() return clean_q def get_relaxed_tsquery(q: str) -> str: """ Converts a space-separated query into a Postgres OR-based tsquery. Example: 'dashboard issue' -> 'dashboard | issue' """ # Filter out very short words from the relaxed query words = [w for w in re.split(r'[^\w]+', q) if len(w) > 2] if not words: return q return " | ".join(words)