SearchFn
Reference

Client API

Create and configure a SearchFn client for in-app search.

Creating a Client

Use createSearchClient() to create a search client. The client validates inputs, applies defaults, and delegates to the underlying adapter.

import { createSearchClient } from "@searchfn/client";
import { MemoryAdapter } from "@searchfn/adapters";

const client = createSearchClient({
  adapter: new MemoryAdapter(),
  defaults: {
    limit: 20,
    limitPerResource: 10,
    fuzzy: true,
  },
});

Configuration

OptionTypeDescription
adapterSearchAdapterRequired. The search adapter to use.
defaults.limitnumberDefault result limit for search calls.
defaults.limitPerResourcenumberDefault per-resource limit for searchAll calls.
defaults.fuzzyboolean | numberEnable fuzzy matching by default. Pass a number for edit distance threshold.

Methods

initialize

Declare resources and their searchable fields. Call before indexing.

await client.initialize?.({
  resources: [
    { name: "tasks", searchFields: ["title", "description"] },
    { name: "notes", searchFields: ["content", "tags"] },
  ],
});
ParameterTypeDescription
resourcesInitializeResourceConfig[]Array of resource declarations with name and searchable fields.

index

Index a batch of documents into a resource.

await client.index({
  resource: "tasks",
  documents: [
    { id: "t-1", fields: { title: "Ship docs", description: "Write the guide" } },
    { id: "t-2", fields: { title: "Fix bug", description: "Handle retries" } },
  ],
});
ParameterTypeDescription
resourcestringTarget resource name.
documentsSearchDocument[]Documents to index. Each has an id and a fields record.
signalAbortSignal?Optional abort signal.

Search a single resource. Returns matching document IDs.

const ids = await client.search({
  resource: "tasks",
  query: "docs",
  limit: 10,
  fuzzy: true,
  fields: ["title"],
  fieldBoosts: { title: 2.0 },
});
ParameterTypeDescription
resourcestringResource to search.
querystringSearch query string.
fieldsstring[]?Restrict search to specific fields.
limitnumber?Max results to return.
fuzzyboolean | number?Enable fuzzy matching.
fieldBoostsRecord<string, number>?Per-field relevance boosts (adapter support required).
signalAbortSignal?Optional abort signal.

Returns: Promise<Array<string | number>>

searchAll

Search across multiple resources. Returns results with resource name, ID, and score.

const results = await client.searchAll({
  query: "launch",
  resources: ["tasks", "notes"],
  limitPerResource: 5,
});

// [{ resource: "tasks", id: "t-3", score: 1.5 }, { resource: "notes", id: "n-1", score: 0.8 }]
ParameterTypeDescription
querystringSearch query string.
resourcesstring[]?Resources to search. Omit to search all initialized resources.
fieldsstring[]?Restrict search to specific fields.
limitnumber?Max total results.
limitPerResourcenumber?Max results per resource.
fuzzyboolean | number?Enable fuzzy matching.
fieldBoostsRecord<string, number>?Per-field relevance boosts.
signalAbortSignal?Optional abort signal.

Returns: Promise<SearchAllResult[]> where each result has resource, id, and score.

If the adapter does not implement searchAll natively, the client runs per-resource searches concurrently and merges results with deterministic score-based ordering.

remove

Remove documents from a resource by ID.

await client.remove({
  resource: "tasks",
  ids: ["t-1", "t-2"],
});

clear

Remove all documents from a resource.

await client.clear("tasks");

dispose

Release resources held by the adapter. After disposal, call initialize() again before further operations.

await client.dispose();

adapterInfo

Returns the adapter name and declared capabilities.

const info = client.adapterInfo();
// { name: "memory", capabilities: { persistent: false, searchAll: true, fuzzy: true, fieldBoosts: true } }

Convenience Constructors

Shorthand for common adapter setups:

import { createMemorySearchClient, createIndexedDbSearchClient } from "@searchfn/client";

const memoryClient = createMemorySearchClient({
  adapterConfig: { pipeline: { /* custom pipeline */ } },
  defaults: { limit: 20, fuzzy: true },
});

const browserClient = createIndexedDbSearchClient({
  adapterConfig: { dbName: "my-app-search", cache: { terms: 4096 } },
  defaults: { limit: 20, fuzzy: true },
});

Validation Limits

The client validates all inputs before passing them to the adapter:

LimitDefault
Max query length1000 characters
Max limit10,000
Max limitPerResource1,000
Max resources in searchAll50
Max documents per index batch10,000

Validation failures throw SearchClientValidationError with a code and optional details.path.