feat:增加Elasticsearch功能
This commit is contained in:
13
config/elastic.py
Normal file
13
config/elastic.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from elasticsearch import Elasticsearch
|
||||
|
||||
from schemas.blog_elastic import BLOG_MAPPING
|
||||
from config.logging import logger
|
||||
|
||||
ES_HOSTS = ["http://localhost:9200"]
|
||||
BLOG_INDEX = "blog"
|
||||
|
||||
es = Elasticsearch(hosts=ES_HOSTS)
|
||||
|
||||
if not es.indices.exists(index=BLOG_INDEX):
|
||||
es.indices.create(index=BLOG_INDEX, body=BLOG_MAPPING)
|
||||
logger.info(f"Elastic索引 {BLOG_INDEX} 创建成功")
|
||||
@@ -35,13 +35,13 @@ async def global_exception_handler(request: Request, call_next):
|
||||
|
||||
except SQLAlchemyError as e:
|
||||
# 记录数据库异常
|
||||
#logger.critical(f"数据库异常: {str(e)}", exc_info=True)
|
||||
logger.critical(f"数据库异常: {str(e)}", exc_info=True)
|
||||
return JSONResponse(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
content={"message": "数据库操作失败", "details": str(e)})
|
||||
|
||||
except Exception as e:
|
||||
# 记录未知异常(带堆栈信息)
|
||||
#logger.critical(f"未知异常: {str(e)}", exc_info=True)
|
||||
logger.critical(f"未知异常: {str(e)}", exc_info=True)
|
||||
return JSONResponse(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
content={"code": 500, "message": "服务器内部错误", "details": str(e)})
|
||||
|
||||
|
||||
@@ -1 +1 @@
|
||||
fastapi~=0.115.12
|
||||
fastapi~=0.115.12
|
||||
53
schemas/blog_elastic.py
Normal file
53
schemas/blog_elastic.py
Normal file
@@ -0,0 +1,53 @@
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class BlogElastic(BaseModel):
|
||||
id: int
|
||||
title: str
|
||||
content: str
|
||||
category: str
|
||||
|
||||
|
||||
BLOG_MAPPING = {
|
||||
"mappings": {
|
||||
"properties": {
|
||||
"id": {"type": "integer"},
|
||||
"title": {
|
||||
"type": "text",
|
||||
"analyzer": "ik_max_word",
|
||||
"fields": {
|
||||
"keyword": {"type": "keyword"}
|
||||
}
|
||||
},
|
||||
"content": {
|
||||
"type": "text",
|
||||
"analyzer": "ik_max_word"
|
||||
},
|
||||
"category": {
|
||||
"type": "keyword"
|
||||
},
|
||||
"createTime": {
|
||||
"type": "date",
|
||||
"format": "yyyy-MM-dd HH:mm:ss||epoch_millis"
|
||||
},
|
||||
"updateTime": {
|
||||
"type": "date",
|
||||
"format": "yyyy-MM-dd HH:mm:ss||epoch_millis"
|
||||
}
|
||||
}
|
||||
},
|
||||
"settings": {
|
||||
"number_of_shards": 1,
|
||||
"number_of_replicas": 1,
|
||||
"analysis": {
|
||||
"analyzer": {
|
||||
"ik_max_word": {
|
||||
"type": "ik_max_word"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
36
service/blog_elastic_service.py
Normal file
36
service/blog_elastic_service.py
Normal file
@@ -0,0 +1,36 @@
|
||||
from datetime import datetime
|
||||
|
||||
from config.elastic import es, BLOG_INDEX
|
||||
from schemas.blog_elastic import BlogElastic
|
||||
|
||||
|
||||
def create_blog(blog: BlogElastic) -> bool:
|
||||
now = datetime.now()
|
||||
blog.createTime = now
|
||||
blog.updateTime = now
|
||||
|
||||
es.index(
|
||||
index=BLOG_INDEX,
|
||||
id=blog.id,
|
||||
body=blog.model_dump()
|
||||
)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def update_blog(blog: BlogElastic) -> bool:
|
||||
blog.updateTime = datetime.now()
|
||||
|
||||
es.index(
|
||||
index=BLOG_INDEX,
|
||||
id=blog.id,
|
||||
body=blog.model_dump()
|
||||
)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def delete_blog(blog_id: int) -> bool:
|
||||
es.delete(index=BLOG_INDEX, id=blog_id)
|
||||
|
||||
return True
|
||||
Reference in New Issue
Block a user