-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathAutocomplete.vue
125 lines (110 loc) · 3.93 KB
/
Autocomplete.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
<script>
import { ReactiveBase, DataSearch } from '@appbaseio/reactivesearch-vue'
import { useDebounceFn } from '@vueuse/core'
Vue.use(ReactiveBase)
Vue.use(DataSearch)
export default {
props: {
additionals: Object,
debounce: {
type: Number,
default: 100,
},
size: {
type: Number,
default: 10,
},
multiMatchTypes: {
type: Array,
default: () => ['best_fields', 'phrase', 'phrase_prefix'],
},
},
render() {
return this.$scopedSlots.default(this)
},
data() {
return {
results: {},
resultsCount: 0,
searchAdditionals: () => null,
overlay: false,
searchLoading: false,
}
},
mounted() {
this.$nextTick(() => this.$emit('mounted'))
let self = this
// Define function here to gain access to the debounce prop
this.searchAdditionals = useDebounceFn(function (query) {
if (!self.additionals) {
return
}
// Initialize with empty data to preserve additionals order
self.results = Object.fromEntries(Object.keys(self.additionals).map((indexName) => [indexName, []]))
self.resultsCount = 0
let url = new URL(config.es_url)
let auth = `Basic ${btoa(`${url.username}:${url.password}`)}`
let baseUrl = url.origin
Object.entries(self.additionals).forEach(([name, data]) => {
let stores = data['stores'] ?? null
if (stores && !stores.includes(window.config.store_code)) {
return
}
let fields = data['fields'] ?? data
let size = data['size'] ?? self.size ?? undefined
let sort = data['sort'] ?? undefined
let fuzziness = data['fuzziness'] ?? 'AUTO'
let multimatch = self.multiMatchTypes.map((type) => ({
multi_match: {
query: query,
type: type,
fields: fields,
fuzziness: type.includes('phrase') ? undefined : fuzziness,
},
}))
let esQuery = {
size: size,
sort: sort,
query: {
bool: {
should: multimatch,
minimum_should_match: 1,
},
},
highlight: {
pre_tags: ['<mark>'],
post_tags: ['</mark>'],
fields: Object.fromEntries(fields.map((field) => [field.split('^')[0], {}])),
require_field_match: false,
},
}
rapidezFetch(`${baseUrl}/${config.es_prefix}_${name}_${config.store}/_search`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', Authorization: auth },
body: JSON.stringify(esQuery),
}).then(async (response) => {
const responseData = await response.json()
self.results[name] = responseData?.hits ?? []
self.results.count += self.results[name]?.hits?.length ?? 0
})
})
}, self.debounce)
},
methods: {
startLoading() {
this.searchLoading = true
},
stopLoading() {
this.searchLoading = false
},
highlight(hit, field) {
let source = hit._source ?? hit.source
let highlight = hit.highlight ?? source.highlight
return highlight?.[field]?.[0] ?? source?.[field] ?? ''
},
showOverlay(isOpen) {
this.overlay = isOpen
},
},
}
</script>