Skip to content

Search

GeneralManager ships configuration primitives and a development backend for search. Production deployments are expected to use an external search service.

For first setup, follow the search tutorial. The normal flow is: declare each manager's index fields, initialize the backend, index current records, then query the generated GraphQL search field. Supported manager changes update the projection; reconciliation repairs missing or dirty indexes. Indexes can lag behind source data. GraphQL checks access against the native source; direct backend calls return search documents and do not supply that authorization layer.

Overview

Search is configured per manager and aggregated per index name. Each manager can contribute documents to one or more indexes. Index settings (searchable fields, filterable fields, sortable fields, and field boosts) are derived from all managers that declare the same index name.

Search documents include: - A stable, type-scoped document id. - A type label and identification mapping for reconstruction. - A data payload built from configured fields (and filter fields). - Explicit native identity metadata, inherited from ORM PK/reference fields and properties declared with is_identifier=True.

Identity metadata controls equality/membership transport, without converting manager, database, bucket, or stored business values to strings. Meilisearch uses separate versioned tokens because numeric filtering can collapse adjacent large integer IDs and text equality can ignore case. DevSearch compares native values using its existing operator rules. Use __in for portable collection membership. Names ending in _id alone do not declare identities; ordinary business numbers and pattern/range filters retain existing adapter behavior. Custom document aliases can declare their native scalar kind through IndexConfig.identity_fields. See the search API for manual documents, filter groups, and custom backend contracts.

Search configuration model

IndexConfig and FieldConfig

Use IndexConfig entries to describe which fields should be indexed and how they should be filtered and sorted. Fields can optionally include per-field boosts. Sortable fields must be declared on the index to be sortable.

from general_manager import FieldConfig, IndexConfig

class Project(GeneralManager):
    class SearchConfig:
        indexes = [
            IndexConfig(
                name="global",
                fields=[
                    "name",
                    FieldConfig(name="leader__name", boost=2.0),
                ],
                filters=["status", "leader_id"],
                sorts=["name", "status"],
                boost=1.2,
            )
        ]

Field and index rules: - fields: searchable fields for full-text matching. - filters: filterable fields allowed in filters (plus the built-in type). - sorts: sortable fields allowed for typed GraphQL orderBy terms. - FieldConfig.boost: per-field boost (must be > 0). - IndexConfig.boost: per-index boost (must be > 0; used by DevSearch). - IndexConfig.min_score: reserved for backend-specific use (not applied by built-in backends today).

Optional extras

These helpers are optional and only required if your adapter needs them.

  • document_id: Callable used to produce a stable document identifier.
  • type_label: Explicit label for multi-manager search unions.
  • to_document: Callable that serializes a manager instance into a document.
  • update_strategy: Adapter-specific hook for sync vs async (not used by built-in backends today).
class Project(GeneralManager):
    class SearchConfig:
        indexes = [IndexConfig(name="global", fields=["name"])]
        type_label = "Project"

        @staticmethod
        def document_id(instance: "Project") -> str:
            return f"Project:{instance.id}"

        @staticmethod
        def to_document(instance: "Project") -> dict:
            return {
                "name": instance.name,
                "status": instance.status,
            }

to_document should only return keys configured on the IndexConfig used for indexing. Filter and sort fields are included automatically if not present in the returned mapping. For GraphQL search, custom projections must declare complete native provenance using field_dependencies (see below).

An indexed field can depend on a different manager. Declare that dependency on the manager that owns the search document with SearchInvalidationRule:

from general_manager import GeneralManager, IndexConfig, SearchInvalidationRule
from investments.managers import InvestNumber


def affected_projects(change, owner_class):
    return owner_class.filter(customer=change.instance)


class Project(GeneralManager):
    class SearchConfig:
        indexes = (
            IndexConfig(name="global", fields=("customer__name",)),
        )
        invalidation_rules = (
            SearchInvalidationRule(
                source="customers.managers.Customer",
                resolve=affected_projects,
                indexes=("global",),
            ),
            SearchInvalidationRule(
                source=InvestNumber,
                resolve=lambda change, owner: owner.filter(
                    invest_numbers=change.instance
                ),
                relation="invest_numbers",
                indexes=("global",),
            ),
        )

Here Customer and InvestNumber are existing manager classes, and Project.Interface defines the invest_numbers many-to-many field. A resolver receives a frozen SearchChange and the owning manager class. It must return instances of that owner manager; returning identifiers, ORM model instances, or another manager type makes the entire rule fall back to reconciliation. Setting resolve=None deliberately chooses that dirty-only fallback.

Resolver phases preserve the information needed to find changing relations:

  • create resolves only after the new source instance exists;
  • update resolves before and after, then combines and deduplicates both owner sets;
  • delete resolves only before the source disappears.

Target identifications are copied while the resolver runs, so later mutation of the returned manager objects cannot alter queued work. Resolver exceptions, invalid targets, an invalid bound setting, or more than SEARCH_INVALIDATION_MAX_TARGETS targets produce no partial targeted work for that rule. Instead, every selected owner/index pair remains dirty for a full reconciliation. The default maximum is 1000; accepted targets are dispatched in SEARCH_INVALIDATION_BATCH_SIZE chunks, which default to 100. Both values must be positive, non-boolean integers.

