askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
object_search.py · 5.0 KB · 140 lines
| 1 | """Cross-collection record search driven by schema ``search`` metadata. |
| 2 | |
| 3 | A collection opts into global search by declaring which fields are |
| 4 | searchable in its schema: |
| 5 | |
| 6 | {"search": {"fields": ["title", "content"]}} |
| 7 | |
| 8 | Search semantics deliberately match the previous Django implementation so |
| 9 | existing clients keep their expectations: the query is split on |
| 10 | whitespace, every term must match at least one searchable field |
| 11 | (case-insensitive substring), and a short hex-ish query also matches |
| 12 | record ids by prefix. Results come back per collection with no ranking. |
| 13 | |
| 14 | This module is pure matching and configuration. The server owns IO, |
| 15 | permission checks, and row filtering — search must only ever see records |
| 16 | the caller is already allowed to read. |
| 17 | """ |
| 18 | |
| 19 | from __future__ import annotations |
| 20 | |
| 21 | import re |
| 22 | from typing import Any, Mapping |
| 23 | |
| 24 | DEFAULT_COLLECTION_LIMIT = 10 |
| 25 | MAX_COLLECTION_LIMIT = 100 |
| 26 | |
| 27 | _ID_PREFIX_RE = re.compile(r"^[0-9a-fA-F-]+$") |
| 28 | _MIN_ID_PREFIX_HEX = 4 |
| 29 | |
| 30 | |
| 31 | class InvalidSearchConfigError(ValueError): |
| 32 | """Raised when a schema declares an unusable ``search`` section.""" |
| 33 | |
| 34 | |
| 35 | def search_config(schema: Mapping[str, Any]) -> dict[str, Any] | None: |
| 36 | """Return the normalized search config for one schema, or None. |
| 37 | |
| 38 | Returns None when the schema does not declare ``search``. Raises |
| 39 | InvalidSearchConfigError when a declared config is malformed, so the |
| 40 | misconfiguration surfaces as a warning instead of silently dropping |
| 41 | the collection from search results. |
| 42 | """ |
| 43 | section = schema.get("search") |
| 44 | if section is None: |
| 45 | return None |
| 46 | if not isinstance(section, Mapping): |
| 47 | raise InvalidSearchConfigError("Schema search section must be an object") |
| 48 | |
| 49 | fields = section.get("fields") |
| 50 | if not isinstance(fields, list) or not fields: |
| 51 | raise InvalidSearchConfigError("Schema search.fields must be a non-empty list") |
| 52 | if not all(isinstance(name, str) and name for name in fields): |
| 53 | raise InvalidSearchConfigError("Schema search.fields must contain field names") |
| 54 | |
| 55 | result_fields = section.get("result_fields", None) |
| 56 | if result_fields is None: |
| 57 | result_fields = _default_result_fields(schema, fields) |
| 58 | elif not isinstance(result_fields, list) or not all( |
| 59 | isinstance(name, str) and name for name in result_fields |
| 60 | ): |
| 61 | raise InvalidSearchConfigError("Schema search.result_fields must be a list of field names") |
| 62 | |
| 63 | return {"fields": list(fields), "result_fields": list(result_fields)} |
| 64 | |
| 65 | |
| 66 | def split_terms(query: str) -> list[str]: |
| 67 | """Return the whitespace-separated search terms of a query.""" |
| 68 | return [term for term in query.split() if term] |
| 69 | |
| 70 | |
| 71 | def looks_like_id_prefix(query: str) -> bool: |
| 72 | """Return True when a query should also match record ids by prefix.""" |
| 73 | text = query.strip() |
| 74 | if not _ID_PREFIX_RE.fullmatch(text): |
| 75 | return False |
| 76 | return len(text.replace("-", "")) >= _MIN_ID_PREFIX_HEX |
| 77 | |
| 78 | |
| 79 | def record_matches(record: Mapping[str, Any], query: str, fields: list[str]) -> bool: |
| 80 | """Return True when a record matches every term of a query. |
| 81 | |
| 82 | Each term must appear in at least one searchable field. As a |
| 83 | fallback, an id-prefix-looking query matches the record id directly. |
| 84 | """ |
| 85 | terms = split_terms(query) |
| 86 | if terms and all(_term_in_fields(record, term, fields) for term in terms): |
| 87 | return True |
| 88 | if looks_like_id_prefix(query): |
| 89 | record_id = str(record.get("id") or "") |
| 90 | return record_id.lower().startswith(query.strip().lower()) |
| 91 | return False |
| 92 | |
| 93 | |
| 94 | def search_records( |
| 95 | records: list[dict[str, str]], |
| 96 | query: str, |
| 97 | config: Mapping[str, Any], |
| 98 | *, |
| 99 | limit: int = DEFAULT_COLLECTION_LIMIT, |
| 100 | ) -> list[dict[str, str]]: |
| 101 | """Return matching records trimmed to the config's result fields.""" |
| 102 | if limit < 1: |
| 103 | raise ValueError("Search limit must be positive") |
| 104 | |
| 105 | matches: list[dict[str, str]] = [] |
| 106 | for record in records: |
| 107 | if not record_matches(record, query, config["fields"]): |
| 108 | continue |
| 109 | matches.append(_result_record(record, config["result_fields"])) |
| 110 | if len(matches) >= limit: |
| 111 | break |
| 112 | return matches |
| 113 | |
| 114 | |
| 115 | def _term_in_fields(record: Mapping[str, Any], term: str, fields: list[str]) -> bool: |
| 116 | needle = term.lower() |
| 117 | for field in fields: |
| 118 | value = record.get(field) |
| 119 | if value is not None and needle in str(value).lower(): |
| 120 | return True |
| 121 | return False |
| 122 | |
| 123 | |
| 124 | def _result_record(record: dict[str, str], result_fields: list[str]) -> dict[str, str]: |
| 125 | return {field: record[field] for field in result_fields if field in record} |
| 126 | |
| 127 | |
| 128 | def _default_result_fields(schema: Mapping[str, Any], fields: list[str]) -> list[str]: |
| 129 | views = schema.get("views") |
| 130 | list_fields: list[str] = [] |
| 131 | if isinstance(views, Mapping): |
| 132 | candidate = views.get("list_fields") |
| 133 | if isinstance(candidate, list): |
| 134 | list_fields = [name for name in candidate if isinstance(name, str)] |
| 135 | |
| 136 | ordered: list[str] = ["id"] |
| 137 | for name in list_fields or fields: |
| 138 | if name not in ordered: |
| 139 | ordered.append(name) |
| 140 | return ordered |