feat:更新elastic模块

This commit is contained in:
2025-09-25 00:08:28 +08:00
parent ef938e6d97
commit 734cd6e82a
6 changed files with 70 additions and 10 deletions

View File

@@ -4,10 +4,10 @@ from config.elastic import es, BLOG_INDEX
from schemas.blog_elastic import BlogElastic
def create_blog(blog: BlogElastic) -> bool:
def add_blog_elastic(blog: BlogElastic) -> bool:
now = datetime.now()
blog.createTime = now
blog.updateTime = now
blog.createTime = now.strftime("%Y-%m-%d %H:%M:%S")
blog.updateTime = now.strftime("%Y-%m-%d %H:%M:%S")
es.index(
index=BLOG_INDEX,
@@ -18,8 +18,9 @@ def create_blog(blog: BlogElastic) -> bool:
return True
def update_blog(blog: BlogElastic) -> bool:
blog.updateTime = datetime.now()
def update_blog_elastic(blog: BlogElastic) -> bool:
now = datetime.now()
blog.updateTime = now.strftime("%Y-%m-%d %H:%M:%S")
es.index(
index=BLOG_INDEX,

View File

@@ -7,9 +7,11 @@ from sqlalchemy.orm import Session
from models.blog import Blog, BlogCategory, BlogVisit, BlogContent, BlogComment
from schemas.blog import BlogResponse, BlogQuery, BlogCategoryResponse, BlogStatsResponse, \
BlogLatestResponse, BlogCommentCreate, BlogCommentResponse, BlogVisitResponse, BlogCreate, BlogUpdate
from schemas.blog_elastic import BlogElastic
from schemas.pagination import PageResult
from schemas.paginate_query import paginate_query
from middleware.exceptions import AppException
from service.blog_elastic_service import add_blog_elastic
from utils.blog_utils import get_blog_summary, get_word_count, get_read_duration
@@ -126,6 +128,14 @@ def add_blog(db: Session, blog: BlogCreate) -> bool:
db.commit()
db.refresh(db_blog)
blog_elastic = BlogElastic(
id=db_blog.id,
title=db_blog.title,
content=blog.content,
category=blog.category
)
add_blog_elastic(blog_elastic)
return True