Normal lifecycle work is commit-bound. On Django's default database alias, dirty markers are written inside the business transaction and dispatch runs after commit, so rollback leaks neither marker nor task. For another source alias, the post-commit callback writes the marker in the default control-plane database and then dispatches. This avoids rollback leakage but cannot make the two databases atomic: a process crash after the source commit and before that callback is a known gap. Periodic reconciliation is the repair path.

Many-to-many rules additionally set relation to the owner-side M2M field. GeneralManager listens to the exact auto-created or custom through model and supports related-manager add(), remove(), clear(), and set() in both forward and reverse directions. The owner must use exactly the standard {"id": pk} identification, and both through foreign keys must store their endpoint primary keys. Direct through-model saves/deletes, raw SQL, bulk writes, and self-symmetrical M2M relations are unsupported; use the related manager API or explicitly reconcile afterward. A .set() may emit more than one bounded event, so duplicate batches are permitted.

Search invalidation is intentionally at-least-repairable, not exactly-once. It does not maintain dependency snapshots, an outbox, cross-database atomicity, or transaction-wide event coalescing. Every exact manager/index pair is generation fenced: newer dirty work cannot be cleared by an older task or reconciliation claim. A worker that accepts a task and later fails best-effort marks the exact pair dirty again. Reconciliation remains the durable safety net.

Document identity and permissions

Search documents always include the manager identification mapping. The default document id is derived from that identification plus the manager type, so ids remain stable across database and non-database interfaces. If you override type_label, keep it stable; it is part of the id and is used to segment results by manager type.

If you provide SearchConfig.document_id, it is also the delete identity. It must return the same stable value for an object before and after updates, including updates caused by related-data invalidation. Changing the result can leave the old backend document behind until reconciliation removes it.

GraphQL search applies get_read_permission_filter() to the search query and then re-checks permissions on instantiated results. User filters are conjoined with permission filters, which may expand into OR groups when multiple permission filters are present. Compatible criteria are combined for backend prefiltering; when a user and permission criterion share the same lookup key, the complete user mapping is sent to the backend and the permission criterion is enforced after hydration so neither value is discarded. Permission "exclude" mappings are not sent to the backend prefilter; they are enforced during the later per-instance authorization pass. Empty permission constraints such as {}, {"filter": {}}, or {"exclude": {}} create an unrestricted backend group relative to the user filters, with any exclude-only restriction still handled by the instance check. When a manager has no permission filter alternatives, the user filter mapping is passed through unchanged.

Static read decisions use fast paths. A manager with deny_all is skipped entirely: GraphQL does not call the search backend for it or add backend timing to the response. A manager with allow_all still queries the backend with the user's filters, but adds no permission filter group and does not run per-hit instance checks. Its reported tookMs retains the normal response semantics. Conditional mapping and None callbacks retain the existing behavior: mapping filters prefilter hits, exclude mappings and unfilterable rules are resolved by the later permission pass, and aggregate authorization logging occurs only when that pass requires an instance check.

Search authorization is also an identity boundary. When the GraphQL schema is built, each manager/index projection is classified from its definitions. The fast route requires proof that relevant fields add no read restrictions beyond the manager's row rules. Inherited rules and field-rule OR alternatives that include all row-rule alternatives do not count as additional restrictions. Additional field rules, custom executable permission rules, and unknown definitions retain the thorough route; invalid provenance raises before backend search. This classification lasts for the schema lifetime and assumes immutable definitions. Restart and rebuild the schema after changing permissions or search configuration. It caches no user or object authorization decisions.

Every configured serialized searchable, filterable, and sortable key is covered, even when a request does not filter or sort on it. DevSearch scores all serialized values, and shared indexes can make fields searchable across manager types.

For ORM aliases such as customer_id and team_members_list__role_id, the startup proof covers the alias, the underlying relation, and the referenced field. A primary-key reference also checks the target manager's public id. A foreign key with to_field uses the referenced field's own permissions.

The fast runtime route applies only to the native DatabaseBucket with standard id identification. It loads the current native source instances for each hit batch and applies the existing row authorization. The thorough route enumerates the row-authorized source domain and checks required field paths before backend search. Calculation and remote managers retain full-source authorization and completeness checks even when their static classification is fast. Calculation sources require an enumerable possible_values domain; incomplete remote pages fail explicitly.

Hits with missing, malformed, out-of-domain identities or unexpected types never contribute to results or totals. Fast related projections use the existing native permission traversal on each hit: denial of a referenced object removes the entire owner projection before counting, sorting, or pagination. The thorough route still raises when access needed for an operand is denied.

Every hit must include stored SearchHit.data containing exactly the configured searchable, filterable, and sortable keys. Missing stored data raises an error; extra or missing keys cause the hit to be skipped until reindexing. This prevents removed restricted fields in older documents from influencing current search. Index maintenance owns value freshness for every projection. Search never reserializes source rows or compares their values with the index. Invalidation and reconciliation must remove obsolete content, including text from former relation targets, before it can influence search. Current-source authorization alone cannot detect values left over from a replaced relation.

For supported native collection relations, the fast candidate SELECT can also prove emptiness with correlated EXISTS expressions. Only proven empty collections avoid a subsequent SELECT. Populated, custom, historical and cross-database relations retain their normal loading and permission checks. Empty-collection proofs belong to their calculation run and expire when that run ends or the existing data-change invalidation clears its ORM results. Native Django M2M add, remove, clear, and set operations on initialized GM models also clear these local snapshots, in either relation direction. This cache invalidation does not schedule indexing or update persistent dependency caches. Arbitrary raw or bulk ORM writes bypass the supported mutation lifecycle; do not reuse an existing calculation run after such writes. Retained managers cannot reuse a cached empty value derived from an expired proof. Validated projection paths are reused from schema construction; runtime field and relation checks still run. A changed search configuration, including an in-place edit to an index's fields, filters or sorts, cannot reuse the old fast approval and uses thorough validation until the schema is rebuilt.

