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