From 734cd6e82a211a09aaaa30ba0f9e3b5658312106 Mon Sep 17 00:00:00 2001 From: Cxx0822 <1556464090@qq.com> Date: Thu, 25 Sep 2025 00:08:28 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=9B=B4=E6=96=B0elastic=E6=A8=A1?= =?UTF-8?q?=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/elastic.py | 7 ++++-- requirements.txt | 2 +- schemas/blog_elastic.py | 6 +++-- service/blog_elastic_service.py | 11 +++++---- service/blog_service.py | 10 ++++++++ sys.stdout | 44 +++++++++++++++++++++++++++++++++ 6 files changed, 70 insertions(+), 10 deletions(-) diff --git a/config/elastic.py b/config/elastic.py index 6d3fdd2..6c07872 100644 --- a/config/elastic.py +++ b/config/elastic.py @@ -3,10 +3,13 @@ from elasticsearch import Elasticsearch from schemas.blog_elastic import BLOG_MAPPING 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" -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): es.indices.create(index=BLOG_INDEX, body=BLOG_MAPPING) diff --git a/requirements.txt b/requirements.txt index 7de8262..769fb15 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -fastapi~=0.115.12 sqlalchemy~=2.0.41 pydantic~=2.11.4 PyJWT~=2.10.1 passlib~=1.7.4 loguru~=0.7.3 pydantic-settings~=2.9.1 pymysql~=1.1.1 python-multipart~=0.0.20 uvicorn~=0.23.0 elasticsearch~=9.1.1 # pip download -r requirements.txt -d ./packages --only-binary=:all: --platform manylinux2014_x86_64 -i https://pypi.tuna.tsinghua.edu.cn/simple \ No newline at end of file +fastapi~=0.115.12 sqlalchemy~=2.0.41 pydantic~=2.11.4 PyJWT~=2.10.1 passlib~=1.7.4 loguru~=0.7.3 pydantic-settings~=2.9.1 pymysql~=1.1.1 python-multipart~=0.0.20 uvicorn~=0.23.0 elasticsearch~=8.12.0 # pip download -r requirements.txt -d ./packages --only-binary=:all: --platform manylinux2014_x86_64 -i https://pypi.tuna.tsinghua.edu.cn/simple \ No newline at end of file diff --git a/schemas/blog_elastic.py b/schemas/blog_elastic.py index 8258c74..e769533 100644 --- a/schemas/blog_elastic.py +++ b/schemas/blog_elastic.py @@ -1,5 +1,5 @@ from datetime import datetime -from typing import Optional +from typing import Optional, Union from pydantic import BaseModel @@ -9,12 +9,14 @@ class BlogElastic(BaseModel): title: str content: str category: str + createTime: Optional[Union[datetime, str]] = None + updateTime: Optional[Union[datetime, str]] = None BLOG_MAPPING = { "mappings": { "properties": { - "id": {"type": "integer"}, + "id": {"type": "long"}, "title": { "type": "text", "analyzer": "ik_max_word", diff --git a/service/blog_elastic_service.py b/service/blog_elastic_service.py index c8d14b0..8ed2237 100644 --- a/service/blog_elastic_service.py +++ b/service/blog_elastic_service.py @@ -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, diff --git a/service/blog_service.py b/service/blog_service.py index 14cba26..790775a 100644 --- a/service/blog_service.py +++ b/service/blog_service.py @@ -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 diff --git a/sys.stdout b/sys.stdout index 18fd478..f2c7343 100644 --- a/sys.stdout +++ b/sys.stdout @@ -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: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-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::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=, 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=, 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::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::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