-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlunr.vue
241 lines (235 loc) · 6.68 KB
/
lunr.vue
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<template>
<div key="outputHash">
<slot v-bind:item="item, key" v-for="(item, key) in theOutput">
{{ item }}
</slot>
</div>
</template>
<script>
import elasticlunr from 'elasticlunr'
export default {
methods:{
flattenObj(feilds, obj, parent, res = {}){
for(let key in feilds){
let propName = parent || key
if (typeof obj[key] == 'object') {
this.flattenObj(obj[key], obj[key], propName, res) // This updates the 'res' but how??
} else {
if (res[propName]) {
res[propName] += " " + obj[key]
} else {
res[propName] = obj[key]
}
}
}
return res
},
output(search){
let that = this
if (this.idx && this.idx.search) {
return [ ...this.idx.search(search).map(function (valu){
const doc = that.input.find((doc,i) => valu.ref == doc.id || valu.ref == i)
if (doc) {
return doc
}
that.$emit("get", valu)
return {id: valu.ref}
}, that).reduce((a,c)=>{
a.set(c.id, c);
return a;
}, new Map()).values()]
}
if (this.idx.then) return this.idx.then(index => {
return [ ...index.search(search).map(function (valu){
const doc = that.input.find((doc,i) => valu.ref == doc.id || valu.ref == i)
if (doc) {
return doc
}
that.$emit("get", valu)
return {id: valu.ref}
}, that).reduce((a,c)=>{
a.set(c.id, c)
return a
}, new Map()).values()]
})
// no index
return this.input
},
makeIndex(fieldHash){
if (this.input[0] && this.search){
let that = this
const first = this.fields
if (this.log) console.log("feilds from ", first)
if (!this.got[fieldHash]) {
this.got[fieldHash] = {}
}
const stopWords = this.stopWords
const documents = this.input.map(function (val, i){
return {id: i, ...that.flattenObj(first, val)}
})
const idx = elasticlunr(function () {
if (!stopWords) {
this.pipeline.remove(elasticlunr.stopWordFilter)
this.pipeline.remove(elasticlunr.stemmer)
}
Object.keys(first).forEach(function (key) {
if (key[0] !== '_') {
this.addField(key)
}
}, this)
if (that.log) console.log(this)
})
documents.forEach(function (doc) {
let docHash = that.hashThis(JSON.stringify(doc))
if (!that.got[fieldHash][docHash]) {
this.addDoc(doc)
that.got[fieldHash][docHash] = true
}
}, idx)
that.cache[fieldHash] = idx.toJSON()
that.$emit("indexed", that.cache)
return idx
} else {
return {}
}
},
hashThis(message, n = 8) {
if (+n === 0) return message
let hash = 0
if (message.length == 0) return this.hashThis(this.hex32(hash),n-1)
for (let i = 0;i < message.length;i++) {
const char = message.charCodeAt(i)
hash = ((hash<<5)-hash)+char
hash = hash & hash // Convert to 32bit integer
}
return this.hashThis(this.hex32(hash), n-1)
},
hex32(val) {
val &= 0xFFFFFFFF;
var hex = val.toString(16).toUpperCase()
return ("00000000" + hex).slice(-8)
},
},
computed:{
hashFields() {
return this.hashThis(JSON.stringify(this.fields))
},
idx() {
if (!this.input[0]) return {}
const loaded = this.cache[this.hashFields]
if (loaded) {
let idx = lunr.Index.load(loaded)
const documents = this.input.map((val, i) => {
if (this.log) console.log({id: i, ...this.flattenObj(this.fields, val)})
return {id: i, ...this.flattenObj(this.fields, val)}
})
if (!this.got[this.fieldHash]){
this.got[this.fieldHash] = {}
}
documents.forEach(doc => {
let docHash = this.hashThis(JSON.stringify(doc))
if (!this.got[this.fieldHash][docHash]) {
idx.updateDoc(doc)
idx.addDoc(doc)
this.got[this.fieldHash][docHash] = true
}
})
this.cache[this.fieldHash] = idx.toJSON()
this.$emit("indexed", this.cache)
return idx
} else {
return this.makeIndex(this.hashFields)
}
},
},
watch:{
search:{
handler(search = ""){
if ((!search.trim() || search == "undefined") && Array.isArray(this.input)) {
this.$emit("results-length", this.input.length)
this.$emit("results", this.input.slice(0, this.limit))
this.theOutput = this.input.slice(0, this.limit)
return
}
const update = output => {
if (output.then) {
let that = this
return output.then(newOutput => {
const hash = that.hashThis(JSON.stringify(newOutput))
if (that.outputHash === hash) return
that.outputHash = hash
that.$emit("results-length", newOutput.length)
that.$emit("results", newOutput.slice(0, that.limit))
that.theOutput = newOutput.slice(0, that.limit)
console.log(newOutput.slice(0, that.limit))
})
}
const hash = this.hashThis(JSON.stringify(output))
if (this.outputHash === hash) return
this.outputHash = hash
try {
this.$emit("results-length", output.length)
this.$emit("results", output.slice(0, this.limit))
this.theOutput = output.slice(0, this.limit)
if (this.log) console.log(output.slice(0, this.limit))
} catch(e) {
console.warn(e, "???", output)
}
}
if (this.output.then) {
return this.output(search).then(async output => {
update((await output))
})
}
update(this.output(search))
},
immediate: true,
},
},
props: {
input: {
type: Array,
required: true,
},
search:{
type: String,
default: '',
},
limit: {
type: Number,
default: 1000,
},
stopWords: {
type: Boolean,
default: false,
},
log: Boolean,
fields: {
type:Object,
default(){
if (typeof this.input[0] === "object"){
return this.input[0]
}
return {}
}
},
cache: {
type:Object,
default(){
return {}
}
},
perpendKey:{
type: String,
default: '',
},
},
data(){
return {
outputHash: "",
theOutput:[],
got: {},
}
},
}
</script>