Typesense is an open-source, typo-tolerant search engine built in C++ for speed. It is like Algolia but self-hosted, free, and with a simpler API.
Setup
docker run -p 8108:8108 \
-v /tmp/typesense-data:/data \
typesense/typesense:latest \
--data-dir /data --api-key=xyz
Create Collection and Index
import Typesense from 'typesense';
const client = new Typesense.Client({
nodes: [{ host: 'localhost', port: 8108, protocol: 'http' }],
apiKey: 'xyz'
});
// Create schema
await client.collections().create({
name: 'products',
fields: [
{ name: 'name', type: 'string' },
{ name: 'description', type: 'string' },
{ name: 'price', type: 'float' },
{ name: 'category', type: 'string', facet: true },
{ name: 'rating', type: 'float' }
],
default_sorting_field: 'rating'
});
// Index documents
await client.collections('products').documents().import([
{ name: 'Wireless Mouse', description: 'Ergonomic wireless mouse', price: 29.99, category: 'peripherals', rating: 4.5 },
{ name: 'Mechanical Keyboard', description: 'Cherry MX switches', price: 89.99, category: 'peripherals', rating: 4.8 }
]);
Search
const results = await client.collections('products').documents().search({
q: 'wireles mous', // typos handled automatically
query_by: 'name,description',
filter_by: 'price:<50 && category:peripherals',
sort_by: 'rating:desc',
facet_by: 'category',
per_page: 10
});
REST API
# Search
curl "http://localhost:8108/collections/products/documents/search?q=keyboard&query_by=name" \
-H "X-TYPESENSE-API-KEY: xyz"
# Get document
curl http://localhost:8108/collections/products/documents/1 \
-H "X-TYPESENSE-API-KEY: xyz"
# Collection stats
curl http://localhost:8108/collections/products \
-H "X-TYPESENSE-API-KEY: xyz"
InstantSearch.js Integration
import instantsearch from 'instantsearch.js';
import TypesenseInstantSearchAdapter from 'typesense-instantsearch-adapter';
const adapter = new TypesenseInstantSearchAdapter({
server: { nodes: [{ host: 'localhost', port: 8108, protocol: 'http' }], apiKey: 'xyz' },
additionalSearchParameters: { query_by: 'name,description' }
});
const search = instantsearch({
indexName: 'products',
searchClient: adapter.searchClient
});
Why This Matters
- Self-hosted Algolia: Same instant search UX, zero cost
- Built in C++: Sub-millisecond search latency
- Typo tolerance: Out of the box, no configuration
- InstantSearch.js: Drop-in frontend integration
Need custom search or data tools? I build developer tools. Check out my web scraping actors on Apify or reach out at spinov001@gmail.com for custom solutions.
Top comments (0)