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

@@ -3,10 +3,13 @@ from elasticsearch import Elasticsearch
from schemas.blog_elastic import BLOG_MAPPING from schemas.blog_elastic import BLOG_MAPPING
from config.logging import logger from config.logging import logger
ES_HOSTS = ["http://localhost:9200"] ES_HOSTS = "http://14.103.235.151:9200"
ES_USER = "elastic"
ES_PASSWORD = "19940822Cxx"
BLOG_INDEX = "blog" BLOG_INDEX = "blog"
es = Elasticsearch(hosts=ES_HOSTS) # 注意Python的elasticsearch的版本要和服务器的一致
es = Elasticsearch(ES_HOSTS, http_auth=(ES_USER, ES_PASSWORD))
if not es.indices.exists(index=BLOG_INDEX): if not es.indices.exists(index=BLOG_INDEX):
es.indices.create(index=BLOG_INDEX, body=BLOG_MAPPING) es.indices.create(index=BLOG_INDEX, body=BLOG_MAPPING)

View File

@@ -1 +1 @@
fastapi~=0.115.12 fastapi~=0.115.12

View File

@@ -1,5 +1,5 @@
from datetime import datetime from datetime import datetime
from typing import Optional from typing import Optional, Union
from pydantic import BaseModel from pydantic import BaseModel
@@ -9,12 +9,14 @@ class BlogElastic(BaseModel):
title: str title: str
content: str content: str
category: str category: str
createTime: Optional[Union[datetime, str]] = None
updateTime: Optional[Union[datetime, str]] = None
BLOG_MAPPING = { BLOG_MAPPING = {
"mappings": { "mappings": {
"properties": { "properties": {
"id": {"type": "integer"}, "id": {"type": "long"},
"title": { "title": {
"type": "text", "type": "text",
"analyzer": "ik_max_word", "analyzer": "ik_max_word",

View File

@@ -4,10 +4,10 @@ from config.elastic import es, BLOG_INDEX
from schemas.blog_elastic import BlogElastic from schemas.blog_elastic import BlogElastic
def create_blog(blog: BlogElastic) -> bool: def add_blog_elastic(blog: BlogElastic) -> bool:
now = datetime.now() now = datetime.now()
blog.createTime = now blog.createTime = now.strftime("%Y-%m-%d %H:%M:%S")
blog.updateTime = now blog.updateTime = now.strftime("%Y-%m-%d %H:%M:%S")
es.index( es.index(
index=BLOG_INDEX, index=BLOG_INDEX,
@@ -18,8 +18,9 @@ def create_blog(blog: BlogElastic) -> bool:
return True return True
def update_blog(blog: BlogElastic) -> bool: def update_blog_elastic(blog: BlogElastic) -> bool:
blog.updateTime = datetime.now() now = datetime.now()
blog.updateTime = now.strftime("%Y-%m-%d %H:%M:%S")
es.index( es.index(
index=BLOG_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 models.blog import Blog, BlogCategory, BlogVisit, BlogContent, BlogComment
from schemas.blog import BlogResponse, BlogQuery, BlogCategoryResponse, BlogStatsResponse, \ from schemas.blog import BlogResponse, BlogQuery, BlogCategoryResponse, BlogStatsResponse, \
BlogLatestResponse, BlogCommentCreate, BlogCommentResponse, BlogVisitResponse, BlogCreate, BlogUpdate BlogLatestResponse, BlogCommentCreate, BlogCommentResponse, BlogVisitResponse, BlogCreate, BlogUpdate
from schemas.blog_elastic import BlogElastic
from schemas.pagination import PageResult from schemas.pagination import PageResult
from schemas.paginate_query import paginate_query from schemas.paginate_query import paginate_query
from middleware.exceptions import AppException 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 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.commit()
db.refresh(db_blog) 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 return True

View File

@@ -814,3 +814,47 @@
2025-09-23 14:41:58.649 | INFO  | middleware.exceptions:global_exception_handler:20 - 查询参数: {'currentPage': '1', 'pageSize': '10'} 2025-09-23 14:41:58.649 | INFO  | middleware.exceptions:global_exception_handler:20 - 查询参数: {'currentPage': '1', 'pageSize': '10'}
2025-09-23 14:42:06.096 | INFO  | middleware.exceptions:global_exception_handler:18 - 请求: PUT http://127.0.0.1:8082/blog/21312 2025-09-23 14:42:06.096 | INFO  | middleware.exceptions:global_exception_handler:18 - 请求: PUT http://127.0.0.1:8082/blog/21312
2025-09-23 14:42:06.098 | WARNING  | middleware.exceptions:global_exception_handler:26 - 响应: 401 2025-09-23 14:42:06.098 | WARNING  | middleware.exceptions:global_exception_handler:26 - 响应: 401
2025-09-24 22:06:12.622 | INFO  | middleware.exceptions:global_exception_handler:18 - 请求: GET http://127.0.0.1:8082/
2025-09-24 22:06:12.623 | WARNING  | middleware.exceptions:global_exception_handler:26 - 响应: 404
2025-09-24 22:06:12.664 | INFO  | middleware.exceptions:global_exception_handler:18 - 请求: GET http://127.0.0.1:8082/favicon.ico
2025-09-24 22:06:12.664 | WARNING  | middleware.exceptions:global_exception_handler:26 - 响应: 404
2025-09-24 22:06:16.724 | INFO  | middleware.exceptions:global_exception_handler:18 - 请求: GET http://127.0.0.1:8082/docs
2025-09-24 22:06:18.882 | INFO  | middleware.exceptions:global_exception_handler:18 - 请求: GET http://127.0.0.1:8082/openapi.json
2025-09-24 22:06:21.928 | INFO  | middleware.exceptions:global_exception_handler:18 - 请求: GET http://127.0.0.1:8082/blog/page?currentPage=1&pageSize=10
2025-09-24 22:06:21.928 | INFO  | middleware.exceptions:global_exception_handler:20 - 查询参数: {'currentPage': '1', 'pageSize': '10'}
2025-09-24 22:06:21.973 | CRITICAL | middleware.exceptions:global_exception_handler:38 - 数据库异常: (pymysql.err.OperationalError) (1045, "Access denied for user 'root'@'localhost' (using password: YES)")
(Background on this error at: https://sqlalche.me/e/20/e3q8)
2025-09-24 22:06:57.700 | INFO  | middleware.exceptions:global_exception_handler:18 - 请求: GET http://127.0.0.1:8082/blog/page?currentPage=1&pageSize=10
2025-09-24 22:06:57.700 | INFO  | middleware.exceptions:global_exception_handler:20 - 查询参数: {'currentPage': '1', 'pageSize': '10'}
2025-09-24 22:08:58.230 | INFO  | middleware.exceptions:global_exception_handler:18 - 请求: POST http://127.0.0.1:8082/session
2025-09-24 22:09:21.913 | INFO  | middleware.exceptions:global_exception_handler:18 - 请求: POST http://127.0.0.1:8082/blog
2025-09-24 23:41:42.733 | INFO  | config.elastic:<module>:16 - Elastic索引 blog 创建成功
2025-09-24 23:42:49.929 | INFO  | middleware.exceptions:global_exception_handler:18 - 请求: POST http://127.0.0.1:8082/blog
2025-09-24 23:42:49.986 | CRITICAL | middleware.exceptions:global_exception_handler:44 - 未知异常: 2 validation errors for BlogElastic
content
Input should be a valid string [type=string_type, input_value=<models.blog.BlogContent ...t at 0x00000240A2F85250>, input_type=BlogContent]
For further information visit https://errors.pydantic.dev/2.11/v/string_type
category
Input should be a valid string [type=string_type, input_value=<models.blog.BlogCategory...t at 0x00000240A2F866C0>, input_type=BlogCategory]
For further information visit https://errors.pydantic.dev/2.11/v/string_type
2025-09-24 23:44:55.381 | INFO  | middleware.exceptions:global_exception_handler:18 - 请求: POST http://127.0.0.1:8082/blog
2025-09-24 23:44:55.418 | CRITICAL | middleware.exceptions:global_exception_handler:44 - 未知异常: "BlogElastic" object has no field "createTime"
2025-09-24 23:45:46.168 | INFO  | middleware.exceptions:global_exception_handler:18 - 请求: POST http://127.0.0.1:8082/blog
2025-09-24 23:45:46.266 | CRITICAL | middleware.exceptions:global_exception_handler:44 - 未知异常: BadRequestError(400, 'document_parsing_exception', "[1:7] failed to parse field [id] of type [integer] in document with id '723322241988038'. Preview of field's value: '723322241988038'")
2025-09-24 23:48:20.491 | INFO  | config.elastic:<module>:16 - Elastic索引 blog 创建成功
2025-09-24 23:49:02.067 | INFO  | middleware.exceptions:global_exception_handler:18 - 请求: POST http://127.0.0.1:8082/blog
2025-09-24 23:49:02.106 | CRITICAL | middleware.exceptions:global_exception_handler:44 - 未知异常: BadRequestError(400, 'document_parsing_exception', "[1:88] failed to parse field [createTime] of type [date] in document with id '723323044185542'. Preview of field's value: '2025-09-24T23:49:02.090135'")
2025-09-24 23:50:56.231 | INFO  | middleware.exceptions:global_exception_handler:18 - 请求: POST http://127.0.0.1:8082/blog
2025-09-24 23:52:16.048 | INFO  | middleware.exceptions:global_exception_handler:18 - 请求: POST http://127.0.0.1:8082/blog
2025-09-24 23:54:31.630 | INFO  | middleware.exceptions:global_exception_handler:18 - 请求: POST http://127.0.0.1:8082/blog
2025-09-24 23:58:28.039 | INFO  | middleware.exceptions:global_exception_handler:18 - 请求: POST http://127.0.0.1:8082/blog
2025-09-24 23:58:40.013 | INFO  | middleware.exceptions:global_exception_handler:18 - 请求: POST http://127.0.0.1:8082/blog
2025-09-25 00:02:25.624 | INFO  | config.elastic:<module>:16 - Elastic索引 blog 创建成功
2025-09-25 00:02:28.582 | INFO  | middleware.exceptions:global_exception_handler:18 - 请求: POST http://127.0.0.1:8082/blog
2025-09-25 00:02:28.636 | CRITICAL | middleware.exceptions:global_exception_handler:44 - 未知异常: BadRequestError(400, 'document_parsing_exception', "[1:98] failed to parse field [createTime] of type [date] in document with id '723326347716038'. Preview of field's value: '2025-09-24T16:02:28.620080+00:00'")
2025-09-25 00:03:06.856 | INFO  | middleware.exceptions:global_exception_handler:18 - 请求: POST http://127.0.0.1:8082/blog
2025-09-25 00:03:06.919 | CRITICAL | middleware.exceptions:global_exception_handler:44 - 未知异常: BadRequestError(400, 'document_parsing_exception', "[1:98] failed to parse field [createTime] of type [date] in document with id '723326504547782'. Preview of field's value: '2025-09-24T16:03:06.904955+00:00'")
2025-09-25 00:04:14.532 | INFO  | middleware.exceptions:global_exception_handler:18 - 请求: POST http://127.0.0.1:8082/blog
2025-09-25 00:06:40.224 | INFO  | middleware.exceptions:global_exception_handler:18 - 请求: POST http://127.0.0.1:8082/blog
2025-09-25 00:07:13.887 | INFO  | middleware.exceptions:global_exception_handler:18 - 请求: POST http://127.0.0.1:8082/blog
2025-09-25 00:07:15.655 | INFO  | middleware.exceptions:global_exception_handler:18 - 请求: POST http://127.0.0.1:8082/blog