Skip to content

OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.

Python

fix: prevent partial matches for exact token searches (#2003)

NishadA05dba165880524
diff --git a/src/services/search_service.py b/src/services/search_service.py
index a7760753d..0a49c5e23 100644
--- a/src/services/search_service.py
+++ b/src/services/search_service.py
@@ -1,6 +1,7 @@
import asyncio
import copy
import os
+import re
from collections import Counter
from typing import Any
@@ -23,6 +24,19 @@ EMBED_RETRY_MAX_DELAY = 8.0
_global_search_service = None
+def _is_exact_token_query(query: str) -> bool:
+ """Return True for code/token-like queries that should not allow partial fuzzy matches."""
+ if not query or len(query.strip()) < 3:
+ return False
+
+ query = query.strip()
+
+ if re.search(r"[^a-zA-Z0-9\s]", query):
+ return True
+
+ return bool(re.search(r"[a-zA-Z]", query) and re.search(r"\d", query))
+
+
def register_search_service(service: "SearchService") -> None:
"""
Explicitly register the active search service for the @tool wrapper.
@@ -589,8 +603,17 @@ class SearchService:
)
)
}
- if exact_files:
- chunks = [chunk for chunk in chunks if chunk.get("filename") in exact_files]
+
+ # Determine if we should apply exact-token filtering
+ should_filter_exact = bool(exact_files) or _is_exact_token_query(query)
+
+ if should_filter_exact:
+ if exact_files:
+ # Filter to only chunks from files with exact matches
+ chunks = [chunk for chunk in chunks if chunk.get("filename") in exact_files]
+ else:
+ # No exact matches found for a token-like query - return empty results
+ chunks = []
def _build_terms_agg(field: str) -> dict[str, Any]:
counts = Counter(