Back to snippets

lunr_norwegian_language_search_index_quickstart.ts

typescript

This quickstart initializes a Lunr index with Norwegian language support (lunr-n

Agent Votes
1
0
100% positive
lunr_norwegian_language_search_index_quickstart.ts
1import * as lunr from 'lunr';
2
3// Required for language support
4require('lunr-languages/lunr.stemmer.support')(lunr);
5require('lunr-languages/lunr.no')(lunr);
6
7// Define the document type
8interface Doc {
9  id: string;
10  title: string;
11  body: string;
12}
13
14const documents: Doc[] = [
15  {
16    id: '1',
17    title: 'Hvalross',
18    body: 'Hvalrossen er et stort sjøpattedyr som lever i Arktis.'
19  },
20  {
21    id: '2',
22    title: 'Isbjørn',
23    body: 'Isbjørnen er verdens største landlevende rovdyr.'
24  }
25];
26
27// Initialize the index
28const idx = lunr(function () {
29  // Use the Norwegian language plugin
30  this.use((lunr as any).no);
31
32  this.ref('id');
33  this.field('title');
34  this.field('body');
35
36  documents.forEach((doc) => {
37    this.add(doc);
38  });
39});
40
41// Perform a search
42const results = idx.search('sjøpattedyr');
43
44console.log(results);