orchestrator-core 4.5.1a1__py3-none-any.whl → 4.5.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. orchestrator/__init__.py +3 -12
  2. orchestrator/agentic_app.py +48 -29
  3. orchestrator/api/api_v1/api.py +8 -6
  4. orchestrator/api/api_v1/endpoints/processes.py +2 -0
  5. orchestrator/api/api_v1/endpoints/search.py +26 -7
  6. orchestrator/cli/main.py +2 -2
  7. orchestrator/cli/search/__init__.py +32 -0
  8. orchestrator/devtools/populator.py +16 -0
  9. orchestrator/domain/base.py +2 -7
  10. orchestrator/domain/lifecycle.py +24 -7
  11. orchestrator/llm_settings.py +9 -3
  12. orchestrator/log_config.py +1 -0
  13. orchestrator/migrations/helpers.py +7 -1
  14. orchestrator/schemas/search.py +13 -0
  15. orchestrator/schemas/workflow.py +1 -0
  16. orchestrator/search/agent/__init__.py +15 -2
  17. orchestrator/search/agent/agent.py +30 -15
  18. orchestrator/search/agent/prompts.py +75 -37
  19. orchestrator/search/agent/state.py +13 -0
  20. orchestrator/search/agent/tools.py +148 -11
  21. orchestrator/search/core/__init__.py +12 -0
  22. orchestrator/search/core/embedding.py +13 -4
  23. orchestrator/search/core/exceptions.py +14 -0
  24. orchestrator/search/core/types.py +15 -0
  25. orchestrator/search/core/validators.py +13 -0
  26. orchestrator/search/docs/running_local_text_embedding_inference.md +1 -0
  27. orchestrator/search/filters/__init__.py +13 -0
  28. orchestrator/search/filters/base.py +84 -61
  29. orchestrator/search/filters/date_filters.py +13 -0
  30. orchestrator/search/filters/definitions.py +16 -2
  31. orchestrator/search/filters/ltree_filters.py +16 -3
  32. orchestrator/search/filters/numeric_filter.py +13 -0
  33. orchestrator/search/indexing/__init__.py +13 -0
  34. orchestrator/search/indexing/indexer.py +14 -3
  35. orchestrator/search/indexing/registry.py +13 -0
  36. orchestrator/search/indexing/tasks.py +17 -1
  37. orchestrator/search/indexing/traverse.py +17 -5
  38. orchestrator/search/llm_migration.py +108 -0
  39. orchestrator/search/retrieval/__init__.py +13 -0
  40. orchestrator/search/retrieval/builder.py +23 -8
  41. orchestrator/search/retrieval/engine.py +36 -34
  42. orchestrator/search/retrieval/exceptions.py +90 -0
  43. orchestrator/search/retrieval/pagination.py +13 -0
  44. orchestrator/search/retrieval/retrievers/__init__.py +26 -0
  45. orchestrator/search/retrieval/retrievers/base.py +123 -0
  46. orchestrator/search/retrieval/retrievers/fuzzy.py +94 -0
  47. orchestrator/search/retrieval/retrievers/hybrid.py +277 -0
  48. orchestrator/search/retrieval/retrievers/semantic.py +94 -0
  49. orchestrator/search/retrieval/retrievers/structured.py +39 -0
  50. orchestrator/search/retrieval/utils.py +21 -7
  51. orchestrator/search/retrieval/validation.py +54 -76
  52. orchestrator/search/schemas/__init__.py +12 -0
  53. orchestrator/search/schemas/parameters.py +13 -0
  54. orchestrator/search/schemas/results.py +15 -1
  55. orchestrator/services/processes.py +2 -1
  56. orchestrator/settings.py +7 -0
  57. orchestrator/utils/state.py +6 -1
  58. orchestrator/workflows/steps.py +16 -1
  59. orchestrator/workflows/tasks/validate_product_type.py +3 -3
  60. {orchestrator_core-4.5.1a1.dist-info → orchestrator_core-4.5.3.dist-info}/METADATA +14 -12
  61. {orchestrator_core-4.5.1a1.dist-info → orchestrator_core-4.5.3.dist-info}/RECORD +67 -60
  62. orchestrator/migrations/versions/schema/2025-08-12_52b37b5b2714_search_index_model_for_llm_integration.py +0 -95
  63. orchestrator/search/retrieval/retriever.py +0 -447
  64. /orchestrator/cli/{index_llm.py → search/index_llm.py} +0 -0
  65. /orchestrator/cli/{resize_embedding.py → search/resize_embedding.py} +0 -0
  66. /orchestrator/cli/{search_explore.py → search/search_explore.py} +0 -0
  67. /orchestrator/cli/{speedtest.py → search/speedtest.py} +0 -0
  68. {orchestrator_core-4.5.1a1.dist-info → orchestrator_core-4.5.3.dist-info}/WHEEL +0 -0
  69. {orchestrator_core-4.5.1a1.dist-info → orchestrator_core-4.5.3.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,277 @@
1
+ # Copyright 2019-2025 SURF, GÉANT.
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
13
+
14
+ from typing import TypedDict
15
+
16
+ from sqlalchemy import BindParameter, Select, and_, bindparam, case, cast, func, literal, or_, select
17
+ from sqlalchemy.sql.expression import ColumnElement, Label
18
+ from sqlalchemy.types import TypeEngine
19
+
20
+ from orchestrator.db.models import AiSearchIndex
21
+ from orchestrator.search.core.types import SearchMetadata
22
+
23
+ from ..pagination import PaginationParams
24
+ from .base import Retriever
25
+
26
+
27
+ class RrfScoreSqlComponents(TypedDict):
28
+ """SQL expression components of the RRF hybrid score calculation."""
29
+
30
+ rrf_num: ColumnElement
31
+ perfect: Label
32
+ beta: ColumnElement
33
+ rrf_max: ColumnElement
34
+ fused_num: ColumnElement
35
+ normalized_score: ColumnElement
36
+
37
+
38
+ def compute_rrf_hybrid_score_sql(
39
+ sem_rank_col: ColumnElement,
40
+ fuzzy_rank_col: ColumnElement,
41
+ avg_fuzzy_score_col: ColumnElement,
42
+ k: int,
43
+ perfect_threshold: float,
44
+ n_sources: int = 2,
45
+ margin_factor: float = 0.05,
46
+ score_numeric_type: TypeEngine | None = None,
47
+ ) -> RrfScoreSqlComponents:
48
+ """Compute RRF (Reciprocal Rank Fusion) hybrid score as SQL expressions for database execution.
49
+
50
+ This function implements the core scoring logic for hybrid search combining semantic
51
+ and fuzzy ranking. It computes:
52
+ 1. Base RRF score from both ranks
53
+ 2. Perfect match detection and boosting
54
+ 3. Dynamic beta parameter based on k and n_sources
55
+ 4. Normalized final score in [0, 1] range
56
+
57
+ Args:
58
+ sem_rank_col: SQLAlchemy column expression for semantic rank
59
+ fuzzy_rank_col: SQLAlchemy column expression for fuzzy rank
60
+ avg_fuzzy_score_col: SQLAlchemy column expression for average fuzzy score
61
+ k: RRF constant controlling rank influence (typically 60)
62
+ perfect_threshold: Threshold for perfect match boost (typically 0.9)
63
+ n_sources: Number of ranking sources being fused (default: 2 for semantic + fuzzy)
64
+ margin_factor: Margin above rrf_max as fraction (default: 0.05 = 5%)
65
+ score_numeric_type: SQLAlchemy numeric type for casting scores
66
+
67
+ Returns:
68
+ RrfScoreSqlComponents: Dictionary of SQL expressions for score components
69
+ - rrf_num: Raw RRF score (cast to numeric type if provided)
70
+ - perfect: Perfect match flag (1 if avg_fuzzy_score >= threshold, else 0)
71
+ - beta: Boost amount for perfect matches
72
+ - rrf_max: Maximum possible RRF score
73
+ - fused_num: RRF + perfect boost
74
+ - normalized_score: Final score normalized to [0, 1]
75
+
76
+ Note:
77
+ - Keep margin_factor small to avoid compressing perfects near 1 after normalization.
78
+
79
+ - The `beta` boost is calculated to be greater than the maximum possible standard
80
+ RRF score (`rrf_max`). This guarantees that any item flagged as a "perfect" match
81
+ will always rank above any non-perfect match.
82
+
83
+ - This function assumes that rank columns do not
84
+ contain `NULL` values. A `NULL` in any rank column will result in a `NULL` final score
85
+ for that item.
86
+ """
87
+ # RRF (rank-based): sum of 1/(k + rank_i) for each ranking source
88
+ rrf_raw = (1.0 / (k + sem_rank_col)) + (1.0 / (k + fuzzy_rank_col))
89
+ rrf_num = cast(rrf_raw, score_numeric_type) if score_numeric_type else rrf_raw
90
+
91
+ # Perfect flag to boost near perfect fuzzy matches
92
+ perfect = case((avg_fuzzy_score_col >= perfect_threshold, 1), else_=0).label("perfect_match")
93
+
94
+ # Dynamic beta based on k and number of sources
95
+ # rrf_max = n_sources / (k + 1)
96
+ k_num = literal(float(k), type_=score_numeric_type) if score_numeric_type else literal(float(k))
97
+ n_sources_lit = (
98
+ literal(float(n_sources), type_=score_numeric_type) if score_numeric_type else literal(float(n_sources))
99
+ )
100
+ rrf_max = n_sources_lit / (k_num + literal(1.0, type_=score_numeric_type if score_numeric_type else None))
101
+
102
+ margin = rrf_max * literal(margin_factor, type_=score_numeric_type if score_numeric_type else None)
103
+ beta = rrf_max + margin
104
+
105
+ # Fused score: RRF + perfect match boost
106
+ perfect_casted = cast(perfect, score_numeric_type) if score_numeric_type else perfect
107
+ fused_num = rrf_num + beta * perfect_casted
108
+
109
+ # Normalize to [0,1] via the theoretical max (beta + rrf_max)
110
+ norm_den = beta + rrf_max
111
+ normalized_score = fused_num / norm_den
112
+
113
+ return RrfScoreSqlComponents(
114
+ rrf_num=rrf_num,
115
+ perfect=perfect,
116
+ beta=beta,
117
+ rrf_max=rrf_max,
118
+ fused_num=fused_num,
119
+ normalized_score=normalized_score,
120
+ )
121
+
122
+
123
+ class RrfHybridRetriever(Retriever):
124
+ """Reciprocal Rank Fusion of semantic and fuzzy ranking with parent-child retrieval."""
125
+
126
+ def __init__(
127
+ self,
128
+ q_vec: list[float],
129
+ fuzzy_term: str,
130
+ pagination_params: PaginationParams,
131
+ k: int = 60,
132
+ field_candidates_limit: int = 100,
133
+ ) -> None:
134
+ self.q_vec = q_vec
135
+ self.fuzzy_term = fuzzy_term
136
+ self.page_after_score = pagination_params.page_after_score
137
+ self.page_after_id = pagination_params.page_after_id
138
+ self.k = k
139
+ self.field_candidates_limit = field_candidates_limit
140
+
141
+ def apply(self, candidate_query: Select) -> Select:
142
+ cand = candidate_query.subquery()
143
+ q_param: BindParameter[list[float]] = bindparam("q_vec", self.q_vec, type_=AiSearchIndex.embedding.type)
144
+
145
+ best_similarity = func.word_similarity(self.fuzzy_term, AiSearchIndex.value)
146
+ sem_expr = case(
147
+ (AiSearchIndex.embedding.is_(None), None),
148
+ else_=AiSearchIndex.embedding.op("<->")(q_param),
149
+ )
150
+ sem_val = func.coalesce(sem_expr, literal(1.0)).label("semantic_distance")
151
+
152
+ filter_condition = literal(self.fuzzy_term).op("<%")(AiSearchIndex.value)
153
+
154
+ field_candidates = (
155
+ select(
156
+ AiSearchIndex.entity_id,
157
+ AiSearchIndex.path,
158
+ AiSearchIndex.value,
159
+ sem_val,
160
+ best_similarity.label("fuzzy_score"),
161
+ )
162
+ .select_from(AiSearchIndex)
163
+ .join(cand, cand.c.entity_id == AiSearchIndex.entity_id)
164
+ .where(
165
+ and_(
166
+ AiSearchIndex.value_type.in_(self.SEARCHABLE_FIELD_TYPES),
167
+ filter_condition,
168
+ )
169
+ )
170
+ .order_by(
171
+ best_similarity.desc().nulls_last(),
172
+ sem_expr.asc().nulls_last(),
173
+ AiSearchIndex.entity_id.asc(),
174
+ )
175
+ .limit(self.field_candidates_limit)
176
+ ).cte("field_candidates")
177
+
178
+ entity_scores = (
179
+ select(
180
+ field_candidates.c.entity_id,
181
+ func.avg(field_candidates.c.semantic_distance).label("avg_semantic_distance"),
182
+ func.avg(field_candidates.c.fuzzy_score).label("avg_fuzzy_score"),
183
+ ).group_by(field_candidates.c.entity_id)
184
+ ).cte("entity_scores")
185
+
186
+ entity_highlights = (
187
+ select(
188
+ field_candidates.c.entity_id,
189
+ func.first_value(field_candidates.c.value)
190
+ .over(
191
+ partition_by=field_candidates.c.entity_id,
192
+ order_by=[field_candidates.c.fuzzy_score.desc(), field_candidates.c.path.asc()],
193
+ )
194
+ .label(self.HIGHLIGHT_TEXT_LABEL),
195
+ func.first_value(field_candidates.c.path)
196
+ .over(
197
+ partition_by=field_candidates.c.entity_id,
198
+ order_by=[field_candidates.c.fuzzy_score.desc(), field_candidates.c.path.asc()],
199
+ )
200
+ .label(self.HIGHLIGHT_PATH_LABEL),
201
+ ).distinct(field_candidates.c.entity_id)
202
+ ).cte("entity_highlights")
203
+
204
+ ranked = (
205
+ select(
206
+ entity_scores.c.entity_id,
207
+ entity_scores.c.avg_semantic_distance,
208
+ entity_scores.c.avg_fuzzy_score,
209
+ entity_highlights.c.highlight_text,
210
+ entity_highlights.c.highlight_path,
211
+ func.dense_rank()
212
+ .over(
213
+ order_by=[entity_scores.c.avg_semantic_distance.asc().nulls_last(), entity_scores.c.entity_id.asc()]
214
+ )
215
+ .label("sem_rank"),
216
+ func.dense_rank()
217
+ .over(order_by=[entity_scores.c.avg_fuzzy_score.desc().nulls_last(), entity_scores.c.entity_id.asc()])
218
+ .label("fuzzy_rank"),
219
+ ).select_from(
220
+ entity_scores.join(entity_highlights, entity_scores.c.entity_id == entity_highlights.c.entity_id)
221
+ )
222
+ ).cte("ranked_results")
223
+
224
+ # Compute RRF hybrid score
225
+ score_components = compute_rrf_hybrid_score_sql(
226
+ sem_rank_col=ranked.c.sem_rank,
227
+ fuzzy_rank_col=ranked.c.fuzzy_rank,
228
+ avg_fuzzy_score_col=ranked.c.avg_fuzzy_score,
229
+ k=self.k,
230
+ perfect_threshold=0.9,
231
+ score_numeric_type=self.SCORE_NUMERIC_TYPE,
232
+ )
233
+
234
+ perfect = score_components["perfect"]
235
+ normalized_score = score_components["normalized_score"]
236
+
237
+ # Round to configured precision
238
+ score = cast(
239
+ func.round(cast(normalized_score, self.SCORE_NUMERIC_TYPE), self.SCORE_PRECISION),
240
+ self.SCORE_NUMERIC_TYPE,
241
+ ).label(self.SCORE_LABEL)
242
+
243
+ stmt = select(
244
+ ranked.c.entity_id,
245
+ score,
246
+ ranked.c.highlight_text,
247
+ ranked.c.highlight_path,
248
+ perfect.label("perfect_match"),
249
+ ).select_from(ranked)
250
+
251
+ stmt = self._apply_fused_pagination(stmt, score, ranked.c.entity_id)
252
+
253
+ return stmt.order_by(
254
+ score.desc().nulls_last(),
255
+ ranked.c.entity_id.asc(),
256
+ ).params(q_vec=self.q_vec)
257
+
258
+ def _apply_fused_pagination(
259
+ self,
260
+ stmt: Select,
261
+ score_column: ColumnElement,
262
+ entity_id_column: ColumnElement,
263
+ ) -> Select:
264
+ """Keyset paginate by fused score + id."""
265
+ if self.page_after_score is not None and self.page_after_id is not None:
266
+ score_param = self._quantize_score_for_pagination(self.page_after_score)
267
+ stmt = stmt.where(
268
+ or_(
269
+ score_column < score_param,
270
+ and_(score_column == score_param, entity_id_column > self.page_after_id),
271
+ )
272
+ )
273
+ return stmt
274
+
275
+ @property
276
+ def metadata(self) -> SearchMetadata:
277
+ return SearchMetadata.hybrid()
@@ -0,0 +1,94 @@
1
+ # Copyright 2019-2025 SURF, GÉANT.
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
13
+
14
+ from sqlalchemy import Select, and_, cast, func, literal, or_, select
15
+ from sqlalchemy.sql.expression import ColumnElement
16
+
17
+ from orchestrator.db.models import AiSearchIndex
18
+ from orchestrator.search.core.types import SearchMetadata
19
+
20
+ from ..pagination import PaginationParams
21
+ from .base import Retriever
22
+
23
+
24
+ class SemanticRetriever(Retriever):
25
+ """Ranks results based on the minimum semantic vector distance."""
26
+
27
+ def __init__(self, vector_query: list[float], pagination_params: PaginationParams) -> None:
28
+ self.vector_query = vector_query
29
+ self.page_after_score = pagination_params.page_after_score
30
+ self.page_after_id = pagination_params.page_after_id
31
+
32
+ def apply(self, candidate_query: Select) -> Select:
33
+ cand = candidate_query.subquery()
34
+
35
+ dist = AiSearchIndex.embedding.l2_distance(self.vector_query)
36
+
37
+ raw_min = func.min(dist).over(partition_by=AiSearchIndex.entity_id)
38
+
39
+ # Normalize score to preserve ordering in accordance with other retrievers:
40
+ # smaller distance = higher score
41
+ similarity = literal(1.0, type_=self.SCORE_NUMERIC_TYPE) / (
42
+ literal(1.0, type_=self.SCORE_NUMERIC_TYPE) + cast(raw_min, self.SCORE_NUMERIC_TYPE)
43
+ )
44
+
45
+ score = cast(
46
+ func.round(cast(similarity, self.SCORE_NUMERIC_TYPE), self.SCORE_PRECISION), self.SCORE_NUMERIC_TYPE
47
+ ).label(self.SCORE_LABEL)
48
+
49
+ combined_query = (
50
+ select(
51
+ AiSearchIndex.entity_id,
52
+ score,
53
+ func.first_value(AiSearchIndex.value)
54
+ .over(partition_by=AiSearchIndex.entity_id, order_by=[dist.asc(), AiSearchIndex.path.asc()])
55
+ .label(self.HIGHLIGHT_TEXT_LABEL),
56
+ func.first_value(AiSearchIndex.path)
57
+ .over(partition_by=AiSearchIndex.entity_id, order_by=[dist.asc(), AiSearchIndex.path.asc()])
58
+ .label(self.HIGHLIGHT_PATH_LABEL),
59
+ )
60
+ .select_from(AiSearchIndex)
61
+ .join(cand, cand.c.entity_id == AiSearchIndex.entity_id)
62
+ .where(AiSearchIndex.embedding.isnot(None))
63
+ .distinct(AiSearchIndex.entity_id)
64
+ )
65
+ final_query = combined_query.subquery("ranked_semantic")
66
+
67
+ stmt = select(
68
+ final_query.c.entity_id,
69
+ final_query.c.score,
70
+ final_query.c.highlight_text,
71
+ final_query.c.highlight_path,
72
+ ).select_from(final_query)
73
+
74
+ stmt = self._apply_semantic_pagination(stmt, final_query.c.score, final_query.c.entity_id)
75
+
76
+ return stmt.order_by(final_query.c.score.desc().nulls_last(), final_query.c.entity_id.asc())
77
+
78
+ @property
79
+ def metadata(self) -> SearchMetadata:
80
+ return SearchMetadata.semantic()
81
+
82
+ def _apply_semantic_pagination(
83
+ self, stmt: Select, score_column: ColumnElement, entity_id_column: ColumnElement
84
+ ) -> Select:
85
+ """Apply semantic score pagination with precise Decimal handling."""
86
+ if self.page_after_score is not None and self.page_after_id is not None:
87
+ score_param = self._quantize_score_for_pagination(self.page_after_score)
88
+ stmt = stmt.where(
89
+ or_(
90
+ score_column < score_param,
91
+ and_(score_column == score_param, entity_id_column > self.page_after_id),
92
+ )
93
+ )
94
+ return stmt
@@ -0,0 +1,39 @@
1
+ # Copyright 2019-2025 SURF, GÉANT.
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
13
+
14
+ from sqlalchemy import Select, literal, select
15
+
16
+ from orchestrator.search.core.types import SearchMetadata
17
+
18
+ from ..pagination import PaginationParams
19
+ from .base import Retriever
20
+
21
+
22
+ class StructuredRetriever(Retriever):
23
+ """Applies a dummy score for purely structured searches with no text query."""
24
+
25
+ def __init__(self, pagination_params: PaginationParams) -> None:
26
+ self.page_after_id = pagination_params.page_after_id
27
+
28
+ def apply(self, candidate_query: Select) -> Select:
29
+ cand = candidate_query.subquery()
30
+ stmt = select(cand.c.entity_id, literal(1.0).label("score")).select_from(cand)
31
+
32
+ if self.page_after_id:
33
+ stmt = stmt.where(cand.c.entity_id > self.page_after_id)
34
+
35
+ return stmt.order_by(cand.c.entity_id.asc())
36
+
37
+ @property
38
+ def metadata(self) -> SearchMetadata:
39
+ return SearchMetadata.structured()
@@ -1,3 +1,16 @@
1
+ # Copyright 2019-2025 SURF, GÉANT.
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
13
+
1
14
  import json
2
15
  import re
3
16
 
@@ -16,7 +29,7 @@ logger = structlog.get_logger(__name__)
16
29
 
17
30
 
18
31
  def generate_highlight_indices(text: str, term: str) -> list[tuple[int, int]]:
19
- """Finds all occurrences of individual words from the term with word boundary matching case-insensitively."""
32
+ """Finds all occurrences of individual words from the term, including both word boundary and substring matches."""
20
33
  if not text or not term:
21
34
  return []
22
35
 
@@ -24,14 +37,15 @@ def generate_highlight_indices(text: str, term: str) -> list[tuple[int, int]]:
24
37
  words = [w.strip() for w in term.split() if w.strip()]
25
38
 
26
39
  for word in words:
40
+ # First find word boundary matches
27
41
  word_boundary_pattern = rf"\b{re.escape(word)}\b"
28
- matches = list(re.finditer(word_boundary_pattern, text, re.IGNORECASE))
29
-
30
- if not matches:
31
- substring_pattern = re.escape(word)
32
- matches = list(re.finditer(substring_pattern, text, re.IGNORECASE))
42
+ word_matches = list(re.finditer(word_boundary_pattern, text, re.IGNORECASE))
43
+ all_matches.extend([(m.start(), m.end()) for m in word_matches])
33
44
 
34
- all_matches.extend([(m.start(), m.end()) for m in matches])
45
+ # Then find all substring matches
46
+ substring_pattern = re.escape(word)
47
+ substring_matches = list(re.finditer(substring_pattern, text, re.IGNORECASE))
48
+ all_matches.extend([(m.start(), m.end()) for m in substring_matches])
35
49
 
36
50
  return sorted(set(all_matches))
37
51
 
@@ -1,4 +1,15 @@
1
- from typing import assert_never
1
+ # Copyright 2019-2025 SURF, GÉANT.
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
2
13
 
3
14
  from sqlalchemy import select, text
4
15
  from sqlalchemy.exc import ProgrammingError
@@ -8,72 +19,48 @@ from orchestrator.db import db
8
19
  from orchestrator.db.database import WrappedSession
9
20
  from orchestrator.db.models import AiSearchIndex
10
21
  from orchestrator.search.core.types import EntityType, FieldType
11
- from orchestrator.search.filters import (
12
- DateRangeFilter,
13
- DateValueFilter,
14
- EqualityFilter,
15
- FilterCondition,
16
- FilterTree,
17
- LtreeFilter,
18
- NumericRangeFilter,
19
- NumericValueFilter,
20
- PathFilter,
21
- StringFilter,
22
+ from orchestrator.search.filters import FilterCondition, FilterTree, LtreeFilter, PathFilter
23
+ from orchestrator.search.filters.definitions import operators_for
24
+ from orchestrator.search.retrieval.exceptions import (
25
+ EmptyFilterPathError,
26
+ IncompatibleFilterTypeError,
27
+ InvalidEntityPrefixError,
28
+ InvalidLtreePatternError,
29
+ PathNotFoundError,
22
30
  )
23
31
 
24
32
 
25
33
  def is_filter_compatible_with_field_type(filter_condition: FilterCondition, field_type: FieldType) -> bool:
26
34
  """Check whether a filter condition is compatible with a given field type.
27
35
 
28
- Parameters
29
- ----------
30
- filter_condition : FilterCondition
31
- The filter condition instance to check.
32
- field_type : FieldType
33
- The type of field from the index schema.
36
+ Args:
37
+ filter_condition (FilterCondition): The filter condition instance to check.
38
+ field_type (FieldType): The type of field from the index schema.
34
39
 
35
40
  Returns:
36
- -------
37
- bool
38
- True if the filter condition is valid for the given field type, False otherwise.
41
+ bool: True if the filter condition is valid for the given field type, False otherwise.
39
42
  """
40
43
 
41
- match filter_condition:
42
- case LtreeFilter():
43
- return True # Filters for path only
44
- case DateRangeFilter() | DateValueFilter():
45
- return field_type == FieldType.DATETIME
46
- case NumericRangeFilter() | NumericValueFilter():
47
- return field_type in {FieldType.INTEGER, FieldType.FLOAT}
48
- case StringFilter():
49
- return field_type == FieldType.STRING
50
- case EqualityFilter():
51
- return field_type in {
52
- FieldType.BOOLEAN,
53
- FieldType.UUID,
54
- FieldType.BLOCK,
55
- FieldType.RESOURCE_TYPE,
56
- FieldType.STRING,
57
- }
58
- case _:
59
- assert_never(filter_condition)
44
+ # LtreeFilter is for path filtering only and is thus compatible with all field types.
45
+ if isinstance(filter_condition, LtreeFilter):
46
+ return True
47
+
48
+ # Get valid operators for this field type and check if the filter's operator is valid.
49
+ valid_operators = operators_for(field_type)
50
+ return filter_condition.op in valid_operators
60
51
 
61
52
 
62
53
  def is_lquery_syntactically_valid(pattern: str, db_session: WrappedSession) -> bool:
63
54
  """Validate whether a string is a syntactically correct `lquery` pattern.
64
55
 
65
- Parameters
66
- ----------
67
- pattern : str
68
- The LTree lquery pattern string to validate.
69
- db_session : WrappedSession
70
- The database session used to test casting.
56
+ Args:
57
+ pattern (str): The LTree lquery pattern string to validate.
58
+ db_session (WrappedSession): The database session used to test casting.
71
59
 
72
60
  Returns:
73
- -------
74
- bool
75
- True if the pattern is valid, False if it fails to cast in PostgreSQL.
61
+ bool: True if the pattern is valid, False if it fails to cast in PostgreSQL.
76
62
  """
63
+
77
64
  try:
78
65
  with db_session.begin_nested():
79
66
  db_session.execute(text("SELECT CAST(:pattern AS lquery)"), {"pattern": pattern})
@@ -86,10 +73,9 @@ def get_structured_filter_schema() -> dict[str, str]:
86
73
  """Retrieve all distinct filterable paths and their field types from the index.
87
74
 
88
75
  Returns:
89
- -------
90
- Dict[str, str]
91
- Mapping of path strings to their corresponding field type values.
76
+ Dict[str, str]: Mapping of path strings to their corresponding field type values.
92
77
  """
78
+
93
79
  stmt = select(AiSearchIndex.path, AiSearchIndex.value_type).distinct().order_by(AiSearchIndex.path)
94
80
  result = db.session.execute(stmt)
95
81
  return {str(path): value_type.value for path, value_type in result}
@@ -98,16 +84,13 @@ def get_structured_filter_schema() -> dict[str, str]:
98
84
  def validate_filter_path(path: str) -> str | None:
99
85
  """Check if a given path exists in the index and return its field type.
100
86
 
101
- Parameters
102
- ----------
103
- path : str
104
- The fully qualified LTree path.
87
+ Args:
88
+ path (str): The fully qualified LTree path.
105
89
 
106
90
  Returns:
107
- -------
108
- Optional[str]
109
- The value type of the field if found, otherwise None.
91
+ Optional[str]: The value type of the field if found, otherwise None.
110
92
  """
93
+
111
94
  stmt = select(AiSearchIndex.value_type).where(AiSearchIndex.path == Ltree(path)).limit(1)
112
95
  result = db.session.execute(stmt).scalar_one_or_none()
113
96
  return result.value if result else None
@@ -123,47 +106,42 @@ async def complete_filter_validation(filter: PathFilter, entity_type: EntityType
123
106
  4. Filter type matches the field's value_type
124
107
  5. Path starts with the correct entity type prefix (unless wildcard)
125
108
 
126
- Parameters
127
- ----------
128
- filter : PathFilter
129
- The filter to validate.
130
- entity_type : EntityType
131
- The entity type being searched.
109
+ Args:
110
+ filter (PathFilter): The filter to validate.
111
+ entity_type (EntityType): The entity type being searched.
132
112
 
133
113
  Raises:
134
- ------
135
- ValueError
136
- If any of the validation checks fail.
114
+ ValueError: If any of the validation checks fail.
137
115
  """
116
+
138
117
  # Ltree is a special case
139
118
  if isinstance(filter.condition, LtreeFilter):
140
119
  lquery_pattern = filter.condition.value
141
120
  if not is_lquery_syntactically_valid(lquery_pattern, db.session):
142
- raise ValueError(f"Ltree pattern '{lquery_pattern}' has invalid syntax.")
121
+ raise InvalidLtreePatternError(lquery_pattern)
143
122
  return
144
123
 
145
124
  if not filter.path or not filter.path.strip():
146
- raise ValueError("Filter path cannot be empty")
125
+ raise EmptyFilterPathError()
147
126
 
148
127
  # 1. Check if path exists in database
149
128
  db_field_type_str = validate_filter_path(filter.path)
150
129
  if db_field_type_str is None:
151
- raise ValueError(f"Path '{filter.path}' does not exist in database schema")
130
+ raise PathNotFoundError(filter.path)
152
131
 
153
132
  db_field_type = FieldType(db_field_type_str)
154
133
 
155
134
  # 2. Check filter compatibility with field type
156
135
  if not is_filter_compatible_with_field_type(filter.condition, db_field_type):
157
- raise ValueError(
158
- f"Filter '{type(filter.condition).__name__}' not compatible with field type '{db_field_type.value}'"
136
+ expected_operators = operators_for(db_field_type)
137
+ raise IncompatibleFilterTypeError(
138
+ filter.condition.op.value, db_field_type.value, filter.path, expected_operators
159
139
  )
160
140
 
161
141
  # 3. Check entity type prefix requirements (unless it's a wildcard path)
162
142
  expected_prefix = f"{entity_type.value.lower()}."
163
143
  if not filter.path.startswith(expected_prefix) and not filter.path.startswith("*"):
164
- raise ValueError(
165
- f"Filter path '{filter.path}' must start with '{expected_prefix}' for {entity_type.value} searches."
166
- )
144
+ raise InvalidEntityPrefixError(filter.path, expected_prefix, entity_type.value)
167
145
 
168
146
 
169
147
  async def validate_filter_tree(filters: FilterTree | None, entity_type: EntityType) -> None:
@@ -0,0 +1,12 @@
1
+ # Copyright 2019-2025 SURF, GÉANT.
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.