"use client" import type React from "react" import { useState, useCallback } from "react" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Input } from "@/components/ui/input" import { Button } from "@/components/ui/button" import { Badge } from "@/components/ui/badge" import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs" import { Avatar } from "@/components/ui/avatar" import { Progress } from "@/components/ui/progress" import { Switch } from "@/components/ui/switch" import { Label } from "@/components/ui/label" import { Search, Sparkles, Clock, TrendingUp, Users, Brain, Zap, Download, RefreshCw, Eye, BarChart3, Lightbulb } from 'lucide-react' import { TooltipHelp, TooltipProvider } from "@/components/ui/tooltip-help" interface SearchResult { id: string type: "user" | "traffic" | "insight" title: string description: string tags: string[] relevanceScore: number updatedAt: string metadata?: Record } interface SearchResponse { results: SearchResult[] stats: { totalResults: number queryTime: number suggestions: string[] filters: Record } hasMore: boolean } export default function IntelligentSearchPage() { const [searchQuery, setSearchQuery] = useState("") const [searchType, setSearchType] = useState<"all" | "user" | "traffic">("all") const [useAI, setUseAI] = useState(true) const [includeInsights, setIncludeInsights] = useState(true) const [isSearching, setIsSearching] = useState(false) const [searchResults, setSearchResults] = useState(null) const [searchHistory, setSearchHistory] = useState([]) const [activeTab, setActiveTab] = useState("search") const [rfmHighValueOnly, setRfmHighValueOnly] = useState(false); const detectQueryType = (q: string) => { const s = q.trim() if (/^\d{5,11}$/.test(s)) { if (/^1[3-9]\d{9}$/.test(s) || /^\d{3}\*{4}\d{4}$/.test(s)) return "手机号" return "QQ号" } if (/^[A-F0-9]{14}$/.test(s)) return "MEID" if (/^0{6,}$/.test(s) || s.includes("000000")) return "流量词" return "通用" } const queryType = detectQueryType(searchQuery) // 执行搜索 const performSearch = useCallback( async (query: string) => { if (!query.trim()) return setIsSearching(true) try { const response = await fetch("/api/search", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ query, type: searchType, options: { useAI, includeInsights, rfmTag: includeInsights ? "高" : undefined, limit: 50, }, }), }) if (!response.ok) { throw new Error("搜索失败") } const data: SearchResponse = await response.json() setSearchResults(data) // 添加到搜索历史 setSearchHistory((prev) => { const newHistory = [query, ...prev.filter((h) => h !== query)].slice(0, 10) return newHistory }) } catch (error) { console.error("搜索错误:", error) } finally { setIsSearching(false) } }, [searchType, useAI, includeInsights], ) // 处理搜索输入 const handleSearch = () => { performSearch(searchQuery) } // 处理回车键搜索 const handleKeyPress = (e: React.KeyboardEvent) => { if (e.key === "Enter") { handleSearch() } } // 获取类型图标 const getTypeIcon = (type: string) => { switch (type) { case "user": return case "traffic": return case "insight": return default: return } } // 获取类型标签颜色 const getTypeBadgeColor = (type: string) => { switch (type) { case "user": return "bg-blue-100 text-blue-800" case "traffic": return "bg-green-100 text-green-800" case "insight": return "bg-purple-100 text-purple-800" default: return "bg-gray-100 text-gray-800" } } return (
{/* 页面标题 */}

智能搜索引擎

AI驱动的亚秒级数据搜索与洞察分析

{/* 搜索配置卡片 */}
{/* 主搜索框 */}
setSearchQuery(e.target.value)} onKeyPress={handleKeyPress} />
识别: {queryType} {queryType === "流量词" && 优先匹配流量池}
{/* 搜索选项 */}
{/* 搜索历史 */} {searchHistory.length > 0 && (
最近搜索:
{searchHistory.slice(0, 5).map((query, index) => ( ))}
)}
{/* 搜索结果 */} 搜索结果 AI洞察 搜索分析 搜索历史 {/* 搜索结果标签页 */} {searchResults && (
搜索结果 {searchResults.stats.totalResults} 个结果
查询耗时: {searchResults.stats.queryTime}ms
{searchResults.stats.suggestions.length > 0 && (
相关建议:
{searchResults.stats.suggestions.map((suggestion, index) => ( ))}
)}
{searchResults.results.length > 0 ? (
{searchResults.results.map((result) => (
{/* 类型图标 */}
{result.type === "user" ? (
) : (
{getTypeIcon(result.type)}
)}
{/* 内容 */}

{result.title}

{result.type === "user" ? "用户" : result.type === "traffic" ? "流量" : "洞察"} 相关性: {(result.relevanceScore * 100).toFixed(1)}%

{result.description}

{/* 标签 */}
{result.tags.slice(0, 5).map((tag, index) => ( {tag} ))}
{/* 元数据 */}
{new Date(result.updatedAt).toLocaleString("zh-CN")} {result.metadata?.searchType && 类型: {result.metadata.searchType}}
{/* 操作按钮 */}
))} {/* 加载更多 */} {searchResults.hasMore && (
)}
) : (

未找到相关结果

尝试使用其他关键词或调整搜索条件

)}
)} {/* 空状态 */} {!searchResults && (

开始智能搜索

输入关键词开始搜索,支持自然语言查询和AI增强分析

{["高价值用户", "流量趋势", "用户行为分析", "RFM分群"].map((example) => ( ))}
)}
{/* AI洞察标签页 */} AI智能洞察 AI分析搜索数据,提供深度业务洞察 {searchResults?.results.filter((r) => r.type === "insight").length > 0 ? (
{searchResults.results .filter((r) => r.type === "insight") .map((insight) => (

{insight.title}

{insight.description}

置信度: {(insight.relevanceScore * 100).toFixed(1)}% {new Date(insight.updatedAt).toLocaleString("zh-CN")}
))}
) : (

执行搜索后,AI将为您生成智能洞察

)}
{/* 搜索分析标签页 */}
查询性能
{searchResults?.stats.queryTime || 0}ms

目标: <1000ms

结果质量
{searchResults ? Math.round( (searchResults.results.reduce((sum, r) => sum + r.relevanceScore, 0) / searchResults.results.length) * 100, ) : 0} %
sum + r.relevanceScore, 0) / searchResults.results.length) * 100, ) : 0 } className="h-2" />

平均相关性评分

AI增强率
{useAI ? "100" : "0"}%

AI功能启用状态

{/* 搜索历史标签页 */} 搜索历史 您最近的搜索记录 {searchHistory.length > 0 ? (
{searchHistory.map((query, index) => (
{ setSearchQuery(query) performSearch(query) }} >
{query}
))}
) : (

暂无搜索历史

)}
) }