Scrypath.Schema behaviour (scrypath v0.3.21)

Copy Markdown View Source

Metadata-only schema declaration support for use Scrypath.

Scrypath.Schema validates declaration options, stores normalized metadata on the schema module, and exposes that metadata through __scrypath__/1.

The initial declaration surface supports these __scrypath__/1 keys:

  • :config
  • :fields
  • :filterable
  • :faceting
  • :sortable
  • :settings
  • :document_id
  • :document_source
  • :backend
  • :tenant_field
  • :fan_outs
  • :preload

The macro does not generate runtime APIs such as search/2 or reindex/1.

Hooks

use Scrypath makes the schema module implement the Scrypath.Schema behaviour. Define these callbacks on the schema module to change how Scrypath builds and loads documents:

  • search_document/1: build the document yourself, for computed or nested values that fields: cannot express.
  • search_document_id/1: use a document id other than the document_id: field.
  • search_records/2: load records by document id yourself, for custom ids, soft deletes or preloads that a plain Ecto query cannot express.

All three are optional. When a hook is absent, Scrypath projects the declared fields:, reads the document_id: field and loads records with an Ecto query.

A hook defined at the wrong arity, such as search_records/1, triggers a compile warning, because Scrypath would otherwise ignore it at runtime.

If the schema module uses @impl for any other behaviour, Elixir expects @impl on every callback, so mark the hooks with @impl Scrypath.Schema.

Summary

Callbacks

Builds the search document for record.

Returns the search document id for record.

Loads the current records for document_ids.

Callbacks

search_document(record)

(optional)
@callback search_document(record :: struct() | map()) :: map()

Builds the search document for record.

When defined, Scrypath uses the returned map instead of projecting the declared fields:. The callback must return a map. An :id (or "id") key in the map overrides the document id. When the schema declares tenant_field:, Scrypath still adds the tenant value to the document.

search_document_id(record)

(optional)
@callback search_document_id(record :: struct() | map()) :: term()

Returns the search document id for record.

When defined, Scrypath uses the returned value instead of reading the document_id: field. If you sync through Oban, also define search_records/2, because the job cannot map a custom id back to a row: without it, enqueueing an upsert raises ArgumentError before any job is inserted.

search_records(document_ids, context)

(optional)
@callback search_records(document_ids :: [term()], context :: %{repo: module() | nil}) ::
  [struct() | map()]

Loads the current records for document_ids.

Scrypath calls this during Oban sync and backfill with at most 1000 deduplicated ids per call and a context map %{repo: repo}, where repo may be nil. The callback must return a list. Ids whose records are not returned are treated as missing, so their documents are deleted from the index.

When the callback is absent, Scrypath loads records with an Ecto query on the document_id: field through the configured repo.