From 38c35fa0810823a8c9fe179269ad3ca90ccee584 Mon Sep 17 00:00:00 2001 From: Altrius Date: Sun, 17 Sep 2023 10:34:04 +0100 Subject: [PATCH] search changes (again) --- components/Header.vue | 114 ++++++++++++++++++++++++++++-------------- 1 file changed, 76 insertions(+), 38 deletions(-) diff --git a/components/Header.vue b/components/Header.vue index 59787c1..6878139 100644 --- a/components/Header.vue +++ b/components/Header.vue @@ -216,7 +216,7 @@ const visible = ref(false) // const thumbs = ref({}) const query = ref('') -const { pending } = useAsyncData(async () => { +useAsyncData(async () => { if (query.value.length > 1) { const res = await (await fetch('https://search.thewandb.com/multi-search', { method: 'POST', @@ -239,43 +239,71 @@ const { pending } = useAsyncData(async () => { q: query.value, attributesToRetrieve: [ 'parent', - 'id' - ] - }, - { - indexUid: 'cast', - q: query.value + 'id', + 'text' + ], + showMatchesPosition: true } ] }) })).json() - results.value = [ - { - key: 'topics', - label: 'Topics', - icon: 'i-heroicons-chat-bubble-left-right', - values: res.results[0] - }, - { - key: 'episodes', - label: 'Episodes', - icon: 'i-heroicons-tv', - values: res.results[1] - }, - { - key: 'transcripts', - label: 'Transcripts', - icon: 'i-heroicons-document-text', - values: res.results[2] - } - ] + try { + res.results[2].hits.map((m) => { + m.matches = [] + m.text = m.text.replaceAll('\\r\\n', ' ') + for (let i = 0; i < m._matchesPosition.text.length; i++) { + const x = m._matchesPosition.text[i] + const regex = new RegExp(`[^.?!]*(?<=[.?\\s!])${m.text.substring(x.start, x.start + x.length)}(?=[\\s.?!])[^.?!]*[.?!]`, 'igm') + m.matches = m.matches.concat(regex.exec(m.text.substring(x.start - 300, x.start + x.length + 1000))) + } + + m.matches = m.matches.filter((elem, pos) => m.matches.indexOf(elem) === pos && elem !== null) + + console.log(m.matches) + m.matches = m.matches.map((elem) => { + if (elem.length > 150) { + elem = elem.substring(0, 147) + elem += '...' + } + + return elem + }) + + return m + }) + + res.results[2].hits.filter(m => m.matches.length > 0) + + results.value = [ + { + key: 'topics', + label: 'Topics', + icon: 'i-heroicons-chat-bubble-left-right', + values: res.results[0] + }, + { + key: 'episodes', + label: 'Episodes', + icon: 'i-heroicons-tv', + values: res.results[1] + }, + { + key: 'transcripts', + label: 'Transcripts', + icon: 'i-heroicons-document-text', + values: res.results[2] + } + ] + } catch (e) { + console.error(e) + } try { epmap.value = (await (await fetcher('search', { method: 'POST', body: JSON.stringify({ - episodes: res.results[0].hits.map(c => c.parent) + episodes: [].concat(res.results[0].hits.map(c => c.parent), res.results[2].hits.map(c => c.parent)) }) })).json()).data } catch (e) { @@ -288,10 +316,6 @@ const { pending } = useAsyncData(async () => { watch: [query] }) -function openVideo (id) { - window.location.pathname = '/videos/' + id -} - - {{ console.log(item) }} @@ -379,7 +402,6 @@ function openVideo (id) { size="lg" /> {{ selected.label }} - {{ console.log(selected) }} @@ -442,7 +464,7 @@ function openVideo (id) { -