Skip to content

feat: implement the Typesense adapter - #39

Open
blarghmatey wants to merge 26 commits into
openedx:mainfrom
mitodl:tmacey/typesense-adapter
Open

blarghmatey wants to merge 26 commits into
openedx:mainfrom
mitodl:tmacey/typesense-adapter

Conversation

@blarghmatey

Copy link
Copy Markdown
Contributor

Stacked PR: built on top of #33 (which is on #35 and #32). Only ece1508, 8f12c35 and 7631831 belong to this PR; review them in the diff against #33's head. I'll rebase as the stack merges.

Description

Implements the Typesense adapter for the contract in ADR 0002, alongside the Meilisearch adapter from #33. It is registered from AppConfig.ready() next to Meilisearch.

The logical index is a Typesense alias. Collections are named like rebuild targets (<index>_v<version>_<generation>), including the first one reconcile creates, so the initial collection and every later rebuild share one naming scheme and one leftover cleanup. commit_rebuild repoints the alias and deletes the collection it replaced.

Typesense's := does not compare strings exactly. It compares tokenized values, so org:=[ab] also matches a-b, AB and a_b, and e matches é. Each filterable string field therefore gets a hidden, unstored <field>__exact companion holding the SHA-256 of each value, and string filters render against the digests. id is already compared exactly and is filtered directly. A filter on a field that is neither id nor declared filterable is refused.

A scoped key can't name a collection, and one search_api_key serves every use case on a backend. So every document carries a hidden __index digest of its logical index, and every minted key filters on it. A key minted for one index found nothing in another index on the same server. Keys also embed exclude_fields for the companions, because store: false hides a top-level field but not a nested one.

Field rendering follows the schema's capabilities, with a few rules the schema doesn't show. Each was checked against Typesense 30.2 and is listed in the module docstring:

  • An indexed child of an unindexed object is silently not indexed, so an object is indexed whenever a descendant is.
  • query_by on an object fails if any declared child is unindexed, so every descendant of a searchable object is indexed. That matches Meilisearch, where a searchable object searches all of its leaves.
  • An indexed object indexes undeclared keys and adds each one to the collection's fields. additional_properties objects get a <name>\..* string pattern after the declared fields, and reconcile ignores the fields Typesense adds under them.
  • datetime is int64. Typesense indexes float at single precision, where 1767225600 and 1767225601.5 compare equal, so float POSIX seconds are truncated to whole seconds the same way int(datetime.timestamp()) truncates.

reconcile compares type, index, optional, facet, sort and store per field. It drops and re-adds each changed field in its own PATCH, parents before children (Typesense rejects one request that changes an object and its child), and adds new fields together. enable_nested_fields can't be altered on a live collection, so a change there raises and asks for a rebuild. A live collection from another schema version is left alone, with a warning, so readers stay on the old index until the migration's rebuild commits. A companion added to a collection that already holds documents is empty for those documents, so reconcile warns that a same-version rebuild is needed.

Writes are synchronous: upsert imports the batch with action=upsert and returns a complete receipt, and check_write makes no request. The import answers 200 even when it rejects documents, so every result line is checked and a rejection raises WriteFailed naming positions and status codes only (a failed line can echo the document). delete renders primary keys as id:=[...] filters in batches of 200, since the ids travel in the query string and Typesense refused a 454 KB one. The shared transport gains request_lines for the JSON Lines import.

The rebuild lifecycle:

  • commit_rebuild checks that the target exists before repointing the alias. Typesense accepts an alias to a missing collection, so a stale rebuild whose target a newer one cleaned up would otherwise delete the live collection.
  • begin_rebuild never deletes a collection that any alias points at. Another index can share the prefix (x and x_v1_y).
  • abort_rebuild leaves the target alone when the alias already points at it, because a commit that repointed the alias and then failed to delete the old collection has made the target live.
  • reconcile refuses a bare collection named like the index, since Typesense won't create an alias over it.

mint_credential derives the key from search_api_key (Typesense refuses one derived from the admin key). A known-answer test pins it to typesense-python 2.2.0's generate_scoped_search_key output. validate_schema rejects more than 16 searchable fields, because Typesense ranks query_by_weights into 16 levels and a 17th would tie. It also rejects declared names that collide with the hidden fields. The ADR said validate_schema is a no-op for Typesense, so the last commit records these limits and the hidden fields in ADR 0002. search raises NotImplementedError until the query API lands, as it does for Meilisearch.

