Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Semantic search

Build a small searchable text index with the on-device lexical embedder, run text queries, filter results with Eq, and read the scores.

Build a small, searchable text index end to end: create a collection backed by the on-device AlgentaMultigramEmbedder, add documents, run text queries, then narrow the results with an equality filter. Every step runs locally.

This assumes Telys is installed and you are signed in (pipx install telys then telys login). If not, do the Quickstart: your first collection first, or see Install Telys. The examples need the signed runtime and the native kernel; telys login installs both.

The embedder: lexical, on-device

AlgentaMultigramEmbedder hashes character unigrams, bigrams, and trigrams and L2-normalizes each block. It is lexical: similarity reflects shared surface text — words, spelling, morphology — not learned meaning. It runs on-device with no model download, a fast default for keyword-flavoured search. For synonym or paraphrase matching, bring a dense model instead (see Lexical vs dense embeddings).

Its dimension comes from the native kernel, so read it from the profile rather than hard-coding a number:

from telys.embedding import AlgentaMultigramEmbedder

emb = AlgentaMultigramEmbedder()
print(emb.profile.model_id, emb.profile.dimension, emb.profile.distance)

Expected output:

multigram 384 ip
1

Build the index

Create a collection, declare category as a filter column so you can filter on it later, add documents with add_texts, and persist with save().

from telys import Telys, Eq
from telys.embedding import AlgentaMultigramEmbedder

t = Telys("./search-demo")
emb = AlgentaMultigramEmbedder()

col = t.create_collection(
    "faq",
    dim=emb.profile.dimension,
    partition_by="scope",
    embedder=emb,
    filter_columns=("category",),
)

col.add_texts(
    texts=[
        "Reset your password from the account settings page.",
        "Export your data as CSV or JSON from the dashboard.",
        "Upgrade to the Pro plan for priority support.",
        "Delete your account permanently under the Danger Zone.",
    ],
    ids=["faq-1", "faq-2", "faq-3", "faq-4"],
    metadata=[
        {"scope": "faq", "category": "account"},
        {"scope": "faq", "category": "data"},
        {"scope": "faq", "category": "billing"},
        {"scope": "faq", "category": "account"},
    ],
)
col.save()

filter_columns declares which metadata fields Telys stores as indexed columns. with_metadata=True returns exactly those fields (undeclared keys are not stored); you can where-filter on the partition key or a declared filter column.

2

Run a text query

search_text embeds the query with the same embedder and returns the closest ids, their scores, and (with with_metadata=True) each hit's payload.

hits = col.search_text(
    "how do I change my password?",
    top_k=3,
    with_metadata=True,
)
print(hits)

Expected output:

{'ids': ['faq-1', 'faq-4', 'faq-2'],
 'scores': [0.62, 0.18, 0.09],
 'metadata': [{'category': 'account'},
              {'category': 'account'},
              {'category': 'data'}]}

faq-1 wins because it shares the most surface text with the query ("password", "your"). The other rows still score above zero on incidental n-gram overlap; that is expected for a lexical embedder.

3

Filter with Eq

Pass where=Eq(column, value) to restrict the search to rows whose column equals a value. Here it keeps only the account FAQs.

hits = col.search_text(
    "how do I close my account?",
    top_k=3,
    where=Eq("category", "account"),
    with_metadata=True,
)
print(hits)

Expected output:

{'ids': ['faq-4', 'faq-1'],
 'scores': [0.57, 0.14],
 'metadata': [{'category': 'account'},
              {'category': 'account'}]}

Only the two account rows are eligible, so faq-2 (data) and faq-3 (billing) are excluded before scoring even though you asked for top_k=3.

Reading the scores

  • Scores are the inner product of L2-normalized vectors — cosine similarity. 1.0 is an exact match; higher means more similar.

  • ids, scores, and metadata are three parallel lists, already sorted best-first.

  • Because the embedder is lexical, a query and a document that mean the same thing but share no characters (pure synonyms) score low. If that is your use case, switch embedders — see Choosing an embedder.

  • Filtering happens before scoring, so a filtered query can return fewer than top_k rows when the filter is narrow.

Next steps

Last updated

Was this helpful?