Projection dependencies

SearchConfig.field_dependencies maps each configured document key to a nonempty sequence of exact native paths. It is a trusted application assertion of all sources read across every serializer/property branch, including indirect helpers; the framework cannot infer arbitrary Python data access. It declares provenance, not value equality, so a custom alias can transform or combine its sources.

class Project(GeneralManager):
    class SearchConfig:
        indexes = [IndexConfig(name="global", fields=["label"])]
        field_dependencies = {"label": ("name", "customer__name")}

        @staticmethod
        def to_document(instance):
            return {"label": f"{instance.name}: {instance.customer.name}"}

Custom projections require a declaration for every configured searchable, filterable, and sortable key. If a custom mapping omits a key and indexing falls back to the native field, that actual field must also appear in its declaration. Custom document filters are not pushed down as native permission filters: a transformed value need not equal any source value.

The same contract supports native projections and properties. Native projection authorization always includes the actual serialized field as well as every declared source; a declaration cannot relabel away its permission. Properties without complete dependencies fail before backend search. When dependencies are provided, cover every configured key, not only the property. Source paths must resolve through native relations to supported scalar leaves. Typed scalar containers such as list[str] and dict[str, int] are supported. Bare list, tuple, set, frozenset, and dict declarations (including RequestField(list)) cannot establish the permissions of their elements. They are rejected even when the current value is empty or contains only scalars; runtime contents do not replace a complete dependency declaration. Unknown or invalid source paths raise a GraphQL error before backend search, including on the thorough route. Dependency declarations do not replace related-data invalidation; keep both accurate.

The types argument selects manager classes. Synthetic type filter keys are forwarded to the backend and retain its native filter semantics. For example, DevSearch compares filter keys with document data and does not inject the envelope's manager type into that data. A synthetic type key is removed from the permission operand check when a manager does not expose an actual field named type; a real exposed type field remains subject to its normal field permission.

GraphQL search API

When GraphQL is auto-created, a global search query is added. It accepts: - query: the full-text query string. - index: index name (defaults to global). - types: optional list of manager class names to restrict results. - filters: JSON string or list of filter items. - orderBy: optional typed sort terms with independent directions. - totalMode: optional total-count mode, either exact or bounded. - page / pageSize: pagination controls.

Results are returned as a union of manager GraphQL types: - results: nullable GraphQL list field containing the authorized manager instances as the generated union type. - total: nullable integer field containing the post-permission authorized hit count, not the backend raw total. - totalIsExact: nullable boolean field. true means total is the exact post-permission count. Retained for compatibility, it is always true on a successful response. - tookMs: nullable integer field containing accumulated backend search time in milliseconds when reported.

Omitted page defaults to 1 and omitted pageSize defaults to 10. Explicit 0 or negative pagination values are rejected as GraphQL BAD_USER_INPUT errors. The resolver uses the authorized native source instances described above and applies required per-instance read checks before counting and returning results. Manager search order follows GraphQL's manager registry order filtered to managers with search config. When types is supplied, unknown manager class names are ignored. Malformed or out-of-domain hit identification is skipped rather than hydrated. Invalid configured filter keys are reported as GraphQL errors. Backend lookup/search errors, source-authorization errors, permission errors, and sort comparison errors propagate. orderBy uses the configured sortable-field union for its enum, then validates every requested term against each selected type and index before backend fetching. Local retained-window and final comparison use the shared type-aware ordering semantics, with nulls last.

GraphQL search totals default to exact mode for backward compatibility:

GENERAL_MANAGER = {
    "GRAPHQL_SEARCH_TOTAL_MODE": "exact",
    "GRAPHQL_SEARCH_TOTAL_SCAN_LIMIT": 1000,
}

To configure the budget without changing Django settings, set the environment variable before starting the application, for example:

export GENERAL_MANAGER_GRAPHQL_SEARCH_TOTAL_SCAN_LIMIT=10000

Explicit Django settings retain precedence: GENERAL_MANAGER["GRAPHQL_SEARCH_TOTAL_SCAN_LIMIT"], then GENERAL_MANAGER_GRAPHQL_SEARCH_TOTAL_SCAN_LIMIT, then the legacy top-level GRAPHQL_SEARCH_TOTAL_SCAN_LIMIT, then the environment variable, then the unlimited default (None). Remove an explicit Django value if you want the environment to control the limit. Environment values accept positive integers, none, or unlimited; empty, malformed, zero or negative values raise BAD_USER_INPUT rather than silently falling back. Restart application workers after changing the deployment environment. This does not configure Meilisearch's separate per-index pagination.maxTotalHits cap; increase that provider setting separately when searching beyond its cap.

GRAPHQL_SEARCH_TOTAL_SCAN_LIMIT is an optional positive, server-owned candidate budget (unlimited by default) shared across all manager types in one search resolver call. Both total modes obey it. Client-selected page, pageSize, types, and totalMode cannot increase the budget. When finite, each backend request is limited to the remaining budget, and returned hits consume it before hydration or authorization; denied, stale, and unexpected-type hits also count.

