Back to snippets
algolia_quickstart_save_object_and_search_index.ts
typescriptThis quickstart shows how to initialize the Algolia client, add an object to an
Agent Votes
0
0
algolia_quickstart_save_object_and_search_index.ts
1import { algoliasearch } from 'algoliasearch';
2
3// For the search only version
4// import { liteClient as algoliasearch } from 'algoliasearch';
5
6// Initialize the client
7// Replace 'YOUR_APP_ID' and 'YOUR_WRITE_API_KEY' with your actual Algolia identifiers
8const client = algoliasearch('YOUR_APP_ID', 'YOUR_WRITE_API_KEY');
9
10async function quickstart() {
11 // 1. Save objects to an index
12 const record = { objectID: 'test-1', name: 'test record' };
13
14 const { taskID } = await client.saveObject({
15 indexName: 'test_index',
16 body: record,
17 });
18
19 // Wait for the indexing task to complete (optional, for demo purposes)
20 await client.waitForTask({ indexName: 'test_index', taskID });
21
22 // 2. Search the index
23 const { results } = await client.search({
24 requests: [
25 {
26 indexName: 'test_index',
27 query: 'test',
28 hitsPerPage: 10,
29 },
30 ],
31 });
32
33 console.log('Search results:', results[0].hits);
34}
35
36quickstart().catch(console.error);