Algolia Upgrade & Migration (v4 to v5)
Overview
Guide for upgrading algoliasearch from v4 to v5. The v5 release is a major rewrite: initIndex() is removed, all methods move to the client, and the import style changes.
Prerequisites
- Current
algoliasearch v4 installed
- Git for version control (work in a branch)
- Test suite passing on current version
Breaking Changes Summary
| v4 Pattern |
v5 Replacement |
const client = algoliasearch(appId, key) |
import { algoliasearch } from 'algoliasearch'; const client = algoliasearch(appId, key); |
const index = client.initIndex('name') |
Removed — pass indexName to every method |
index.search('query') |
client.searchSingleIndex({ indexName, searchParams: { query } }) |
index.saveObjects(records) |
client.saveObjects({ indexName, objects }) |
index.saveObject(record) |
client.saveObject({ indexName, body: record }) |
index.partialUpdateObject(data) |
client.partialUpdateObject({ indexName, objectID, attributesToUpdate }) |
index.deleteObject('id') |
client.deleteObject({ indexName, objectID }) |
index.setSettings(settings) |
client.setSettings({ indexName, indexSettings }) |
index.getSettings() |
client.getSettings({ indexName }) |
index.browse() |
client.browse({ indexName, browseParams }) |
index.findObject(cb) |
client.findObject({ indexName, ... }) |
index.replaceAllObjects(records) |
client.replaceAllObjects({ indexName, objects }) |
index.saveSynonyms(syns) |
client.saveSynonyms({ indexName, synonymHit }) |
index.saveRule(rule) |
client.saveRule({ indexName, objectID, rule }) |
index.waitTask(taskID) |
client.waitForTask({ indexName, taskID }) |
Instructions
Step 1: Create Upgrade Branch and Install v5
git checkout -b upgrade/algoliasearch-v5
npm install algoliasearch@latest
npm list algoliasearch # Verify v5.x.x
Step 2: Update Imports
// v4
import algoliasearch from 'algoliasearch';
const client = algoliasearch('APP_ID', 'API_KEY');
// v5
import { algoliasearch } from 'algoliasearch';
const client = algoliasearch('APP_ID', 'API_KEY');
// v5 lite client (search-only, frontend)
import { liteClient } from 'algoliasearch/lite';
const searchClient = liteClient('APP_ID', 'SEARCH_KEY');
// v5 individual API client (if you only need one)
import { searchClient } from '@algolia/client-search';
Step 3: Remove initIndex and Update Method Calls
// v4: index-based API
const index = client.initIndex('products');
const { hits } = await index.search('laptop');
await index.saveObjects(records);
await index.setSettings({ searchableAttributes: ['name'] });
// v5: client-based API with indexName parameter
const { hits } = await client.searchSingleIndex({
indexName: 'products',
searchParams: { query: 'laptop' },
});
await client.saveObjects({ indexName: 'products', objects: records });
await client.setSettings({
indexName: 'products',
indexSettings: { searchableAttributes: ['name'] },
});
Step 4: Update waitTask
// v4
const { taskID } = await index.saveObjects(records);
await index.waitTask(taskID);
// v5
const { taskID } = await client.saveObjects({ indexName: 'products', objects: records });
await client.waitForTask({ indexName: 'products', taskID });
Step 5: Update Error Handling
// v4: error classes from algoliasearch
import { AlgoliaError } from 'algoliasearch';
// v5: error classes
import { ApiError } from 'algoliasearch';
try {
await client.searchSingleIndex({ indexName: 'products', searchParams: { query: 'test' } });
} catch (error) {
if (error instanceof ApiError) {
console.error(`HTTP ${error.status}: ${error.message}`);
}
}
Step 6: Find All Usage and Verify
# Find all files using Algolia v4 patterns
grep -rn "initIndex\|\.search(\|\.saveObjects\|\.setSettings\|\.deleteObject\|\.waitTask" \
--include="*.ts" --include="*.tsx" --include="*.js" --include="*.jsx" src/
# Run tests
npm test
# Type-check
npx tsc --noEmit
Rollback Procedure
# If v5 breaks things, revert to v4
npm install algoliasearch@4
git checkout -- src/ # Restore v4 code
npm test # Verify v4 still works
Error Handling
| Issue |
Cause |
Solution |
initIndex is not a function |
v5 installed but v4 code |
Remove initIndex, pass indexName to methods |
searchSingleIndex is not a function |
v4 installed but v5 code |
Run npm install algoliasearch@latest |
| Type errors after upgrade |
Changed type signatures |
Update to new parameter objects |
default import error |
v5 uses named exports |
Change import algoliasearch to import { algoliasearch } |
Resources
Next Steps
For CI integration during upgrades, see algolia-ci-integration.
1---2name: algolia-upgrade-migration3description: |4 Upgrade algoliasearch from v4 to v5 with breaking change detection and codemod.5 Use when upgrading SDK versions, detecting deprecations, or migrating initIndex patterns.6 Trigger: "upgrade algolia", "algolia migration v5", "algolia breaking changes",7 "update algolia SDK", "algolia v4 to v5".8allowed-tools: Read, Write, Edit, Bash(npm:*), Bash(git:*), Bash(npx:*), Grep9version: 1.0.010license: MIT11author: Jeremy Longshore <jeremy@intentsolutions.io>12tags: [saas, search, algolia]13compatible-with: claude-code14---1516# Algolia Upgrade & Migration (v4 to v5)1718## Overview1920Guide for upgrading `algoliasearch` from v4 to v5. The v5 release is a major rewrite: `initIndex()` is removed, all methods move to the client, and the import style changes.2122## Prerequisites2324- Current `algoliasearch` v4 installed25- Git for version control (work in a branch)26- Test suite passing on current version2728## Breaking Changes Summary2930| v4 Pattern | v5 Replacement |31|-----------|----------------|32| `const client = algoliasearch(appId, key)` | `import { algoliasearch } from 'algoliasearch'; const client = algoliasearch(appId, key);` |33| `const index = client.initIndex('name')` | Removed — pass `indexName` to every method |34| `index.search('query')` | `client.searchSingleIndex({ indexName, searchParams: { query } })` |35| `index.saveObjects(records)` | `client.saveObjects({ indexName, objects })` |36| `index.saveObject(record)` | `client.saveObject({ indexName, body: record })` |37| `index.partialUpdateObject(data)` | `client.partialUpdateObject({ indexName, objectID, attributesToUpdate })` |38| `index.deleteObject('id')` | `client.deleteObject({ indexName, objectID })` |39| `index.setSettings(settings)` | `client.setSettings({ indexName, indexSettings })` |40| `index.getSettings()` | `client.getSettings({ indexName })` |41| `index.browse()` | `client.browse({ indexName, browseParams })` |42| `index.findObject(cb)` | `client.findObject({ indexName, ... })` |43| `index.replaceAllObjects(records)` | `client.replaceAllObjects({ indexName, objects })` |44| `index.saveSynonyms(syns)` | `client.saveSynonyms({ indexName, synonymHit })` |45| `index.saveRule(rule)` | `client.saveRule({ indexName, objectID, rule })` |46| `index.waitTask(taskID)` | `client.waitForTask({ indexName, taskID })` |4748## Instructions4950### Step 1: Create Upgrade Branch and Install v55152```bash53git checkout -b upgrade/algoliasearch-v554npm install algoliasearch@latest55npm list algoliasearch # Verify v5.x.x56```5758### Step 2: Update Imports5960```typescript61// v462import algoliasearch from 'algoliasearch';63const client = algoliasearch('APP_ID', 'API_KEY');6465// v566import { algoliasearch } from 'algoliasearch';67const client = algoliasearch('APP_ID', 'API_KEY');6869// v5 lite client (search-only, frontend)70import { liteClient } from 'algoliasearch/lite';71const searchClient = liteClient('APP_ID', 'SEARCH_KEY');7273// v5 individual API client (if you only need one)74import { searchClient } from '@algolia/client-search';75```7677### Step 3: Remove initIndex and Update Method Calls7879```typescript80// v4: index-based API81const index = client.initIndex('products');82const { hits } = await index.search('laptop');83await index.saveObjects(records);84await index.setSettings({ searchableAttributes: ['name'] });8586// v5: client-based API with indexName parameter87const { hits } = await client.searchSingleIndex({88 indexName: 'products',89 searchParams: { query: 'laptop' },90});91await client.saveObjects({ indexName: 'products', objects: records });92await client.setSettings({93 indexName: 'products',94 indexSettings: { searchableAttributes: ['name'] },95});96```9798### Step 4: Update waitTask99100```typescript101// v4102const { taskID } = await index.saveObjects(records);103await index.waitTask(taskID);104105// v5106const { taskID } = await client.saveObjects({ indexName: 'products', objects: records });107await client.waitForTask({ indexName: 'products', taskID });108```109110### Step 5: Update Error Handling111112```typescript113// v4: error classes from algoliasearch114import { AlgoliaError } from 'algoliasearch';115116// v5: error classes117import { ApiError } from 'algoliasearch';118119try {120 await client.searchSingleIndex({ indexName: 'products', searchParams: { query: 'test' } });121} catch (error) {122 if (error instanceof ApiError) {123 console.error(`HTTP ${error.status}: ${error.message}`);124 }125}126```127128### Step 6: Find All Usage and Verify129130```bash131# Find all files using Algolia v4 patterns132grep -rn "initIndex\|\.search(\|\.saveObjects\|\.setSettings\|\.deleteObject\|\.waitTask" \133 --include="*.ts" --include="*.tsx" --include="*.js" --include="*.jsx" src/134135# Run tests136npm test137138# Type-check139npx tsc --noEmit140```141142## Rollback Procedure143144```bash145# If v5 breaks things, revert to v4146npm install algoliasearch@4147git checkout -- src/ # Restore v4 code148npm test # Verify v4 still works149```150151## Error Handling152153| Issue | Cause | Solution |154|-------|-------|----------|155| `initIndex is not a function` | v5 installed but v4 code | Remove `initIndex`, pass `indexName` to methods |156| `searchSingleIndex is not a function` | v4 installed but v5 code | Run `npm install algoliasearch@latest` |157| Type errors after upgrade | Changed type signatures | Update to new parameter objects |158| `default import` error | v5 uses named exports | Change `import algoliasearch` to `import { algoliasearch }` |159160## Resources161162- [Official v4 to v5 Migration Guide](https://www.algolia.com/doc/libraries/javascript/v5/upgrade/)163- [v5 Method Reference](https://www.algolia.com/doc/libraries/javascript/v5/methods/search/)164- [algoliasearch npm](https://www.npmjs.com/package/algoliasearch)165166## Next Steps167168For CI integration during upgrades, see `algolia-ci-integration`.169