'commit'
This commit is contained in:
@@ -1,47 +1,14 @@
|
||||
import warnings
|
||||
|
||||
from elasticsearch import Elasticsearch
|
||||
|
||||
from Config import Config
|
||||
from ElasticSearch.Utils.EsSearchUtil import EsSearchUtil, disableWarning
|
||||
|
||||
# 抑制HTTPS相关警告
|
||||
warnings.filterwarnings('ignore', message='Connecting to .* using TLS with verify_certs=False is insecure')
|
||||
warnings.filterwarnings('ignore', message='Unverified HTTPS request is being made to host')
|
||||
# 禁用警告
|
||||
disableWarning()
|
||||
|
||||
# 初始化ES连接
|
||||
es = Elasticsearch(
|
||||
hosts=Config.ES_CONFIG['hosts'],
|
||||
basic_auth=Config.ES_CONFIG['basic_auth'],
|
||||
verify_certs=False
|
||||
)
|
||||
# 创建EsSearchUtil实例
|
||||
search_util = EsSearchUtil(Config.ES_CONFIG)
|
||||
|
||||
# 定义mapping结构
|
||||
mapping = {
|
||||
"mappings": {
|
||||
"properties": {
|
||||
"embedding": {
|
||||
"type": "dense_vector",
|
||||
"dims": 1024, # embedding维度为1024
|
||||
"index": True,
|
||||
"similarity": "l2_norm" # 使用L2距离
|
||||
},
|
||||
"user_input": {"type": "text"},
|
||||
"tags": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"tags": {"type": "keyword"},
|
||||
"full_content": {"type": "text"}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# 创建索引
|
||||
index_name = Config.ES_CONFIG['index_name']
|
||||
if es.indices.exists(index=index_name):
|
||||
es.indices.delete(index=index_name)
|
||||
print(f"删除已存在的索引 '{index_name}'")
|
||||
|
||||
es.indices.create(index=index_name, body=mapping)
|
||||
print(f"索引 '{index_name}' 创建成功,mapping结构已设置。")
|
||||
# 调用重建mapping方法
|
||||
if search_util.rebuild_mapping():
|
||||
print("重建mapping操作完成成功")
|
||||
else:
|
||||
print("重建mapping操作失败")
|
@@ -11,9 +11,15 @@ from langchain_openai import OpenAIEmbeddings
|
||||
from pydantic import SecretStr
|
||||
|
||||
from Config import Config
|
||||
# 抑制HTTPS相关警告
|
||||
warnings.filterwarnings('ignore', message='Connecting to .* using TLS with verify_certs=False is insecure')
|
||||
warnings.filterwarnings('ignore', message='Unverified HTTPS request is being made to host')
|
||||
|
||||
# 禁用HTTPS相关警告
|
||||
def disableWarning():
|
||||
# 抑制HTTPS相关警告
|
||||
warnings.filterwarnings('ignore', message='Connecting to .* using TLS with verify_certs=False is insecure')
|
||||
warnings.filterwarnings('ignore', message='Unverified HTTPS request is being made to host')
|
||||
|
||||
# 初始化配置
|
||||
disableWarning()
|
||||
|
||||
# 初始化日志
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -38,6 +44,59 @@ class EsSearchUtil:
|
||||
self.index_name = es_config['index_name']
|
||||
logger.info(f"EsSearchUtil初始化成功,索引名称: {self.index_name}")
|
||||
|
||||
def rebuild_mapping(self):
|
||||
"""
|
||||
重建Elasticsearch索引和mapping结构
|
||||
|
||||
返回:
|
||||
bool: 操作是否成功
|
||||
"""
|
||||
try:
|
||||
# 从连接池获取连接
|
||||
conn = self.es_pool.get_connection()
|
||||
|
||||
# 定义mapping结构
|
||||
mapping = {
|
||||
"mappings": {
|
||||
"properties": {
|
||||
"embedding": {
|
||||
"type": "dense_vector",
|
||||
"dims": 1024, # embedding维度为1024
|
||||
"index": True,
|
||||
"similarity": "l2_norm" # 使用L2距离
|
||||
},
|
||||
"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}'")
|
||||
|
||||
# 创建索引和mapping
|
||||
conn.indices.create(index=self.index_name, body=mapping)
|
||||
logger.info(f"索引 '{self.index_name}' 创建成功,mapping结构已设置")
|
||||
print(f"索引 '{self.index_name}' 创建成功,mapping结构已设置。")
|
||||
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"重建mapping失败: {str(e)}")
|
||||
print(f"重建mapping失败: {e}")
|
||||
return False
|
||||
finally:
|
||||
# 释放连接回连接池
|
||||
self.es_pool.release_connection(conn)
|
||||
|
||||
def text_search(self, query, size=10):
|
||||
# 从连接池获取连接
|
||||
conn = self.es_pool.get_connection()
|
||||
|
Binary file not shown.
Reference in New Issue
Block a user