Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

lunr-search-index.js 2.3 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  1. const lunr = require('lunr')
  2. require('lunr-languages/lunr.stemmer.support')(lunr)
  3. require('lunr-languages/tinyseg')(lunr)
  4. require('lunr-languages/lunr.ja')(lunr)
  5. require('lunr-languages/lunr.es')(lunr)
  6. require('lunr-languages/lunr.pt')(lunr)
  7. require('lunr-languages/lunr.de')(lunr)
  8. const fs = require('fs').promises
  9. const path = require('path')
  10. const rank = require('./rank')
  11. const validateRecords = require('./validate-records')
  12. const { compress } = require('../../lib/search/compress')
  13. module.exports = class LunrIndex {
  14. constructor (name, records) {
  15. this.name = name
  16. // Add custom rankings
  17. this.records = records.map(record => {
  18. record.customRanking = rank(record)
  19. return record
  20. })
  21. this.validate()
  22. return this
  23. }
  24. validate () {
  25. return validateRecords(this.name, this.records)
  26. }
  27. build () {
  28. const language = this.name.split('-').pop()
  29. const records = this.records
  30. this.index = lunr(function constructIndex () { // No arrow here!
  31. if (['ja', 'es', 'pt', 'de'].includes(language)) {
  32. this.use(lunr[language])
  33. }
  34. this.ref('objectID')
  35. this.field('url')
  36. this.field('slug')
  37. this.field('breadcrumbs')
  38. this.field('heading')
  39. this.field('title')
  40. this.field('content')
  41. this.field('topics')
  42. this.field('customRanking')
  43. this.metadataWhitelist = ['position']
  44. for (const record of records) {
  45. this.add(record)
  46. }
  47. })
  48. }
  49. toJSON () {
  50. this.build()
  51. return JSON.stringify(this.index, null, 2)
  52. }
  53. get recordsObject () {
  54. return Object.fromEntries(
  55. this.records.map(record => [record.objectID, record])
  56. )
  57. }
  58. async write () {
  59. this.build()
  60. // Write the parsed records
  61. await Promise.resolve(this.recordsObject)
  62. .then(JSON.stringify)
  63. .then(compress)
  64. .then(content => fs.writeFile(
  65. path.posix.join(__dirname, '../../lib/search/indexes', `${this.name}-records.json.br`),
  66. content
  67. // Do not set to 'utf8'
  68. ))
  69. // Write the index
  70. await Promise.resolve(this.index)
  71. .then(JSON.stringify)
  72. .then(compress)
  73. .then(content => fs.writeFile(
  74. path.posix.join(__dirname, '../../lib/search/indexes', `${this.name}.json.br`),
  75. content
  76. // Do not set to 'utf8'
  77. ))
  78. }
  79. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...