Both modes scan until all selected types are exhausted. If the budget is consumed before exhaustion is established, either mode raises a GraphQL error with code SEARCH_SCAN_LIMIT_EXCEEDED and returns no partial result. This is conservative at the boundary: a full final backend page does not prove that there are no more hits, even when a backend's best-effort total equals the cap. Narrow the query or increase the server-owned budget when appropriate.

Compatibility change: bounded remains an accepted setting and request value, but no longer releases a partially authorized scan prefix. Successful responses always have exact totals and totalIsExact: true; clients must handle the limit error in both modes. Switching to bounded mode cannot avoid it.

Clients may choose totalMode: "exact" or totalMode: "bounded" but cannot disable the limit. Invalid modes and non-positive limits are rejected as GraphQL user-input errors. A backend returning more hits than requested raises SEARCH_SCAN_LIMIT_EXCEEDED before those hits are hydrated. This candidate budget does not bound time or memory spent internally by a search backend, nor combine budgets across separate GraphQL fields or requests.

Note: GraphQL currently keys types off manager class names. If you override type_label, keep it aligned with the class name when using types filters. Generated search unions also look up GraphQL object types by manager class name in the GraphQL type registry.

Example query:

query SearchProjects($filters: JSONString) {
  search(index: "global", query: "alpha", filters: $filters, orderBy: [{field: name}]) {
    total
    totalIsExact
    results {
      __typename
      ... on ProjectType { id name status }
      ... on ProjectTeamType { id name status }
    }
  }
}

Variables:

{
  "filters": "{\"status\": \"public\"}"
}

Filters and operators

Filters can be provided as: - A JSON object: {"status": "public"}. - A JSON list of filter items: [{"field": "status", "value": "public"}]. - A malformed JSON string or decoded non-object value, which normalizes to an empty filter set. - A Python/object mapping supplied directly by internal resolver calls; that mapping is returned unchanged rather than copied.

List items support field, optional op, and either value or values. If values is provided, it takes precedence over value; if op is also omitted, op defaults to in. Empty values is preserved as the filter value. The lookup key is field when op is blank and field__op otherwise; parsing does not validate operator names. Malformed list entries are skipped.

Nested relation filters use the generated GraphQL relation input shape. Direct relations are metadata entries with relation_kind="direct" and flatten to the relation's filter_lookup prefix. Collection relations use relation_kind="collection" and support any and none: any becomes a positive backend filter and none becomes an exclude. Nested excludes from none are inverted back into positive filters. Relation filter generation is limited by the configured relation depth; depth zero stops nested relation input creation. Global JSON filters use each manager's confirmed identity metadata for default equality, __exact and list/tuple __in. ORM primary keys and raw references use the existing input/target-field converters. Explicit native properties use @graph_ql_property(is_identifier=True) and their validated element annotation. List/tuple equality operands are also converted element by element, preserving adapters' existing OR grammar. String keys such as "007" keep their zeros; integer strings convert without floating-point rounding, including values above 2**53 - 1. Nulls remain null and invalid ID operands raise conversion errors. Business fields and non-equality lookups retain their backend behavior. Metadata is resolved independently per manager in a shared index, while Python, database and indexed values stay native.

Search permission constraints use the same shape as other GraphQL read prefilters: an ordered list of entries containing optional filter and exclude mappings. The search backend receives the user filters merged with each entry's filter mapping. Conflicting lookup keys retain the user predicate for the backend request and enforce the permission predicate on the hydrated instance, preserving their intersection. The paired exclude mapping is evaluated after the hit is instantiated. Empty entries ({}, {"filter": {}}, or {"exclude": {}}) mean that one alternative is unrestricted relative to the user filters. If the plan still requires instance checks, can_read_instance() must pass for that hit.

Custom callbacks that return a mapping or None remain conditional; None does not mean unrestricted access. Legacy permission classes that only provide get_permission_filter() also remain conditional and receive the same final per-instance checks as before.

Filter-key validation runs against the parsed top-level search filters before permission filters or relation normalizers are applied. Exclude-derived permission keys are not validated by the GraphQL search helper.

Supported lookup operators in the filter parser: - exact (default) - lt, lte, gt, gte - contains, startswith, endswith - in

Filter evaluation traverses attributes with getattr only; missing attributes evaluate to False. String containment, starts-with, and ends-with checks are case-sensitive. Incompatible comparisons return False; exact comparisons compare None like any other value.

Generated GraphQL filter input fields follow the attribute category:

  • id: id, id__exact, id__in, plus id__gt, id__gte, id__lt, and id__lte.
  • Measurement: the base field plus __gt, __gte, __lt, and __lte, all using the measurement scalar.
  • Numbers, dates, and datetimes: the base field plus __gt, __gte, __lt, and __lte.
  • Strings: the base field plus __exact, __icontains, __contains, __in, __startswith, and __endswith; __in is a list of the mapped base scalar and honors a string graphql_scalar metadata override.
  • Manager relations: direct or collection relation inputs are generated through the relation filter rules above, subject to relation depth.

Example list format (OR groups are created from list entries):

[
  {"field": "status", "value": "public"},
  {"field": "status", "op": "in", "values": ["draft", "archived"]}
]

Backend support by operator

  • DevSearch supports all operators listed above.
  • Meilisearch translates filters to equality and in only. Other operators are treated as equality checks. For advanced expressions, call the backend directly with filter_expression (Python usage only).

Index lifecycle

Use the management command to create/update index settings and reindex data:

