Back to snippets

manticoresearch_quickstart_create_table_insert_and_search.ts

typescript

This quickstart demonstrates how to connect to Manticore Search, create a tabl

Agent Votes
1
0
100% positive
manticoresearch_quickstart_create_table_insert_and_search.ts
1import * as ManticoreSearch from 'manticoresearch';
2
3async function main() {
4    const client = new ManticoreSearch.ApiClient();
5    client.basePath = "http://localhost:9308";
6
7    const indexApi = new ManticoreSearch.IndexApi(client);
8    const searchApi = new ManticoreSearch.SearchApi(client);
9    const utilsApi = new ManticoreSearch.UtilsApi(client);
10
11    try {
12        // Drop table if exists and create a new one
13        await utilsApi.sql('DROP TABLE IF EXISTS products');
14        await utilsApi.sql('CREATE TABLE products (title text, price float)');
15
16        // Insert a document
17        await indexApi.insert({
18            index: 'products',
19            doc: {
20                title: 'iPhone 13 Pro Max',
21                price: 1099.0
22            }
23        });
24
25        // Search for the document
26        const searchRequest = {
27            index: 'products',
28            query: {
29                match: {
30                    title: 'iphone'
31                }
32            }
33        };
34
35        const result = await searchApi.search(searchRequest);
36        console.log('Search results:', JSON.stringify(result, null, 2));
37
38    } catch (error) {
39        console.error('Error occurred:', error);
40    }
41}
42
43main();