Known differences and limits I left alone:

  • float fields are single precision on Typesense.
  • A float filter value on an integer field is rejected rather than coerced.
  • PATCH and import run under the transport's 10 s timeout, so reconcile on a large collection can time out while Typesense keeps altering it.
  • reconcile is not leased. If it races the first rebuild's commit, it can orphan the rebuilt collection until the next begin_rebuild cleans it up.

Testing instructions

  • uv run --group test pytest -q for the suite (typesense.py and transport.py at 100% branch coverage) and uv run tox -e quality for the linters.
  • Against a real engine: docker run --rm -p 8108:8108 typesense/typesense:30.2 --data-dir /tmp --api-key=devkey. Then drive TypesenseAdapter(STUDIO_LIBRARY, backend) through reconcile, upsert, delete, the rebuild lifecycle and a search with a minted key. I ran that end to end with the Studio library fixture (51 checks). Those covered exact string filters, a key that couldn't reach another index, widen its filter or outlive its expiry, and reconcile being a no-op once Typesense had added free-form fields. They also covered a parent-and-child drift patched in place, a version bump leaving the old collection alone, and a stale commit refused. Another index's collection survived prefix cleanup, and 1000 ids of about 440 bytes each were deleted in batches. CI has no Typesense service, so the backend-agnostic suite in Test cases that can run against any backend #13 is where those checks belong.

Additional Information

Closes #18

Depends on:

https://claude.ai/code/session_01FGZebDGDTv1ZTtGCEXmxTd

rpenido and others added 26 commits November 24, 2026 12:18
Typesense's import endpoint takes one document per line and answers with
one result per line, so neither the request nor the response is a single
JSON value. request_lines shares the redirect refusal, size bound and
error handling of request.

Claude-Session: https://claude.ai/code/session_01FGZebDGDTv1ZTtGCEXmxTd
The logical index is an alias over collections named like rebuild
targets, so reconcile's first collection and every later rebuild share
one naming scheme and one leftover cleanup. Writes are synchronous and
return complete receipts; the import's per-line results are checked
because Typesense answers 200 when it rejects documents.

Typesense's := ignores case, punctuation and accents in strings, so
filterable string fields get a hidden SHA-256 companion and string
filters match on the digests. Scoped keys cannot name a collection, so
every document carries a hidden digest of its index and every minted
key filters on it, alongside the tenant filter, query_by and
query_by_weights. The key matches typesense-python's output byte for
byte. search() waits for the query API, as it does for Meilisearch.

Claude-Session: https://claude.ai/code/session_01FGZebDGDTv1ZTtGCEXmxTd
validate_schema is not a no-op for Typesense: weights rank into 16
levels and the adapter reserves its hidden field names. String filters
and scoped keys on Typesense also need the hidden fields to be exact and
bound to one index.

Claude-Session: https://claude.ai/code/session_01FGZebDGDTv1ZTtGCEXmxTd
@openedx-webhooks openedx-webhooks added the open-source-contribution PR author is not from Axim or 2U label Dec 1, 2026
@openedx-webhooks

Copy link
Copy Markdown

Thanks for the pull request, @blarghmatey!

This repository is currently maintained by @ormsbee.

Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review.

🔘 Get product approval

If you haven't already, check this list to see if your contribution needs to go through the product review process.

  • If it does, you'll need to submit a product proposal for your contribution, and have it reviewed by the Product Working Group.
    • This process (including the steps you'll need to take) is documented here.
  • If it doesn't, simply proceed with the next step.
🔘 Provide context

To help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:

  • Dependencies

    This PR must be merged before / after / at the same time as ...

  • Blockers

    This PR is waiting for OEP-1234 to be accepted.

  • Timeline information

    This PR must be merged by XX date because ...

  • Partner information

    This is for a course on edx.org.

  • Supporting documentation
  • Relevant Open edX discussion forum threads
🔘 Get a green build

If one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green.

Details
Where can I find more information?

If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources:

When can I expect my changes to be merged?

Our goal is to get community contributions seen and reviewed as efficiently as possible.

However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:

  • The size and impact of the changes that it introduces
  • The need for product review
  • Maintenance status of the parent repository

💡 As a result it may take up to several weeks or months to complete a review and merge your PR.

@mphilbrick211 mphilbrick211 moved this from Needs Triage to Ready for Review in Contributions Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

open-source-contribution PR author is not from Axim or 2U

Projects

Status: Ready for Review

Development

Successfully merging this pull request may close these issues.

Implement Typesense

3 participants