Keyword & Hybrid Search

Find records by meaning, by exact words, or both at once, with the same access control in every mode. Available on all plans; SDK version 1.6 and later.

Which mode to use

Vector search is great at meaning and weak at exact terms: a product code or a ticket id has no meaning an embedding can capture. Keyword search is the opposite. Hybrid runs both and merges the rankings, so a question finds the passage that answers it and the one that mentions its exact error code.

modeFindsNeedsGood for
vectorRecords that mean the same as the queryA query vectorQuestions in natural language, paraphrases, other languages
keywordRecords that contain the query's wordsOnly text: no embeddingError codes, product names, ticket ids, people's names
hybridBoth, fused into one rankingA query vector and textRAG and assistant memory: the best default when you have both

All three go through POST /v1/search with a mode field (see the API reference). Vector is the default, and sending text without a mode doesn't change it, so existing code behaves exactly as before.

Keyword search

Matches the words in each record's metadata text field, which is what embedDocumentChunks and the examples in these docs already store. Results are ranked with BM25: rarer words and shorter passages count for more.

TypeScript
import { SwarmV } from "@swarmv/sdk";

const swarmv = new SwarmV();   // SWARMV_API_KEY from the environment

// No embedding needed: matches the words in each record's metadata "text".
const hits = await swarmv.searchText("ERR_4021 payment", { topK: 5 });
for (const h of hits) console.log(h.id, h.score, h.metadata?.text);
Python
from swarmv import SwarmV

swarmv = SwarmV()

hits = swarmv.search_text("ERR_4021 payment", top_k=5)
for h in hits:
    print(h.id, h.score, (h.metadata or {}).get("text"))

Hybrid search

Pass the query vector and the query text. Both searches run in parallel; their rankings are merged with Reciprocal Rank Fusion, which uses each record's position in each list rather than raw scores, so nothing needs calibrating. keywordWeight (keyword_weight) shifts the balance: try 0.3 for conversational questions and 0.7 when queries are mostly codes or names.

TypeScript
const question = "Why did the payment fail with ERR_4021?";
const vector = await embedder.embedQuery(question);

// Vector and keyword matches, fused. keywordWeight: 0 = vector only, 1 = keyword only (default 0.5).
const hits = await swarmv.hybridSearch(vector, question, { topK: 8, keywordWeight: 0.5 });
for (const h of hits) {
  console.log(h.id, h.match, h.score);   // match: "vector" | "keyword" | "both"
}
Python
question = "Why did the payment fail with ERR_4021?"
vector = embedder.embed_query(question)

hits = swarmv.hybrid_search(vector, question, top_k=8, keyword_weight=0.5)
for h in hits:
    print(h.id, h.match, h.score)
curl
curl -X POST https://api.swarmv.dev/v1/search \
  -H "Authorization: Bearer $SWARMV_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"mode": "hybrid", "query": [0.12, -0.04, 0.33], "text": "refund policy", "top_k": 5}'

Results

Keyword and hybrid results add score (higher is better; compare within one response only) and match: vector, keyword or both. distance is there when the record was also a vector match. Vector-mode results are unchanged.

Hybrid response
[
  { "id": "4021", "score": 0.0325, "match": "both",    "distance": 0.21, "metadata": { "text": "Payment failed with ERR_4021 ..." } },
  { "id": "5310", "score": 0.0164, "match": "vector",  "distance": 0.34, "metadata": { "text": "Card declined: what to check ..." } },
  { "id": "7702", "score": 0.0161, "match": "keyword",                   "metadata": { "text": "ERR_4021 runbook ..." } }
]

How words match

  • Case and accents don't matter: "conexao" finds "Conexão". Words are split on anything that isn't a letter or digit, so ERR_4021 matches as "err" and "4021".
  • Common words are ignored in English and Portuguese ("the", "what", "de", "para"…), so full questions work as queries.
  • All words first: records containing every query word rank first. If fewer than top_k do, records with any of the words fill the rest.
  • No stemming yet: "invoice" doesn't match "invoices". In hybrid mode the vector side covers this.
  • Your text is never parsed as query syntax: quotes, OR, NEAR, * and the like are treated as plain words or ignored.

Filters

Every search mode takes a metadata filter. Conditions on different fields must all hold; a field can have several operators (all must hold too). Metadata is stored as text, so a number in the filter compares numerically, and text compares character by character, which sorts ISO-8601 dates such as 2026-10-01 correctly. For dates, store Unix seconds or ISO dates and filter the same way.

OperatorMatchesExample
value or $eqEquals{ "lang": "pt" }
$neNot equal (also matches records without the field){ "status": { "$ne": "draft" } }
$in / $ninOne of / none of, up to 100 values{ "lang": { "$in": ["pt", "es"] } }
$gt $gte $lt $lteRanges: numbers numerically, text byte-wise{ "year": { "$gte": 2020, "$lt": 2025 } }
$existsHas (or doesn't have) the field{ "deleted_at": { "$exists": false } }
$orAny of up to 8 filters; other fields apply to each{ "$or": [{ "team": "web" }, { "team": "mobile" }] }
TypeScript
// Last 30 days, in Portuguese or Spanish, not drafts.
const since = Math.floor(Date.now() / 1000) - 30 * 86400;
const hits = await swarmv.search(vector, {
  topK: 10,
  filter: {
    created_at: { $gte: since },          // store created_at as Unix seconds
    lang: { $in: ["pt", "es"] },
    status: { $ne: "draft" },
  },
});

// Either of two teams' documents, from 2024 on (other fields apply to every branch).
await swarmv.searchText("roadmap", { filter: { $or: [{ team: "web" }, { team: "mobile" }], year: { $gte: 2024 } } });
Python
import time

since = int(time.time()) - 30 * 86400
hits = swarmv.search(vector, top_k=10, filter={
    "created_at": {"$gte": since},
    "lang": {"$in": ["pt", "es"]},
    "status": {"$ne": "draft"},
})

Only metadata values up to 128 bytes can be filtered: longer ones (such as a chunk's text) aren't kept in the fast index. A filter that can't be understood is refused with 400 invalid_filter and a message saying what to fix. Filters narrow results after access control: they never widen what a user can see.

Access control and freshness

Keyword matches follow the same rule as vector search: with a user token (or auth_user_id), only records in containers that user can view are considered. The rule is applied inside the search, not to the results afterwards. See Authentication.

TypeScript
// A user token searches only what that user can view, in every mode.
const agent = new SwarmV({ token: await swarmv.generateUserToken("user:dave", { scopes: ["vector:read"] }) });
const hits = await agent.searchText("board minutes");   // nothing from containers dave can't view

New writes become keyword-searchable once merged, usually within seconds. With fresh: true, writes that aren't merged yet match too, as in vector search.

Limits

  • Query text up to 2,000 characters; the first 16 distinct words are used.
  • Only the metadata field text is indexed. Metadata is limited to 8 KB per record, so very long passages should be split into chunks (which also improves both kinds of search).
  • Metadata filters apply to keyword matches too, after matching, so a filtered keyword or hybrid search can return fewer than top_k results.
  • Each search counts as one read, whatever the mode.