'commit'
This commit is contained in:
54
dsSchoolBuddy/ElasticSearch/Utils/EsSearchUtil.py
Normal file
54
dsSchoolBuddy/ElasticSearch/Utils/EsSearchUtil.py
Normal file
@@ -0,0 +1,54 @@
|
||||
import logging
|
||||
|
||||
from ElasticSearch.Utils.ElasticsearchConnectionPool import ElasticsearchConnectionPool
|
||||
|
||||
# 初始化日志
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.setLevel(logging.INFO)
|
||||
|
||||
class EsSearchUtil:
|
||||
def __init__(self, es_config):
|
||||
"""
|
||||
初始化Elasticsearch搜索工具
|
||||
:param es_config: Elasticsearch配置字典,包含hosts, username, password, index_name等
|
||||
"""
|
||||
self.es_config = es_config
|
||||
|
||||
# 初始化连接池
|
||||
self.es_pool = ElasticsearchConnectionPool(
|
||||
hosts=es_config['hosts'],
|
||||
basic_auth=es_config['basic_auth'],
|
||||
verify_certs=es_config.get('verify_certs', False),
|
||||
max_connections=50
|
||||
)
|
||||
|
||||
self.index_name = es_config['index_name']
|
||||
logger.info(f"EsSearchUtil初始化成功,索引名称: {self.index_name}")
|
||||
|
||||
def text_search(self, query, size=10):
|
||||
# 从连接池获取连接
|
||||
conn = self.es_pool.get_connection()
|
||||
try:
|
||||
# 使用连接执行搜索
|
||||
result = conn.search(
|
||||
index=self.es_config['index_name'],
|
||||
query={"match": {"user_input": query}},
|
||||
size=size
|
||||
)
|
||||
return result
|
||||
except Exception as e:
|
||||
logger.error(f"文本搜索失败: {str(e)}")
|
||||
raise
|
||||
finally:
|
||||
# 释放连接回连接池
|
||||
self.es_pool.release_connection(conn)
|
||||
|
||||
def hybrid_search(self, query, size=10):
|
||||
# 注意:混合搜索方法需要更新以使用火山引擎API计算向量
|
||||
# 此处保留方法框架,具体实现需要根据火山引擎API进行调整
|
||||
pass
|
||||
|
||||
# 以下功能已删除,建议通过火山引擎API实现向量计算:
|
||||
# 1. 本地模型加载和相关配置 (KeyedVectors)
|
||||
# 2. text_to_embedding方法 (基于本地模型的向量计算)
|
||||
# 3. vector_search方法 (使用本地模型向量的搜索)
|
0
dsSchoolBuddy/ElasticSearch/Utils/__init__.py
Normal file
0
dsSchoolBuddy/ElasticSearch/Utils/__init__.py
Normal file
@@ -15,7 +15,7 @@ from starlette.responses import StreamingResponse
|
||||
from starlette.staticfiles import StaticFiles
|
||||
|
||||
from Config import Config
|
||||
from Util.EsSearchUtil import *
|
||||
from ElasticSearch.Utils.EsSearchUtil import *
|
||||
from Util.MySQLUtil import init_mysql_pool
|
||||
|
||||
# 初始化日志
|
||||
|
Reference in New Issue
Block a user