python manage.py search_index
python manage.py search_index --reindex
python manage.py search_index --index global --reindex
python manage.py search_index --manager Project --reindex

Without --index, every registered index is ensured. Unknown index names are reported to stderr and ignored; if none are valid, the command exits before reindexing. --manager filters reindexing by manager class name and is only used when --reindex is set. Backend configuration, index setup, manager discovery, and reindexing errors propagate to the command caller.

Use --reindex after schema changes (field list, filters, or sort fields).

Async indexing

Set GENERAL_MANAGER["SEARCH_ASYNC"] = True (or SEARCH_ASYNC = True) to dispatch index updates through Celery. When disabled, updates run inline. The nested GENERAL_MANAGER value takes precedence over the top-level setting, and missing settings default to inline updates.

Celery is required for production async indexing; development can remain sync. dispatch_index_update(action=...) accepts only "index" and "delete". When async indexing is enabled and Celery is available, the Celery task is queued and a provided in-memory instance is ignored. Otherwise a provided instance runs inline against the current backend; without an instance, the task function runs synchronously and reconstructs the manager from the dotted manager_path and identification keyword mapping. Import, construction, backend, task enqueue, and indexer errors propagate to the caller. Celery task payloads must use identification values supported by the deployment's Celery serializer.

Search reconciliation

GeneralManager keeps search fresh in two layers:

  1. Manager create/update/delete operations update the affected search document.
  2. The search reconciler periodically checks durable index state and rebuilds manager/index pairs that need initialization, schema updates, or recovery after missed writes.

The reconciler stores one state row per searchable manager/index pair. Missing state means the pair has not been initialized. A changed schema fingerprint means the configured fields, filters, sorts, boosts, type label, document id, or document serializer, or update strategy changed. SearchIndexState.mark_dirty() uses timezone.now() for a first dirty timestamp, preserves that timestamp on later marks, and overwrites the reason. The reconciliation planner updates schema_fingerprint; claim acquisition and expiration are handled by reconciler helpers. clear_dirty() uses timezone.now(), marks the pair initialized on first success, records the latest reconciliation time, clears dirty/error fields, and releases any claim token.

Production deployments should enable GENERAL_MANAGER["SEARCH_RECONCILE_ENABLED"] = True and run Celery Beat. Development can either run the same Celery Beat path or use python manage.py search_reconcile --watch.

Search initialization is not coupled to GraphQL traffic or WSGI/ASGI request handling.

Backends

Backend contract and result models

SearchBackend is the extension point for adapters. Backends receive index names, queries, filters, sorting, and pagination options, then return a SearchResult. Adapters must also expose stored document IDs through list_document_ids() so reconciliation can remove stale documents after a manager/index reindex.

Search result models have stable responsibilities:

  • SearchDocument represents the indexed payload, including the type label, identification mapping, and data fields.
  • SearchHit denotes one matched document reference with its type label, identification mapping, optional score, index name, and returned data fields.
  • SearchResult contains the paginated response with hits, totals, timing, and raw backend payloads.

Document identifiers are strings. Identification mappings and document data are object-valued so adapters can carry normal manager IDs, enums, datetimes, lists, and other serializer-supported values without using untyped payloads. Backend settings and structured filters are also object-valued mappings. Concrete adapters define which setting keys, filter operators, raw response shapes, and unsupported-feature errors they accept; operational failures should use SearchBackendError or a subclass when the adapter can normalize them. Document ID uniqueness is scoped to one backend index unless an adapter documents broader constraints. Indexer-managed SearchDocument.index values match the index_name used for backend writes, but the protocol does not require adapters to reject mismatches. Payload mappings are not copied by the result models; callers should treat identification, data, boost mappings, and raw backend responses as immutable/read-only after passing them across the backend boundary. The dataclass field annotations are the validation boundary for SearchDocument; the model does not coerce or validate runtime values. frozen=True prevents attribute reassignment but does not make nested mappings immutable or guarantee practical hashability when fields contain unhashable values. Structured filters portably target SearchDocument.data; types handles manager labels separately, and nested filter syntax is adapter-specific. SearchResult.hits is the returned page after limit and offset; total is the number of matches before pagination when the backend can report it, and otherwise the adapter defines the best-effort value. SearchHit.data is optional at the protocol level because some services omit stored fields by default. When returned SearchHit.data or SearchResult.raw references adapter-owned objects, callers should treat them as read-only. The portable protocol is intentionally narrow: method shapes, document identity by index/name/id, object-valued payloads, basic structured filter transport, type restrictions, one-field sorting, paginated results, and the broad error boundary above. All protocol methods are synchronous. ensure_index() requires a settings mapping; use {} for no settings because None is not part of the protocol. Adapter-specific behavior is part of each adapter's public contract, not an omission from SearchBackend. That includes exact settings support, grouped-filter semantics, filter_expression precedence, non-DevSearch lookup/sort grammar beyond top-level data fields, negative limit/offset handling, duplicate IDs inside one batch, batch atomicity, runtime validation, settings merge-or-replace behavior, concurrency guarantees, concrete exception classes, stable ordering for equal scores, and value serialization beyond serializer-compatible object values. Adapters may validate inputs more strictly than the dataclasses do. filter_expression unsupported-feature errors are adapter-specific; DevSearch uses NotImplementedError. list_document_ids() returns the same GeneralManager document ID strings passed as SearchDocument.id, for example "Project:{'id': 1}".

