This commit is contained in:
2025-08-19 13:31:18 +08:00
parent 35c5abd81a
commit 96dbe96921
5 changed files with 83 additions and 37 deletions

View File

@@ -1,10 +1,10 @@
# Elasticsearch配置 # Elasticsearch配置
ES_CONFIG = { ES_CONFIG = {
"hosts": "https://127.0.0.1:9200", 'hosts': ['https://localhost:9200'],
"basic_auth": ("elastic", "jv9h8uwRrRxmDi1dq6u8"), 'basic_auth': ('elastic', 'jv9h8uwRrRxmDi1dq6u8'),
"verify_certs": False, 'verify_certs': False,
"ssl_show_warn": False, 'index_name': 'ds_db', # 默认索引名称
"index_name": "ds_kb" 'student_info_index': 'student_info' # 添加student_info索引名称配置
} }
# 嵌入向量模型 # 嵌入向量模型

View File

@@ -4,8 +4,29 @@ from ElasticSearch.Utils.EsSearchUtil import EsSearchUtil
# 创建EsSearchUtil实例 # 创建EsSearchUtil实例
search_util = EsSearchUtil(Config.ES_CONFIG) search_util = EsSearchUtil(Config.ES_CONFIG)
# 调用重建mapping方法 def rebuild_index(index_name):
if search_util.rebuild_mapping(): """
print("重建mapping操作完成成功") 重建指定的索引
else:
print("重建mapping操作失败") 参数:
index_name: 要重建的索引名称
返回:
bool: 操作是否成功
"""
print(f"开始重建索引: {index_name}")
if search_util.rebuild_mapping(index_name):
print(f"重建索引 '{index_name}' 操作成功")
return True
else:
print(f"重建索引 '{index_name}' 操作失败")
return False
if __name__ == "__main__":
# 重建ds_db索引
rebuild_index(Config.ES_CONFIG['index_name'])
# 重建student_info索引
rebuild_index(Config.ES_CONFIG['student_info_index'])
print("所有索引重建操作完成")

View File

@@ -6,7 +6,6 @@ import time
import requests import requests
from Config.Config import ES_CONFIG
from ElasticSearch.Utils.ElasticsearchConnectionPool import ElasticsearchConnectionPool from ElasticSearch.Utils.ElasticsearchConnectionPool import ElasticsearchConnectionPool
from langchain_core.documents import Document from langchain_core.documents import Document
from langchain_text_splitters import RecursiveCharacterTextSplitter from langchain_text_splitters import RecursiveCharacterTextSplitter
@@ -41,10 +40,13 @@ class EsSearchUtil:
self.index_name = es_config['index_name'] self.index_name = es_config['index_name']
logger.info(f"EsSearchUtil初始化成功索引名称: {self.index_name}") logger.info(f"EsSearchUtil初始化成功索引名称: {self.index_name}")
def rebuild_mapping(self): def rebuild_mapping(self, index_name=None):
""" """
重建Elasticsearch索引和mapping结构 重建Elasticsearch索引和mapping结构
参数:
index_name: 可选,指定要重建的索引名称,默认使用初始化时的索引名称
返回: 返回:
bool: 操作是否成功 bool: 操作是否成功
""" """
@@ -52,15 +54,32 @@ class EsSearchUtil:
# 从连接池获取连接 # 从连接池获取连接
conn = self.es_pool.get_connection() conn = self.es_pool.get_connection()
# 使用指定的索引名称或默认索引名称
target_index = index_name if index_name else self.index_name
logger.info(f"开始重建索引: {target_index}")
# 定义mapping结构 # 定义mapping结构
if target_index == 'student_info':
mapping = {
"mappings": {
"properties": {
"user_id": {"type": "keyword"},
"grade": {"type": "keyword"},
"recent_questions": {"type": "text"},
"learned_knowledge": {"type": "text"},
"updated_at": {"type": "date"}
}
}
}
else:
mapping = { mapping = {
"mappings": { "mappings": {
"properties": { "properties": {
"embedding": { "embedding": {
"type": "dense_vector", "type": "dense_vector",
"dims": Config.EMBED_DIM, # embedding维度为Config.EMBED_DIM "dims": Config.EMBED_DIM,
"index": True, "index": True,
"similarity": "l2_norm" # 使用L2距离 "similarity": "l2_norm"
}, },
"user_input": {"type": "text"}, "user_input": {"type": "text"},
"tags": { "tags": {
@@ -75,20 +94,26 @@ class EsSearchUtil:
} }
# 检查索引是否存在,存在则删除 # 检查索引是否存在,存在则删除
if conn.indices.exists(index=self.index_name): if conn.indices.exists(index=target_index):
conn.indices.delete(index=self.index_name) conn.indices.delete(index=target_index)
logger.info(f"删除已存在的索引 '{self.index_name}'") logger.info(f"删除已存在的索引 '{target_index}'")
print(f"删除已存在的索引 '{self.index_name}'") print(f"删除已存在的索引 '{target_index}'")
# 创建索引和mapping # 创建索引和mapping
conn.indices.create(index=self.index_name, body=mapping) conn.indices.create(index=target_index, body=mapping)
logger.info(f"索引 '{self.index_name}' 创建成功mapping结构已设置") logger.info(f"索引 '{target_index}' 创建成功mapping结构已设置")
print(f"索引 '{self.index_name}' 创建成功mapping结构已设置。") print(f"索引 '{target_index}' 创建成功mapping结构已设置。")
return True return True
except Exception as e: except Exception as e:
logger.error(f"重建mapping失败: {str(e)}") logger.error(f"重建索引 '{target_index}' 失败: {str(e)}")
print(f"重建mapping失败: {e}") print(f"重建索引 '{target_index}' 失败: {e}")
# 提供认证错误的具体提示
if 'AuthenticationException' in str(e):
print("认证失败提示: 请检查Config.py中的ES_CONFIG配置确保用户名和密码正确。")
logger.error("认证失败: 请检查Config.py中的ES_CONFIG配置确保用户名和密码正确。")
return False return False
finally: finally:
# 释放连接回连接池 # 释放连接回连接池