331 lines
12 KiB
Vue
331 lines
12 KiB
Vue
<template>
|
|
<div class="space-y-6">
|
|
<h2 class="text-2xl font-bold">影片搜尋</h2>
|
|
|
|
<!-- Search Form -->
|
|
<div class="bg-gray-800 rounded-lg p-6 border border-gray-700">
|
|
<div class="flex space-x-4 mb-4">
|
|
<input
|
|
v-model="searchQuery"
|
|
@keyup.enter="performSearch"
|
|
type="text"
|
|
placeholder="輸入搜尋關鍵字..."
|
|
class="flex-1 bg-gray-700 border border-gray-600 rounded-lg px-4 py-3 text-white focus:outline-none focus:border-blue-500"
|
|
/>
|
|
<button
|
|
@click="performSearch"
|
|
:disabled="loading"
|
|
class="bg-blue-600 hover:bg-blue-700 disabled:bg-gray-600 px-6 py-3 rounded-lg font-semibold transition"
|
|
>
|
|
{{ loading ? '搜尋中...' : '搜尋' }}
|
|
</button>
|
|
</div>
|
|
<div class="flex flex-wrap items-center gap-4">
|
|
<!-- Result Type -->
|
|
<div class="flex items-center space-x-2">
|
|
<span class="text-gray-400 text-sm">搜尋類型:</span>
|
|
<select
|
|
v-model="searchType"
|
|
class="bg-gray-700 border border-gray-600 rounded-lg px-3 py-2 text-white text-sm focus:outline-none focus:border-blue-500"
|
|
>
|
|
<option value="chunk">文字區塊 (Chunk)</option>
|
|
<option value="trace">臉部軌跡 (Trace)</option>
|
|
</select>
|
|
</div>
|
|
<!-- Mode Selector -->
|
|
<div v-if="searchType === 'chunk'" class="flex items-center space-x-2">
|
|
<span class="text-gray-400 text-sm">搜尋模式:</span>
|
|
<select
|
|
v-model="searchMode"
|
|
class="bg-gray-700 border border-gray-600 rounded-lg px-3 py-2 text-white text-sm focus:outline-none focus:border-blue-500"
|
|
>
|
|
<option value="vector">向量搜尋 (Vector)</option>
|
|
<option value="bm25">關鍵字搜尋 (BM25)</option>
|
|
<option value="hybrid">混合搜尋 (Hybrid)</option>
|
|
<option value="smart">智慧搜尋 (Smart)</option>
|
|
</select>
|
|
</div>
|
|
<!-- File Selector -->
|
|
<div class="flex items-center space-x-2">
|
|
<span class="text-gray-400 text-sm">搜尋檔案:</span>
|
|
<select
|
|
v-model="selectedFileUuid"
|
|
class="bg-gray-700 border border-gray-600 rounded-lg px-3 py-2 text-white text-sm max-w-[300px] focus:outline-none focus:border-blue-500"
|
|
>
|
|
<option value="">所有檔案</option>
|
|
<option v-for="f in files" :key="f.file_uuid" :value="f.file_uuid">
|
|
{{ f.file_name?.substring(0, 40) || f.file_uuid }}
|
|
</option>
|
|
</select>
|
|
</div>
|
|
<span class="text-gray-500 text-xs">
|
|
{{ modeDescription }}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Results: Chunks -->
|
|
<div v-if="searchType === 'chunk' && results.length > 0" class="space-y-4">
|
|
<h3 class="text-xl font-semibold">搜尋結果 ({{ results.length }})</h3>
|
|
<div class="grid gap-4">
|
|
<div
|
|
v-for="(hit, index) in results"
|
|
:key="index"
|
|
@click="goToDetail(hit.vid, hit.id)"
|
|
class="bg-gray-800 rounded-lg p-6 border border-gray-700 hover:border-blue-500 cursor-pointer transition"
|
|
>
|
|
<div class="flex justify-between items-start">
|
|
<div class="flex-1">
|
|
<div class="flex items-center space-x-3 mb-2">
|
|
<span class="text-gray-400 text-sm">
|
|
type: <span class="text-blue-300">{{ hit.id.split('_')[0] }}</span>
|
|
uuid: <span class="text-purple-300">{{ hit.vid }}</span>
|
|
</span>
|
|
<span v-if="hit.parent_id" class="bg-yellow-900 text-yellow-300 px-2 py-1 rounded text-sm">
|
|
parent_id: {{ hit.parent_id }}
|
|
</span>
|
|
<span v-if="hit.has_visual_stats" class="bg-cyan-900 text-cyan-300 px-2 py-1 rounded text-sm">
|
|
Visual
|
|
</span>
|
|
<button
|
|
@click.stop="playChunk(hit)"
|
|
class="bg-green-700 hover:bg-green-600 px-3 py-1 rounded text-sm transition ml-auto"
|
|
>
|
|
▶ Play
|
|
</button>
|
|
</div>
|
|
<p class="text-lg mb-3">{{ hit.text }}</p>
|
|
<!-- Frame Range (精確定位) -->
|
|
<div class="bg-gray-900 p-2 rounded mb-2 text-sm">
|
|
<span class="text-gray-500">Frame:</span>
|
|
<span class="text-white font-mono ml-2">{{ hit.start_frame }}</span>
|
|
<span class="text-gray-600 mx-1">→</span>
|
|
<span class="text-white font-mono">{{ hit.end_frame }}</span>
|
|
<span class="text-gray-500 ml-2">({{ hit.fps.toFixed(2) }} fps)</span>
|
|
</div>
|
|
<!-- Time (參考) -->
|
|
<div class="flex space-x-6 text-sm text-gray-400">
|
|
<span>時間: {{ hit.start.toFixed(2) }}s → {{ hit.end.toFixed(2) }}s</span>
|
|
<span>分數: {{ hit.score.toFixed(3) }}</span>
|
|
</div>
|
|
<div v-if="hit.title || hit.file_path" class="mt-2 text-sm text-gray-500 space-y-1">
|
|
<div v-if="hit.title">標題: {{ hit.title }}</div>
|
|
<div v-if="hit.file_path">檔案: {{ hit.file_path.split('/').pop() }}</div>
|
|
</div>
|
|
</div>
|
|
<div class="text-right">
|
|
<div class="text-2xl font-bold text-blue-400">
|
|
{{ (hit.score * 100).toFixed(1) }}%
|
|
</div>
|
|
<div class="text-sm text-gray-500">匹配度</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- No Results: Chunks -->
|
|
<div v-else-if="searchType === 'chunk' && searched && !loading" class="text-center py-12 text-gray-500">
|
|
找不到符合的結果
|
|
</div>
|
|
|
|
<!-- Results: Traces -->
|
|
<div v-if="searchType === 'trace' && traceResults.length > 0" class="space-y-4">
|
|
<h3 class="text-xl font-semibold">Trace 搜尋結果 ({{ traceResults.length }})</h3>
|
|
<div class="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-5 gap-4">
|
|
<div
|
|
v-for="trace in traceResults"
|
|
:key="trace.trace_id"
|
|
@click="goToTrace(trace)"
|
|
class="bg-gray-800 rounded-lg border border-gray-700 overflow-hidden cursor-pointer hover:border-blue-500 transition"
|
|
>
|
|
<div class="aspect-square bg-gray-700 flex items-center justify-center overflow-hidden">
|
|
<img
|
|
:src="getTraceThumbnail(trace)"
|
|
alt="Trace"
|
|
class="w-full h-full object-cover"
|
|
loading="lazy"
|
|
/>
|
|
</div>
|
|
<div class="p-3 space-y-1">
|
|
<div class="text-sm font-semibold text-blue-300">Trace #{{ trace.trace_id }}</div>
|
|
<div class="text-xs text-gray-400">{{ trace.face_count }} faces, {{ trace.duration_sec?.toFixed(1) || '?' }}s</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- No Results: Traces -->
|
|
<div v-else-if="searchType === 'trace' && searched && !loading" class="text-center py-12 text-gray-500">
|
|
找不到符合的 Trace
|
|
</div>
|
|
</div>
|
|
<!-- Player Modal -->
|
|
<div v-if="player.visible" class="fixed inset-0 bg-black bg-opacity-80 z-50 flex items-center justify-center p-4" @click.self="player.visible = false">
|
|
<div class="bg-gray-900 rounded-lg w-full max-w-4xl border border-gray-700">
|
|
<div class="flex items-center justify-between p-4 border-b border-gray-700">
|
|
<span class="text-white font-semibold">{{ player.title }}</span>
|
|
<span class="text-gray-400 text-sm">{{ player.start.toFixed(1) }}s - {{ player.end.toFixed(1) }}s</span>
|
|
<button @click="player.visible = false" class="text-gray-400 hover:text-white text-xl">×</button>
|
|
</div>
|
|
<video
|
|
ref="videoPlayer"
|
|
:key="player.url"
|
|
:src="player.url"
|
|
class="w-full"
|
|
controls
|
|
autoplay
|
|
@loadedmetadata="seekToStart"
|
|
/>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { ref, computed, onMounted, reactive } from 'vue'
|
|
import { useRouter, useRoute } from 'vue-router'
|
|
import { searchVideos, getVideos, getCurrentConfig } from '@/api/client'
|
|
|
|
const router = useRouter()
|
|
const route = useRoute()
|
|
|
|
const videoPlayer = ref<HTMLVideoElement | null>(null)
|
|
|
|
const player = reactive({
|
|
visible: false,
|
|
url: '',
|
|
title: '',
|
|
start: 0,
|
|
end: 0
|
|
})
|
|
|
|
const playChunk = (hit: SearchHit) => {
|
|
const config = getCurrentConfig()
|
|
player.visible = true
|
|
player.title = hit.text.substring(0, 80)
|
|
player.start = hit.start
|
|
player.end = hit.end
|
|
player.url = `${config.api_base_url}/api/v1/file/${hit.vid}/video?start=${hit.start}&end=${hit.end}`
|
|
}
|
|
|
|
const seekToStart = () => {
|
|
const el = videoPlayer.value
|
|
if (!el || !player.start) return
|
|
if (!player.url.includes('start=')) {
|
|
el.currentTime = player.start
|
|
}
|
|
const duration = Math.max(player.end - player.start, 1)
|
|
setTimeout(() => { if (el.currentTime >= player.end) el.pause() }, duration * 1000 + 2000)
|
|
}
|
|
|
|
async function loadFiles() {
|
|
try {
|
|
const result = await getVideos()
|
|
files.value = result.data || result.files || []
|
|
if (files.value.length > 0 && !selectedFileUuid.value) {
|
|
// Don't auto-select; let user choose "所有檔案" by default
|
|
}
|
|
} catch { /* ignore */ }
|
|
}
|
|
|
|
onMounted(() => {
|
|
localStorage.removeItem('searchState')
|
|
loadFiles()
|
|
|
|
const q = route.query.q as string
|
|
if (q) {
|
|
searchQuery.value = q
|
|
performSearch()
|
|
}
|
|
})
|
|
|
|
interface SearchHit {
|
|
id: string
|
|
vid: string
|
|
start_frame: number
|
|
end_frame: number
|
|
fps: number
|
|
start: number
|
|
end: number
|
|
text: string
|
|
score: number
|
|
title?: string
|
|
file_path?: string
|
|
has_visual_stats?: boolean
|
|
parent_id?: string
|
|
}
|
|
|
|
const searchQuery = ref('')
|
|
const searchMode = ref('vector')
|
|
const searchType = ref('chunk')
|
|
const results = ref<any[]>([])
|
|
const traceResults = ref<any[]>([])
|
|
const loading = ref(false)
|
|
const searched = ref(false)
|
|
const files = ref<any[]>([])
|
|
const selectedFileUuid = ref('')
|
|
|
|
const modeDescription = computed(() => {
|
|
const modes: Record<string, string> = {
|
|
vector: '語意向量搜尋,使用 Qdrant 向量資料庫',
|
|
bm25: '關鍵字搜尋,使用 PostgreSQL tsvector',
|
|
hybrid: '混合搜尋,結合向量與關鍵字',
|
|
smart: '智慧搜尋,使用 Gemma4 LLM 分析查詢'
|
|
}
|
|
return modes[searchMode.value] || ''
|
|
})
|
|
|
|
const performSearch = async () => {
|
|
if (!searchQuery.value.trim()) return
|
|
|
|
loading.value = true
|
|
searched.value = true
|
|
|
|
if (files.value.length === 0) {
|
|
try {
|
|
const result = await getVideos()
|
|
files.value = result.data || result.files || []
|
|
} catch { /* ignore */ }
|
|
}
|
|
|
|
try {
|
|
if (searchType.value === 'chunk') {
|
|
const result = await searchVideos(searchQuery.value, 20, searchMode.value, selectedFileUuid.value || undefined)
|
|
results.value = result.hits
|
|
} else {
|
|
// Trace search — API returns traces with overlapping chunk matches
|
|
const config = getCurrentConfig()
|
|
const params = new URLSearchParams({ query: searchQuery.value, limit: '50' })
|
|
if (selectedFileUuid.value) params.set('uuid', selectedFileUuid.value)
|
|
const resp = await fetch(`${config.api_base_url}/api/v1/search/traces?${params}`, {
|
|
headers: config.api_key ? { 'X-API-Key': config.api_key } : {}
|
|
})
|
|
const data = await resp.json()
|
|
traceResults.value = data.traces || []
|
|
}
|
|
} catch (error) {
|
|
console.error('Search failed:', error)
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
const goToTrace = (trace: any) => {
|
|
router.push(`/traces/${trace.file_uuid || selectedFileUuid.value}/${trace.trace_id}`)
|
|
}
|
|
|
|
const getTraceThumbnail = (trace: any): string => {
|
|
const config = getCurrentConfig()
|
|
const fuid = trace.file_uuid || selectedFileUuid.value
|
|
return `${config.api_base_url}/api/v1/file/${fuid}/thumbnail?frame=${trace.first_frame}`
|
|
}
|
|
|
|
const goToDetail = (uuid: string, chunkId: string) => {
|
|
localStorage.setItem('searchState', JSON.stringify({ query: searchQuery.value, results: results.value }))
|
|
router.push({
|
|
name: 'chunk-detail',
|
|
params: { file_uuid: uuid, chunk_id: chunkId }
|
|
})
|
|
}
|
|
</script>
|