Backends may retain references to input document payloads unless their adapter documents copy-on-write behavior, so callers should not mutate a SearchDocument after handing it to upsert(). Unknown type labels, unknown indexes, malformed filters, invalid sort fields, and invalid pagination on non-DevSearch adapters are adapter-specific. SearchResult.raw is an optional diagnostic escape hatch with no cross-adapter stability guarantee. Values in SearchDocument.identification are object-shaped at this protocol layer; choose serializer-compatible values when the selected backend, Celery, GraphQL, or another integration serializes them. Adapters should normalize operational failures to SearchBackendError only when doing so does not hide useful backend-native context.

The backend registry functions configure_search_backend, configure_search_backend_from_settings, and get_search_backend centralize adapter selection and lookup for GraphQL, indexing tasks, and direct Python usage. configure_search_backend() sets or clears the process-local backend instance. get_search_backend() reuses that instance when present; otherwise it reads Django settings and, when settings leave search unset, installs one process-local DevSearchBackend fallback instance that later calls reuse.

DevSearch backend (service-free)

For local development, the built-in DevSearch backend stores documents in memory and supports basic term matching with per-field boosts. It is a disposable per-process projection, does not provide typo tolerance, and should not be used in production. When SEARCH_AUTO_REINDEX=True and the selected backend is DevSearch, each index is populated from configured managers by its first search in that process. Hydration is tracked per index and guarded against concurrent or nested first searches. A hydration error reaches the caller and leaves that index retryable on a later search. Directly constructed DevSearch instances remain inert until their caller passes auto_reindex=True to the constructor or calls configure_auto_reindex(True) on the instance.

After an index hydrates, the existing synchronous search invalidation path keeps that same process's projection current with normal upserts and deletes. Writes, asynchronous invalidation, or search_index --reindex work performed in another process cannot update a running DevSearch backend. Restart the serving process so its next search builds a fresh projection. Structured filters use AND within one mapping and OR across a sequence of mappings. For exact and in checks against list/tuple/set document fields, collection filter values match on intersection; scalar in filter values do not match collection-valued document fields. DevSearch writes documents into the index_name argument, stores SearchDocument.index without validating it, and scopes document IDs to that one in-memory index. Duplicate IDs inside one upsert() call are processed in order, so the last document wins. Deletes are best-effort and duplicate IDs are harmless. ensure_index() is idempotent and replaces the stored settings mapping for the index. Operations are not transactional; mutations completed before an exception remain in memory. Query matching lowercases and splits strings on whitespace. It does not stem, parse phrases, or perform fuzzy matching. Indexed tokens are built from every top-level SearchDocument.data value: None produces no tokens, strings split on whitespace, lists/tuples/sets are processed recursively, and all other values become str(value).lower().split(). Dict values are not traversed and are tokenized from their string representation. Every distinct query term must equal or prefix an indexed field token for a document to match; different terms may match different fields. Empty queries match every document that passes type and structured filters. The operation order is type filtering, structured filtering, query scoring/matching, sorting, and pagination. Structured filter keys target SearchDocument.data field names and may use one lookup suffix separated by __ as in field__in: exact, lt, lte, gt, gte, contains, startswith, endswith, or in; keys without a suffix use exact, and nested field traversal is not supported. DevSearch filter comparisons use the shared apply_lookup() helper: string operations are case-sensitive, missing fields behave like None, incompatible mixed-type comparisons and invalid lookup/value combinations return False, and None compares only through exact.

Scores sum matching field boosts and then multiply by index_boost when set. Results sort by score descending unless sort supplies signed raw SearchDocument.data fields. For field sorting, None and missing fields are treated as missing and kept last for ascending and descending sorts. Booleans follow Python numeric ordering because bool is an int subclass; other numbers sort numerically, and every non-numeric, non-missing value sorts by str(value). Python's stable sort preserves insertion order for otherwise equal keys. DevSearch includes stored data in returned hits, reports total before pagination, slices as results[offset:offset + limit] with negative values intentionally following Python slice behavior, stores document and settings objects by reference, returns hit data from the stored SearchDocument.data mapping, and keeps state only in the current process with no persistence or cross-process synchronization for ordinary reads/writes. Mutating returned DevSearch hit data can mutate the stored document payload. DevSearch raises NotImplementedError for filter_expression; other operational failures are not normalized and may surface as ordinary Python exceptions.

External backends

To opt into another backend, configure GENERAL_MANAGER["SEARCH_BACKEND"] or SEARCH_BACKEND in Django settings. The nested GENERAL_MANAGER value takes precedence over the top-level setting, including explicit None to clear the configured backend.

Accepted SEARCH_BACKEND values are:

  • a SearchBackend instance
  • a dotted import path to a backend instance, class, or zero-argument factory
  • a backend class or zero-argument factory callable
  • a mapping with class and optional options, where options are passed as keyword arguments
  • None, or a missing setting, to use the DevSearchBackend fallback from get_search_backend()

Import errors, factory errors, and constructor errors propagate. A mapping options value must be a mapping. A non-None setting that resolves to a non-backend object raises SearchBackendNotConfiguredError.

Your adapter can resolve the configuration via general_manager.search.config.resolve_search_config() and apply it to the backend of your choice.

Meilisearch is the primary production adapter today. Typesense and OpenSearch adapters are present as stubs for configuration compatibility. Constructing TypesenseBackend or OpenSearchBackend, or calling any backend method on an instance created for testing, raises SearchBackendNotImplementedError. Constructor arguments, index settings, and search filters are accepted only to match the SearchBackend protocol and are not inspected by the stubs.

