diff --git a/app/features/search/repository.py b/app/features/search/repository.py index 3455f45..181e020 100644 --- a/app/features/search/repository.py +++ b/app/features/search/repository.py @@ -39,12 +39,29 @@ def _string_value(value: Any) -> str | None: return None -def _flatten_projection_values(value: Any) -> list[str]: +def _flatten_projection_values(value: Any, json_path: Any = None) -> list[str]: if value is None: return [] if isinstance(value, list): result: list[str] = [] for item in value: + result.extend(_flatten_projection_values(item, json_path)) + return result + if isinstance(value, dict): + if json_path: + paths = [json_path] if isinstance(json_path, str) else list(json_path) + extracted: list[str] = [] + for p in paths: + sub_val = value.get(p) + if sub_val is not None: + extracted.extend(_flatten_projection_values(sub_val)) + if extracted: + return extracted + direct = _string_value(value) + if direct: + return [direct] + result = [] + for item in value.values(): result.extend(_flatten_projection_values(item)) return result direct = _string_value(value) @@ -290,9 +307,30 @@ class SearchRepository: values.append(row["value_date"].isoformat()) return values - def _project_value(self, raw: Any, strategy: str) -> list[ProjectionValue]: + def _project_value( + self, + raw: Any, + strategy: str, + metadata: dict[str, Any] | None = None, + ) -> list[ProjectionValue]: strategy = strategy.strip().lower() - raw_values = _flatten_projection_values(raw) + json_path = (metadata or {}).get("json_path") or (metadata or {}).get("json_key") + raw_values = _flatten_projection_values(raw, json_path=json_path) + if strategy in ("multivalue", "comma_delimited", "delimited"): + split_values: list[str] = [] + for item in raw_values: + if "," in item or ";" in item: + for token in item.replace(";", ",").split(","): + cleaned = token.strip().rstrip(".").strip() + if cleaned: + cleaned = cleaned[:1].upper() + cleaned[1:] + split_values.append(cleaned) + else: + cleaned = item.strip().rstrip(".").strip() + if cleaned: + cleaned = cleaned[:1].upper() + cleaned[1:] + split_values.append(cleaned) + raw_values = split_values projected: list[ProjectionValue] = [] seen: set[str] = set() for label in raw_values: @@ -406,7 +444,13 @@ class SearchRepository: ) values: list[ProjectionValue] = [] for raw in raw_rows: - values.extend(self._project_value(raw, mapping.mapping_strategy)) + values.extend( + self._project_value( + raw, + mapping.mapping_strategy, + metadata=mapping.metadata, + ) + ) seen: set[str] = set() for value in values: