AI搜尋引擎重構實戰:語意搜尋與LLM增強檢索架構
AI与大数据
摘要
- AI搜尋正在顛覆傳統搜尋:從「關鍵字匹配」到「語意理解+智慧生成」,2026年AI搜尋市場規模突破$50億
- 語意搜尋3層架構:Query理解→向量檢索→LLM重排+生成,檢索精度提升35%+
- 向量檢索引擎選型:Milvus適合大規模、Qdrant適合中小規模、Elasticsearch 8.x適合混合搜尋
- LLM增強排序(Reranking)是AI搜尋的殺手鐧:Cross-Encoder重排可將NDCG提升15%-25%
- 本文提供從傳統搜尋到AI搜尋的完整重構方案,含Elasticsearch+Milvus雙引擎架構
目錄
AI搜尋:搜尋的下一代範式
傳統搜尋 vs AI搜尋
| 維度 | 傳統搜尋(BM25) | AI搜尋(語意+LLM) |
|---|---|---|
| 匹配方式 | 關鍵字精確匹配 | 語意相似度匹配 |
| 查詢理解 | 分詞+同義詞 | 意圖識別+實體抽取 |
| 排序訊號 | TF-IDF+PageRank | 語意相關性+使用者意圖 |
| 結果呈現 | 10條藍色連結 | 直接答案+引用來源 |
| 複雜查詢 | 差(需精確關鍵字) | 強(理解自然語言) |
| 即時性 | 高(索引預構建) | 中(需即時推理) |
AI搜尋市場格局
| 產品 | 類型 | 核心技術 | 特點 |
|---|---|---|---|
| Perplexity | AI原生搜尋 | RAG+LLM生成 | 引用來源 |
| Google SGE | 傳統+AI增強 | BERT+MUM | 生態最強 |
| Bing Chat | 傳統+AI增強 | GPT-4+Prometheus | 微軟生態 |
| 秘塔AI搜尋 | AI原生搜尋 | 自研RAG | 中文最佳化 |
| 夸克AI搜尋 | 傳統+AI增強 | 自研模型 | 行動端優先 |
語意搜尋3層架構
┌──────────────────────────────────────────────────────────────┐
│ 語意搜尋3層架構 │
│ │
│ Layer 1: Query理解 │
│ ┌──────────────────────────────────────────────────────┐ │
│ │ 使用者查詢 → 意圖識別 → 實體抽取 → Query改寫 │ │
│ │ 「K8s怎麼配GPU」→ 意圖:技術教學 → 實體:K8s,GPU │ │
│ │ → 改寫:「Kubernetes GPU排程設定教學」 │ │
│ └──────────────────────────────────────────────────────┘ │
│ ↓ │
│ Layer 2: 多路檢索 │
│ ┌──────────────────────────────────────────────────────┐ │
│ │ 向量檢索(Milvus) + 關鍵字檢索(ES) + 知識圖譜(Neo4j) │ │
│ │ 各路Top-K結果合併 → 去重 → 候選集 │ │
│ └──────────────────────────────────────────────────────┘ │
│ ↓ │
│ Layer 3: LLM重排+生成 │
│ ┌──────────────────────────────────────────────────────┐ │
│ │ Cross-Encoder重排 → Top-N → LLM生成答案+引用來源 │ │
│ └──────────────────────────────────────────────────────┘ │
└──────────────────────────────────────────────────────────────┘
Query理解實作
class QueryUnderstanding:
def __init__(self, llm_client, embedding_model):
self.llm = llm_client
self.embedding = embedding_model
async def understand(self, query: str) -> dict:
intent = await self._classify_intent(query)
entities = await self._extract_entities(query)
rewritten = await self._rewrite_query(query, intent, entities)
query_embedding = self._embed(rewritten)
return {
"original_query": query,
"intent": intent,
"entities": entities,
"rewritten_query": rewritten,
"embedding": query_embedding,
}
async def _classify_intent(self, query: str) -> str:
response = self.llm.chat.completions.create(
model="Qwen/Qwen2.5-7B-Instruct",
messages=[{
"role": "user",
"content": f"分類以下搜尋查詢的意圖,只輸出類別名:\n查詢:{query}\n類別:技術教學/問題排查/產品對比/概念解釋/最新資訊"
}],
temperature=0.0, max_tokens=10,
)
return response.choices[0].message.content.strip()
async def _rewrite_query(self, query: str, intent: str, entities: list) -> str:
response = self.llm.chat.completions.create(
model="Qwen/Qwen2.5-7B-Instruct",
messages=[{
"role": "user",
"content": f"將以下搜尋查詢改寫為更適合語意檢索的形式,只輸出改寫結果:\n原始查詢:{query}\n意圖:{intent}\n實體:{entities}"
}],
temperature=0.0, max_tokens=100,
)
return response.choices[0].message.content.strip()
向量檢索引擎選型與部署
引擎對比
| 維度 | Milvus 2.5 | Qdrant 1.13 | ES 8.x(kNN) |
|---|---|---|---|
| 向量索引 | HNSW/IVF/DiskANN | HNSW | HNSW |
| 混合搜尋 | ✅(BM25+向量) | ✅(稀疏+稠密) | ✅(原生BM25+kNN) |
| 規模 | 1億+ | <5000萬 | 1億+ |
| 延遲(P50) | 5ms | 3ms | 8ms |
| 運維複雜度 | 高 | 低 | 中 |
| 生態 | AI/ML生態 | Rust生態 | 企業搜尋生態 |
ES 8.x kNN搜尋
from elasticsearch import Elasticsearch
es = Elasticsearch("http://localhost:9200")
mapping = {
"mappings": {
"properties": {
"title": {"type": "text", "analyzer": "ik_max_word"},
"content": {"type": "text", "analyzer": "ik_max_word"},
"embedding": {
"type": "dense_vector",
"dims": 1536,
"index": True,
"similarity": "cosine",
"index_options": {"type": "hnsw", "m": 32, "ef_construction": 256},
},
"source": {"type": "keyword"},
"published_at": {"type": "date"},
}
}
}
es.indices.create(index="knowledge_base", body=mapping)
def hybrid_search(query: str, query_embedding: list, top_k: int = 20):
result = es.search(
index="knowledge_base",
body={
"size": top_k,
"query": {
"bool": {
"should": [
{
"script_score": {
"query": {"match_all": {}},
"script": {
"source": "cosineSimilarity(params.query_vector, 'embedding') + 1.0",
"params": {"query_vector": query_embedding},
},
}
},
{
"multi_match": {
"query": query,
"fields": ["title^3", "content"],
"type": "best_fields",
}
},
]
}
},
},
)
return [hit["_source"] for hit in result["hits"]["hits"]]
LLM增強排序:Reranking
Cross-Encoder Reranking
from sentence_transformers import CrossEncoder
class SemanticReranker:
def __init__(self, model_name: str = "BAAI/bge-reranker-v2-m3"):
self.model = CrossEncoder(model_name, max_length=512)
def rerank(self, query: str, documents: list[dict], top_k: int = 5) -> list[dict]:
pairs = [(query, doc["content"][:512]) for doc in documents]
scores = self.model.predict(pairs)
for doc, score in zip(documents, scores):
doc["rerank_score"] = float(score)
documents.sort(key=lambda x: x["rerank_score"], reverse=True)
return documents[:top_k]
LLM生成答案
class AISearchGenerator:
def __init__(self, llm_client):
self.llm = llm_client
async def generate_answer(self, query: str, contexts: list[dict]) -> dict:
context_text = "\n\n".join(
f"[來源{i+1}] {c['title']}\n{c['content'][:500]}"
for i, c in enumerate(contexts)
)
response = self.llm.chat.completions.create(
model="Qwen/Qwen2.5-7B-Instruct",
messages=[{
"role": "system",
"content": "基於提供的搜尋結果回答使用者問題。每個事實必須標註來源編號。如果搜尋結果不足以回答,請說明。"
}, {
"role": "user",
"content": f"問題:{query}\n\n搜尋結果:\n{context_text}"
}],
temperature=0.3, max_tokens=1024,
)
answer = response.choices[0].message.content
return {"answer": answer, "sources": contexts[:5]}
Reranking效果
| 方法 | NDCG@10 | MRR@10 | 延遲 |
|---|---|---|---|
| 純BM25 | 0.62 | 0.58 | 5ms |
| 純向量檢索 | 0.71 | 0.67 | 8ms |
| BM25+向量混合 | 0.78 | 0.74 | 12ms |
| 混合+Cross-Encoder | 0.89 | 0.85 | 35ms |
AI搜尋生產部署:ES+Milvus雙引擎
部署架構
apiVersion: apps/v1
kind: Deployment
metadata:
name: ai-search-api
namespace: ai-search
spec:
replicas: 3
selector:
matchLabels:
app: ai-search-api
template:
spec:
containers:
- name: search-api
image: myregistry/ai-search-api:v1.0
ports:
- containerPort: 8080
resources:
requests:
cpu: "2"
memory: 4Gi
limits:
cpu: "4"
memory: 8Gi
env:
- name: ELASTICSEARCH_URL
value: "http://elasticsearch:9200"
- name: MILVUS_URI
value: "http://milvus-svc:19530"
- name: LLM_URL
value: "http://vllm:8000/v1"
- name: RERANKER_MODEL
value: "BAAI/bge-reranker-v2-m3"
搜尋效能基準
| 指標 | 傳統搜尋 | AI搜尋 |
|---|---|---|
| 檢索延遲(P50) | 5ms | 35ms |
| 檢索精度(NDCG@10) | 0.62 | 0.89 |
| 答案生成延遲 | N/A | 800ms |
| 使用者滿意度 | 3.2/5 | 4.5/5 |
| 零結果率 | 12% | 2% |
總結與延伸閱讀
AI搜尋正在從「關鍵字匹配」進化到「語意理解+智慧生成」。3層架構(Query理解→多路檢索→LLM重排+生成)將檢索精度從0.62提升到0.89。ES+Milvus雙引擎是生產部署的標配。
重構要點回顧:
- AI搜尋3層架構:Query理解→多路檢索→LLM重排+生成
- Query改寫是語意搜尋的第一步,將口語化查詢轉為檢索友好的形式
- ES 8.x kNN支援混合搜尋,適合傳統搜尋升級AI搜尋
- Cross-Encoder Reranking可將NDCG提升15%-25%
- AI搜尋延遲35ms(檢索)+800ms(生成),需權衡延遲與精度
延伸閱讀:
- 向量資料庫生產調優實戰 — AI搜尋的向量檢索層調優
- GraphRAG實戰:知識圖譜增強RAG — 知識圖譜增強搜尋
- 大模型推理加速基準測試 — AI搜尋的推理後端最佳化
權威參考:
本站提供瀏覽器本地工具,免註冊即可試用 →
#AI搜索引擎#语义搜索#向量检索引擎#LLM搜索增强#搜索架构重构#2026