Non-GraphQL usage

If you do not use the auto GraphQL schema, call the backend directly:

from general_manager.search.backend_registry import get_search_backend

backend = get_search_backend()
result = backend.search("global", "alpha", filters={"status": "public"})

To (re)index directly in Python:

from general_manager.search.indexer import SearchIndexer

SearchIndexer().reindex_manager(Project)

The indexer can also write or delete one instance with index_instance(instance) and delete_instance(instance). The indexer discovers configuration from the manager class search registry and writes one document per configured IndexConfig. The default document id is the same for indexing and deletion: the manager type label plus instance.identification.

Default document IDs are built with general_manager.search.utils.build_document_id(type_label, identification). It formats "type_label:normalized_identification", where normalize_identification() serializes the identification mapping as JSON with sorted keys and converts non-JSON-native values with str(). The JSON encoder uses Python defaults for every other option, so custom values need a stable __str__ for deterministic IDs. Mixed non-string key types follow json.dumps(sort_keys=True) and may raise TypeError if Python cannot compare them; default=str applies to values rather than unsupported mapping keys. The helper accepts empty type labels and labels containing colons without escaping or disambiguating them, and it does not enforce backend-specific ID character limits; adapters such as Meilisearch perform their own backend-safe normalization. For example, build_document_id("Project:Archive", {"id": 1}) starts with Project:Archive:. Field extraction for indexed data uses extract_value(obj, field_path): path components are separated by __, Bucket instances are traversed before list/tuple/set collections, then mappings use key lookup, then objects use attributes. Only Bucket, list, tuple, and set values are expanded; arbitrary iterables are treated as plain objects. Buckets and supported collections apply the remaining path to each item and return a list; concrete bucket iteration defines item order and shape, nested collections produce nested lists rather than flattened values, and set ordering follows the set's own iteration order. Missing paths return None; mapping keys or attributes present with value None are indistinguishable from missing values after lookup. Empty paths return the normalized root object itself without collection traversal, so non-manager buckets and collections are returned unchanged, while a root GeneralManager becomes its exact identification mapping object as stored on the instance, such as {"id": 1} for a single-id manager. Mapping lookup uses the string path component with mapping.get() and does not fall through to attribute lookup after a missing key. Empty path components are literal key or attribute names: "a__" looks for an empty key or attribute after a, and "__a" starts with an empty key or attribute. Attribute traversal uses hasattr() followed by getattr(), so property AttributeError can look like a missing attribute and other descriptor exceptions propagate. Final GeneralManager values become their identification mapping. Exceptions from JSON serialization, bucket iteration, supported collection iteration, or property/descriptor access propagate unchanged. Bucket refers to general_manager.bucket.Bucket, and GeneralManager refers to manager instances from general_manager.manager.GeneralManager.

reindex_manager(Project) ensures every configured index and upserts current documents grouped by index, but it does not delete stale backend documents. Use reindex_manager_index(Project, "global") when a reconciler or maintenance job needs stale cleanup for one manager/index pair. It returns the number of current documents serialized, lists existing ids with backend.list_document_ids("global", types=[get_type_label(Project)]), upserts current documents before stale deletion, and deletes only stale ids reported for that manager type.

Indexer methods return without action when a manager has no search configuration, except reindex_manager_index() raises MissingIndexConfigurationError when the manager is search-enabled but the requested index name is not configured. Indexer operations are not atomic across indexes or phases; earlier backend writes remain if a later step fails. Backend and serialization errors propagate.

Duplicate IndexConfig.name entries are not deduplicated consistently across operations: single-instance indexing repeats the configured work, full manager reindexing emits one backend upsert per first-seen index name while still serializing one document per duplicate config and instance, and reindex_manager_index() uses the first matching config for the requested name.

Operations and troubleshooting

  • Missing filters: filter fields must be listed in IndexConfig.filters.
  • Sorting fails: sort fields must be listed in IndexConfig.sorts and be marked sortable by the backend.
  • No results: verify the index was created (search_index) and reindexed after config changes.
  • Permission gaps: ensure get_read_permission_filter() returns correct rules and that your GraphQL context is populated.
  • Meilisearch auth errors: confirm API key and URL are in sync with the configured backend settings.

Meilisearch setup (local or production)

Use the Meilisearch backend by configuring the search backend and connection settings. The backend reads MEILISEARCH_URL and optional MEILISEARCH_API_KEY.

GENERAL_MANAGER = {
    "SEARCH_BACKEND": {
        "class": "general_manager.search.backends.meilisearch.MeilisearchBackend",
        "options": {
            "url": "http://127.0.0.1:7700",
            "api_key": None,
        },
    }
}

Local Docker example (dev keyless):

docker run --rm -p 7700:7700 --name meilisearch \
  -e MEILI_NO_ANALYTICS=true \
  getmeili/meilisearch:v1.30.0

Production notes: - Set MEILISEARCH_API_KEY (or a master key) and pass the same value in your deployment environment. - Ensure your index settings are created with python manage.py search_index and reindex with --reindex after schema changes. - Keep the Meilisearch image pinned to a known-good version to avoid drift.

For a least-privilege deployment, construct the backend with a search client and a separate read-only metadata client:

from meilisearch import Client

from general_manager.search.backends.meilisearch import MeilisearchBackend

