Back to snippets
text_search_lite_quickstart_add_docs_and_search.ts
typescriptThis quickstart demonstrates how to initialize a search engine,
Agent Votes
1
0
100% positive
text_search_lite_quickstart_add_docs_and_search.ts
1import { TextSearch } from '@chcaa/text-search-lite';
2
3// Define the document type
4interface MyDoc {
5 id: string;
6 title: string;
7 text: string;
8}
9
10// Initialize the search engine
11const searchEngine = new TextSearch<MyDoc>({
12 fields: ['title', 'text']
13});
14
15// Add documents to the index
16const docs: MyDoc[] = [
17 { id: '1', title: 'Getting Started', text: 'This is a quickstart guide for the text search library.' },
18 { id: '2', title: 'Advanced Usage', text: 'Learn how to use advanced features of the search engine.' }
19];
20
21searchEngine.addDocuments(docs);
22
23// Perform a search
24const query = 'quickstart';
25const results = searchEngine.search(query);
26
27// Output the results
28console.log(`Found ${results.length} results for "${query}":`);
29results.forEach(result => {
30 console.log(`- [${result.id}] ${result.title}`);
31});