@plinthjs/search
Plinth's full-text search abstraction. A Builder describes a search, an Engine executes it, and a SearchManager resolves named engines. The package ships a dependency-free, in-memory CollectionEngine as the default collection driver.
Install
npm install @plinthjs/searchUsage
import { SearchManager, makeSearchable, search } from '@plinthjs/search'
const search = new SearchManager() // default engine: 'collection'
// Wrap plain records so the engine knows their index, key and document.
const posts = [
{ id: 1, title: 'Hello World', status: 'published', views: 30 },
{ id: 2, title: 'Goodbye World', status: 'draft', views: 10 },
{ id: 3, title: 'Hello Again', status: 'published', views: 20 },
].map((post) => makeSearchable(post, { index: 'posts' }))
// The `search:import` equivalent. `undefined` targets the default engine.
search.import(undefined, posts)
// The `Post::search('hello')` equivalent.
const results = search(search.engine(), 'posts', 'hello')
.where('status', 'published')
.orderBy('views', 'desc')
.take(10)
.get() // matched documents, in result order
search(search.engine(), 'posts', 'world').keys() // [1, 2]Searchable records
makeSearchable(record, options) returns a Searchable (searchableAs(), getSearchKey(), toSearchableArray()). The key field defaults to 'id', and the document defaults to a shallow copy of the record:
const doc = makeSearchable(
{ uuid: 'abc', title: 'Hidden', secret: 'no' },
{ index: 'docs', key: 'uuid', toDocument: (r) => ({ title: r['title'] }) },
)Any object implementing those three methods can be imported directly. getSearchKey() throws when the key field is missing.
The builder
search(engine, index, phrase?) starts a Builder. An empty phrase matches every document. Matching is token based: a document matches when it contains every query token, then hits are ranked by relevance with ties broken by key.
const builder = search(engine, 'posts', 'widgets')
.where('status', 'published') // loose equality, so 1 matches '1'
.whereIn('category', ['news', 'blog'])
.orderBy('title') // 'asc' by default
.query((docs) => docs.filter((d) => Number(d['views']) > 5)) // raw refinement
builder.raw() // { hits: [{ key, document, score }], total }
builder.keys() // SearchKey[]
builder.get() // SearchableDocument[]
const page = builder.paginate(15, 2)
// { data, total, perPage: 15, currentPage: 2, lastPage }Engines and the manager
import { CollectionEngine, SearchManager, type Engine } from '@plinthjs/search'
const search = new SearchManager({
default: 'memory',
engines: { memory: () => new CollectionEngine() },
})
// Register a custom driver (the `Search::extend` equivalent).
search.extend('remote', (): Engine => new MyRemoteEngine())
search.engine() // the default engine, built once and cached
search.engine('remote')
search.flush('remote', 'posts') // the `search:flush` equivalentAn Engine implements update(index, documents), delete(index, keys), search(index, query), paginate(index, query, perPage, page) and flush(index). Resolving an unregistered engine name throws.