'commit'
This commit is contained in:
@@ -4,8 +4,29 @@ from ElasticSearch.Utils.EsSearchUtil import EsSearchUtil
|
||||
# 创建EsSearchUtil实例
|
||||
search_util = EsSearchUtil(Config.ES_CONFIG)
|
||||
|
||||
# 调用重建mapping方法
|
||||
if search_util.rebuild_mapping():
|
||||
print("重建mapping操作完成成功")
|
||||
else:
|
||||
print("重建mapping操作失败")
|
||||
def rebuild_index(index_name):
|
||||
"""
|
||||
重建指定的索引
|
||||
|
||||
参数:
|
||||
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("所有索引重建操作完成")
|
@@ -6,7 +6,6 @@ import time
|
||||
|
||||
import requests
|
||||
|
||||
from Config.Config import ES_CONFIG
|
||||
from ElasticSearch.Utils.ElasticsearchConnectionPool import ElasticsearchConnectionPool
|
||||
from langchain_core.documents import Document
|
||||
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
||||
@@ -41,10 +40,13 @@ class EsSearchUtil:
|
||||
self.index_name = es_config['index_name']
|
||||
logger.info(f"EsSearchUtil初始化成功,索引名称: {self.index_name}")
|
||||
|
||||
def rebuild_mapping(self):
|
||||
def rebuild_mapping(self, index_name=None):
|
||||
"""
|
||||
重建Elasticsearch索引和mapping结构
|
||||
|
||||
参数:
|
||||
index_name: 可选,指定要重建的索引名称,默认使用初始化时的索引名称
|
||||
|
||||
返回:
|
||||
bool: 操作是否成功
|
||||
"""
|
||||
@@ -52,43 +54,66 @@ class EsSearchUtil:
|
||||
# 从连接池获取连接
|
||||
conn = self.es_pool.get_connection()
|
||||
|
||||
# 使用指定的索引名称或默认索引名称
|
||||
target_index = index_name if index_name else self.index_name
|
||||
logger.info(f"开始重建索引: {target_index}")
|
||||
|
||||
# 定义mapping结构
|
||||
mapping = {
|
||||
"mappings": {
|
||||
"properties": {
|
||||
"embedding": {
|
||||
"type": "dense_vector",
|
||||
"dims": Config.EMBED_DIM, # embedding维度为Config.EMBED_DIM
|
||||
"index": True,
|
||||
"similarity": "l2_norm" # 使用L2距离
|
||||
},
|
||||
"user_input": {"type": "text"},
|
||||
"tags": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"tags": {"type": "keyword"},
|
||||
"full_content": {"type": "text"}
|
||||
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 = {
|
||||
"mappings": {
|
||||
"properties": {
|
||||
"embedding": {
|
||||
"type": "dense_vector",
|
||||
"dims": Config.EMBED_DIM,
|
||||
"index": True,
|
||||
"similarity": "l2_norm"
|
||||
},
|
||||
"user_input": {"type": "text"},
|
||||
"tags": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"tags": {"type": "keyword"},
|
||||
"full_content": {"type": "text"}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# 检查索引是否存在,存在则删除
|
||||
if conn.indices.exists(index=self.index_name):
|
||||
conn.indices.delete(index=self.index_name)
|
||||
logger.info(f"删除已存在的索引 '{self.index_name}'")
|
||||
print(f"删除已存在的索引 '{self.index_name}'")
|
||||
if conn.indices.exists(index=target_index):
|
||||
conn.indices.delete(index=target_index)
|
||||
logger.info(f"删除已存在的索引 '{target_index}'")
|
||||
print(f"删除已存在的索引 '{target_index}'")
|
||||
|
||||
# 创建索引和mapping
|
||||
conn.indices.create(index=self.index_name, body=mapping)
|
||||
logger.info(f"索引 '{self.index_name}' 创建成功,mapping结构已设置")
|
||||
print(f"索引 '{self.index_name}' 创建成功,mapping结构已设置。")
|
||||
conn.indices.create(index=target_index, body=mapping)
|
||||
logger.info(f"索引 '{target_index}' 创建成功,mapping结构已设置")
|
||||
print(f"索引 '{target_index}' 创建成功,mapping结构已设置。")
|
||||
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"重建mapping失败: {str(e)}")
|
||||
print(f"重建mapping失败: {e}")
|
||||
logger.error(f"重建索引 '{target_index}' 失败: {str(e)}")
|
||||
print(f"重建索引 '{target_index}' 失败: {e}")
|
||||
|
||||
# 提供认证错误的具体提示
|
||||
if 'AuthenticationException' in str(e):
|
||||
print("认证失败提示: 请检查Config.py中的ES_CONFIG配置,确保用户名和密码正确。")
|
||||
logger.error("认证失败: 请检查Config.py中的ES_CONFIG配置,确保用户名和密码正确。")
|
||||
|
||||
return False
|
||||
finally:
|
||||
# 释放连接回连接池
|
||||
|
Binary file not shown.
Reference in New Issue
Block a user