Repository navigation
feat: implement the Typesense adapter - #39
blarghmatey wants to merge 26 commits into
Conversation
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
|
Thanks for the pull request, @blarghmatey! This repository is currently maintained by 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 approvalIf you haven't already, check this list to see if your contribution needs to go through the product review process.
🔘 Provide contextTo 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:
🔘 Get a green buildIf one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green. DetailsWhere 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:
💡 As a result it may take up to several weeks or months to complete a review and merge your PR. |
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 onereconcilecreates, so the initial collection and every later rebuild share one naming scheme and one leftover cleanup.commit_rebuildrepoints the alias and deletes the collection it replaced.Typesense's
:=does not compare strings exactly. It compares tokenized values, soorg:=[ab]also matchesa-b,ABanda_b, andematchesé. Each filterable string field therefore gets a hidden, unstored<field>__exactcompanion holding the SHA-256 of each value, and string filters render against the digests.idis already compared exactly and is filtered directly. A filter on a field that is neitheridnor declaredfilterableis refused.A scoped key can't name a collection, and one
search_api_keyserves every use case on a backend. So every document carries a hidden__indexdigest 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 embedexclude_fieldsfor the companions, becausestore: falsehides 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:
query_byon 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.additional_propertiesobjects get a<name>\..*string pattern after the declared fields, andreconcileignores the fields Typesense adds under them.datetimeisint64. Typesense indexesfloatat single precision, where 1767225600 and 1767225601.5 compare equal, so float POSIX seconds are truncated to whole seconds the same wayint(datetime.timestamp())truncates.reconcilecomparestype,index,optional,facet,sortandstoreper field. It drops and re-adds each changed field in its ownPATCH, parents before children (Typesense rejects one request that changes an object and its child), and adds new fields together.enable_nested_fieldscan'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, soreconcilewarns that a same-version rebuild is needed.Writes are synchronous:
upsertimports the batch withaction=upsertand returns a complete receipt, andcheck_writemakes no request. The import answers 200 even when it rejects documents, so every result line is checked and a rejection raisesWriteFailednaming positions and status codes only (a failed line can echo the document).deleterenders primary keys asid:=[...]filters in batches of 200, since the ids travel in the query string and Typesense refused a 454 KB one. The shared transport gainsrequest_linesfor the JSON Lines import.The rebuild lifecycle:
commit_rebuildchecks 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_rebuildnever deletes a collection that any alias points at. Another index can share the prefix (xandx_v1_y).abort_rebuildleaves 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.reconcilerefuses a bare collection named like the index, since Typesense won't create an alias over it.mint_credentialderives the key fromsearch_api_key(Typesense refuses one derived from the admin key). A known-answer test pins it to typesense-python 2.2.0'sgenerate_scoped_search_keyoutput.validate_schemarejects more than 16 searchable fields, because Typesense ranksquery_by_weightsinto 16 levels and a 17th would tie. It also rejects declared names that collide with the hidden fields. The ADR saidvalidate_schemais a no-op for Typesense, so the last commit records these limits and the hidden fields in ADR 0002.searchraisesNotImplementedErroruntil the query API lands, as it does for Meilisearch.Known differences and limits I left alone:
floatfields are single precision on Typesense.PATCHand import run under the transport's 10 s timeout, soreconcileon a large collection can time out while Typesense keeps altering it.reconcileis not leased. If it races the first rebuild's commit, it can orphan the rebuilt collection until the nextbegin_rebuildcleans it up.Testing instructions
uv run --group test pytest -qfor the suite (typesense.pyandtransport.pyat 100% branch coverage) anduv run tox -e qualityfor the linters.docker run --rm -p 8108:8108 typesense/typesense:30.2 --data-dir /tmp --api-key=devkey. Then driveTypesenseAdapter(STUDIO_LIBRARY, backend)throughreconcile,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, andreconcilebeing 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