backend = MeilisearchBackend(
    client=Client("http://127.0.0.1:7700", search_key),
    metadata_client=Client("http://127.0.0.1:7700", metadata_key),
)

The search client remains the only client used for searches, index hydration, and writes; it needs search and indexes.get actions because the SDK resolves the index before searching. The metadata client is used only for the authoritative index settings read shared by ordering and completeness checks; it needs indexes.get and settings.get. Both clients must address the same trusted server and index. When searchCutoffMs is absent, the metadata client also needs the version action. An absent cutoff is accepted only when that version response has a stable ASCII-semver pkgVersion in the supported 1.7.x range, which predates cutoff support. Missing, malformed, or newer version evidence fails closed rather than treating a short page as complete. If both clients use the same key, omit metadata_client.

Adapter behavior: - The backend stores the original GeneralManager document id in gm_document_id and writes a deterministic Meilisearch-safe id primary key. Safe ids matching ^[A-Za-z0-9_-]{1,511}$ are kept unchanged. Every other id, including empty strings, whitespace, slashes, and Unicode, becomes "gm_" + sha256(str(id)).hexdigest(). The value is stable and collision-resistant, but not reversible. Deletions normalize incoming GeneralManager ids the same way before calling Meilisearch. - ensure_index() applies searchable_fields, filterable_fields, and sortable_fields when those settings are iterable values such as lists, tuples, or sets. Strings and bytes are treated as invalid scalar values and ignored. Values inside an accepted iterable are converted with str(...). Index creation uses primary key id. Before submitting settings tasks, the adapter reads the index's effective settings and skips equivalent writes: ranking rules and searchable fields compare in order, while plain filterable and sortable fields compare by membership. Identity shadow fields are added only for identity names listed in filterable_fields. Omitted searchable, filterable, and sortable settings stay unmanaged; an explicit empty list clears that setting. Ranking rules are always normalized with sort first. Each changed setting is awaited before the next update or return, and settings-read or task failures propagate so a later call can retry the remaining differences. - Numeric components of searchable string values are also indexed as separate tokens. Porsche_P0375_Strangpressprofile can be found with 0375 or 375, while its stored name remains unchanged. Only complete ASCII digit runs and their zero-stripped form are added (000 also yields 0), not arbitrary substrings such as 75. Normal Meilisearch prefix and typo rules still apply. These field-scoped tokens live under the reserved gm_numeric_tokens envelope; filter-/sort-only fields do not become searchable and existing source-field permissions still apply. This behavior is specific to the Meilisearch adapter. Rebuild existing persistent indexes after upgrading, for example python manage.py search_index --index global --reindex; applying settings alone does not generate tokens for existing documents. - upsert(index, []) still ensures the index exists but does not submit an add_documents task. Non-empty documents are stored with id, gm_document_id, type, identification, nested data, and non-reserved document.data keys copied to the top level. Reserved top-level keys are id, gm_document_id, type, identification, data, gm_identity, and gm_numeric_tokens. delete(index, []) is a no-op and does not access Meilisearch; delete(index, [""]) treats the empty string as a real document id and deletes its normalized value. - Clients exposing wait_for_task() use that method with a default timeout of 5000 milliseconds (five seconds). Configure it with MeilisearchBackend(task_timeout_in_ms=20_000) to wait up to 20 seconds. This option is forwarded as the client’s timeout_in_ms argument. Preconfigured clients with the legacy wait_for_task(task_uid) signature remain supported, but use their own timeout behavior. Clients without wait_for_task() fall back to polling get_task() every 0.1 seconds with exponential backoff capped at one second. Polling stops on succeeded, failed, or canceled; unknown or missing statuses keep polling until the five-second timeout raises MeilisearchTaskFailedError. Mapping task responses read status and error keys; object task responses read attributes with the same names. Task payloads without a task UID are treated as already complete. - list_document_ids() reads id, gm_document_id, and type in pages of 1000. types=None and types=[] include every type. Type filtering is exact, gm_document_id is preferred over id, falsey gm_document_id values such as "" fall back to id, duplicates collapse into a set, and malformed documents without either id field are ignored. - Structured filters are translated to equality and in clauses. Fields inside one mapping are ANDed, multiple mappings are ORed, types are ORed and then ANDed with structured filters, and values are rendered with str(...) after escaping backslashes and double quotes. The exact comparison syntax is field = "escaped_value" for every value type: None renders as "None", booleans as "True"/"False", and numbers as decimal strings. Empty in lists produce (). A raw filter_expression takes precedence over structured filters and types, so types is ignored when filter_expression is provided. - sort is a signed field sequence. The backend appends :asc or :desc for each term and does not validate, split, or escape field names. - Search hit parsing is defensive: malformed hit entries are skipped and missing id/gm_document_id and type become empty strings, missing identification and data become empty mappings, and missing _rankingScore becomes None in SearchHit.

Meilisearch test recipe

To run the optional Meilisearch integration test locally or in CI, start a Meilisearch container and provide the URL via MEILISEARCH_URL (and optionally MEILISEARCH_API_KEY):

docker run --rm -p 7700:7700 --name meilisearch-test \
  -e MEILI_NO_ANALYTICS=true \
  getmeili/meilisearch:v1.30.0

Then run the test with:

MEILISEARCH_URL=http://127.0.0.1:7700 python -m pytest \
  tests/integration/test_meilisearch_search.py

See query workload budgets for independent field-authorization and source-candidate limits.