Skip to content

Commit 14400b7

Browse files
authored
feat: 添加es_search接口 (#6)
1 parent aac7cf2 commit 14400b7

File tree

4 files changed

+647
-0
lines changed

4 files changed

+647
-0
lines changed

app/service/elasticsearch.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,12 @@ def search(self, parameters: SearchParameters) -> SearchResult:
374374
response, search_time_ms, parameters.limit, search_conditions
375375
)
376376

377+
def es_search(
378+
self, index_name: str, query: dict[str, Any]
379+
) -> dict[str, Any]:
380+
resp = self._client.search(index=index_name, body=query)
381+
return dict(resp.body)
382+
377383
@staticmethod
378384
def _classify_conditions(
379385
conditions: list[SearchCondition],

app/web/document.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import shutil
1818
import uuid
1919
from pathlib import Path
20+
from typing import Any
2021
from urllib.parse import urlparse
2122

2223
from elasticsearch import NotFoundError
@@ -38,6 +39,7 @@
3839
from app.service.elasticsearch import ElasticsearchService
3940
from app.utils.converters import SearchConverter
4041
from app.web.vo import (
42+
ESSearchRequest,
4143
FileUploadResponse,
4244
SaveRequest,
4345
SaveResponse,
@@ -116,6 +118,11 @@ def register_routes(self) -> None:
116118
summary="在知识库中进行搜索",
117119
)(self.search)
118120

121+
self._router.post(
122+
"/es_search",
123+
summary="使用传递过来的es查询语句在知识库中直接搜索",
124+
)(self.es_search)
125+
119126
self._router.post(
120127
"/documents/save",
121128
response_model=SaveResponse,
@@ -438,6 +445,21 @@ async def search(self, request: SearchRequest) -> SearchResponse:
438445
logger.error(f"❌ 搜索失败: {e}", exc_info=True)
439446
raise HTTPException(status_code=500, detail="搜索处理失败") from e
440447

448+
async def es_search(self, request: ESSearchRequest) -> dict[str, Any]:
449+
"""通过ES语法搜索文档接口"""
450+
try:
451+
logger.info(
452+
f"🔍 收到ES搜索请求: index='{request.index}', query='{request.query}'"
453+
)
454+
return self._service.es_search(request.index, request.query)
455+
except NotFoundError as e:
456+
raise HTTPException(
457+
status_code=404, detail=f"索引 {request.index} 不存在"
458+
) from e
459+
except Exception as e:
460+
logger.error(f"❌ 搜索失败: {e}", exc_info=True)
461+
raise HTTPException(status_code=500, detail="搜索处理失败") from e
462+
441463
async def save(self, request: SaveRequest) -> SaveResponse:
442464
"""保存JSON格式文档到指定的Elasticsearch索引"""
443465
try:

app/web/vo.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ def validate_query(cls, v: Query, info: ValidationInfo) -> Query:
130130
return v
131131

132132

133+
class ESSearchRequest(BaseModel):
134+
index: str = Field(..., min_length=1, description="ES索引名称")
135+
query: dict[str, Any] = Field(..., description="符合ES语法规范的查询语句")
136+
137+
133138
class VectorHybridSearchResult(BaseModel):
134139
"""向量+全文混合搜索结果"""
135140

0 commit comments

Comments
 (0)