背景:因公司业务需求需要利用Langchain中对ES数据库的相关操作将某些文件通过embedding录入向量数据库;
代码如下:
def read_excel_to_file(filepath):
'''
将处理好的.xls文件导入到es中,.xls文件中每一行只保留问题和答案,其他的不要
'''
wb = xlrd.open_workbook(filename=filepath)
sheet1 = wb.sheet_by_index(0)
#逐行读取excel文件
for i in range(sheet1.nrows):
print("".join(sheet1.row_values(i)).strip())
#对每行读出的列表进行组合,去除首尾空格
texts = "".join(sheet1.row_values(i)).strip()
#设置文本切分量级
text_splitter = TokenTextSplitter(chunk_size=1000, chunk_overlap=0)
texts = text_splitter.create_documents([texts])
print(texts)
# 通过openai进行embeddings,并存入es中
index_name = "**********"
embeddings = llm_utils.get_custom_embeddings()
docsearch = ElasticVectorSearch.from_documents(texts, embeddings,
elasticsearch_url="localhost:80",
index_name=index_name)
在录入过程中程序报错:
elasticsearch.exceptions.RequestError: RequestError(400,‘validation_exception’, ‘Validation Failed: 1: this action would add [2] total shards, but this cluster currently has [999]/[1000] maximum shards open;’)
通过查询相关资料以及询问GPT发现问题根源所在
链接:https://discuss.openedx.org/t/elasticsearch-maximum-shards-open/7319
导致报错的原因是由于ES向量数据的分片数已被占满,从而导致无法录入新的索引数据,解决办法就是清理无关的索引,将分片空余出来。