Typesense is an open-source search engine built for speed — searches complete in under 10ms with built-in typo tolerance.
Define Collection & Index Data
import Typesense from 'typesense'
const client = new Typesense.Client({
nodes: [{ host: 'localhost', port: 8108, protocol: 'http' }],
apiKey: 'xyz'
})
// Create collection with 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: 'brand', type: 'string', facet: true },
{ name: 'rating', type: 'float' }
],
default_sorting_field: 'rating'
})
// Index documents
await client.collections('products').documents().import([
{ id: '1', name: 'MacBook Pro M3', description: 'Pro laptop', price: 1999, category: 'Laptops', brand: 'Apple', rating: 4.8 },
{ id: '2', name: 'ThinkPad X1', description: 'Business laptop', price: 1499, category: 'Laptops', brand: 'Lenovo', rating: 4.5 }
])
Search
const results = await client.collections('products').documents().search({
q: 'macbok', // Typo-tolerant!
query_by: 'name,description',
filter_by: 'price:<2000 && category:Laptops',
sort_by: 'rating:desc',
facet_by: 'category,brand',
per_page: 10,
highlight_full_fields: 'name'
})
REST API
curl 'http://localhost:8108/collections/products/documents/search?q=laptop&query_by=name,description&filter_by=price:<2000' \
-H 'X-TYPESENSE-API-KEY: xyz'
Geosearch
await client.collections('stores').create({
name: 'stores',
fields: [
{ name: 'name', type: 'string' },
{ name: 'location', type: 'geopoint' }
]
})
// Find stores within 10km
const results = await client.collections('stores').documents().search({
q: '*',
filter_by: 'location:(48.85, 2.35, 10 km)', // Near Paris
sort_by: 'location(48.85, 2.35):asc'
})
Vector Search (Semantic)
await client.collections().create({
name: 'docs',
fields: [
{ name: 'title', type: 'string' },
{ name: 'content', type: 'string' },
{ name: 'embedding', type: 'float[]', num_dim: 384 }
]
})
// Hybrid search: keyword + vector
const results = await client.collections('docs').documents().search({
q: 'how to deploy',
query_by: 'title,content',
vector_query: 'embedding:([0.1, 0.2, ...], k:10)',
exclude_fields: 'embedding'
})
InstantSearch Integration
import TypesenseInstantSearchAdapter from 'typesense-instantsearch-adapter'
import { InstantSearch, SearchBox, Hits } from 'react-instantsearch'
const adapter = new TypesenseInstantSearchAdapter({
server: { apiKey: 'search-only-key', nodes: [{ host: 'localhost', port: 8108, protocol: 'http' }] },
additionalSearchParameters: { query_by: 'name,description' }
})
function Search() {
return (
<InstantSearch searchClient={adapter.searchClient} indexName="products">
<SearchBox />
<Hits hitComponent={Hit} />
</InstantSearch>
)
}
Real-World Use Case
An e-commerce platform with 500K products was using Elasticsearch. Infrastructure: 3 nodes, 24GB RAM, $300/month. Search latency: 200-500ms. They migrated to Typesense: single node, 4GB RAM, $20/month. Search latency: 5-15ms. Same features (facets, typo tolerance, sorting), 95% less infrastructure.
Typesense is search without the Elasticsearch tax.
Build Smarter Data Pipelines
Need to scrape websites, extract APIs, or automate data collection? Check out my ready-to-use scrapers on Apify — no coding required.
Custom scraping solution? Email me at spinov001@gmail.com — fast turnaround, fair prices.
Top comments (0)