|
|
|
@ -1,8 +1,9 @@
|
|
|
|
|
from Config.Config import ES_CONFIG
|
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
|
|
from elasticsearch import Elasticsearch
|
|
|
|
|
|
|
|
|
|
from Config.Config import ES_CONFIG
|
|
|
|
|
from T2_Txt2Vec import text_to_embedding
|
|
|
|
|
import datetime
|
|
|
|
|
import warnings
|
|
|
|
|
|
|
|
|
|
# 初始化ES连接
|
|
|
|
|
es = Elasticsearch(
|
|
|
|
@ -12,16 +13,15 @@ es = Elasticsearch(
|
|
|
|
|
ssl_show_warn=ES_CONFIG["ssl_show_warn"]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 修改create_vector_index和save_to_es函数中使用ES_CONFIG["default_index"]
|
|
|
|
|
def create_vector_index(index_name="knowledge_base"):
|
|
|
|
|
"""创建带有向量字段的索引"""
|
|
|
|
|
"""创建带有向量字段的索引(适配200维腾讯词向量)"""
|
|
|
|
|
mapping = {
|
|
|
|
|
"mappings": {
|
|
|
|
|
"properties": {
|
|
|
|
|
"text": {"type": "text", "analyzer": "ik_max_word"},
|
|
|
|
|
"vector": {
|
|
|
|
|
"type": "dense_vector",
|
|
|
|
|
"dims": 768, # 需与text2vec模型维度一致
|
|
|
|
|
"dims": 200, # 修改为腾讯词向量实际维度
|
|
|
|
|
"index": True,
|
|
|
|
|
"similarity": "cosine"
|
|
|
|
|
},
|
|
|
|
@ -34,7 +34,7 @@ def create_vector_index(index_name="knowledge_base"):
|
|
|
|
|
if es.indices.exists(index=index_name):
|
|
|
|
|
es.indices.delete(index=index_name)
|
|
|
|
|
es.indices.create(index=index_name, body=mapping)
|
|
|
|
|
print(f"索引 {index_name} 创建成功")
|
|
|
|
|
print(f"索引 {index_name} 创建成功(200维)")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"创建索引失败: {str(e)}")
|
|
|
|
|
raise
|
|
|
|
|