-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.ts
141 lines (118 loc) Β· 3.55 KB
/
search.ts
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import lunr from 'lunr'
import { readFileSync, writeFileSync } from 'fs'
import { join } from 'path'
import boxen from 'boxen'
import cron from 'node-cron'
import { CategoryPattern } from '../typings/types'
import { ensureDirExists, tempDir } from './commons'
import { profile } from './env'
import { quotes } from './quotes'
const task = cron.schedule('0 * * * *', () => {
console.log(
boxen('Running task every 60 minutes on index search', {
padding: 1,
margin: 1,
borderStyle: 'single',
borderColor: 'yellow',
})
)
storeIndex()
})
task.start()
const filePath: string = join(tempDir, `${profile.indexOptions.path}`)
const getIndexPath = (filePath: string): string => join(filePath, `${profile.indexOptions.name}`)
const storeIndex = (): lunr.Index => {
try {
ensureDirExists(filePath)
const index = createIndex()
const indexPath = getIndexPath(filePath)
console.log(
boxen(`Storing index file by path=${indexPath}`, {
padding: 1,
margin: 1,
borderStyle: 'single',
borderColor: 'yellow',
})
)
writeFileSync(indexPath, JSON.stringify(index))
return index
} catch (e) {
console.error(
boxen(`Failed to store index file, message=${e.message}`, {
padding: 1,
margin: 1,
borderStyle: 'double',
borderColor: 'red',
})
)
throw e
}
}
const restoreIndex = (): lunr.Index => {
try {
const indexPath = getIndexPath(filePath)
console.log(
boxen(`Restoring index file from path=${indexPath}`, {
padding: 1,
margin: 1,
borderStyle: 'single',
borderColor: 'yellow',
})
)
const index = readFileSync(indexPath, 'utf-8')
return lunr.Index.load(JSON.parse(index))
} catch (e) {
console.error(
boxen(`Failed to restore index file, message=${e.message}`, {
padding: 1,
margin: 1,
borderStyle: 'double',
borderColor: 'red',
})
)
throw e
}
}
const createIndex = (): lunr.Index => {
return lunr(function () {
this.field('quote')
this.field('author')
for (const category of Object.values(CategoryPattern)) {
for (const [index, value] of quotes[category].entries()) {
value.id = `${category}${profile.indexOptions.delimiter}${index}`
this.add(value)
}
}
})
}
export const idx = (): lunr.Index => {
try {
return restoreIndex()
} catch (e) {
return storeIndex()
}
}
export const getSearchResultSet = <T>(
data: T[],
index: lunr.Index,
query: lunr.Index.QueryString
): (T | undefined)[] => {
const results = getSearchResults(index, query)
return results.map(result => {
return data.find(item => result.ref === item['id'])
})
}
export const getSearchResults = (index: lunr.Index, query: lunr.Index.QueryString): lunr.Index.Result[] => {
return index.search(query)
}
export const getSearchResultsByTerm = (
index: lunr.Index,
term: string | object | object[]
): lunr.Index.Result[] => {
return index.query(q => {
q.term(lunr.tokenizer(term), {
wildcard: lunr.Query.wildcard.TRAILING,
presence: lunr.Query.presence.REQUIRED,
})
})
}