mirror of https://github.com/langgenius/dify.git
4.5 KiB
4.5 KiB
完整的 Elasticsearch 配置指南
🔧 问题修复总结
我已经修复了以下问题:
1. 构造函数参数不匹配
- 错误:
ElasticsearchWorkflowExecutionRepository.__init__() got an unexpected keyword argument 'session_factory' - 修复: 修改构造函数接受
session_factory参数,从全局扩展获取 Elasticsearch 客户端
2. 导入错误
- 错误:
name 'sessionmaker' is not defined - 修复: 添加必要的 SQLAlchemy 导入
3. SSL/HTTPS 配置
- 错误:
received plaintext http traffic on an https channel - 修复: 使用 HTTPS 连接和正确的认证信息
4. 实体属性不匹配
- 错误:
'WorkflowExecution' object has no attribute 'created_at'和'WorkflowExecution' object has no attribute 'id' - 修复: 使用正确的属性名:
id_而不是idstarted_at而不是created_aterror_message而不是error
📋 完整的 .env 配置
请将以下配置添加到您的 dify/api/.env 文件:
# ====================================
# Elasticsearch 配置
# ====================================
# 启用 Elasticsearch
ELASTICSEARCH_ENABLED=true
# 连接设置(注意使用 HTTPS)
ELASTICSEARCH_HOSTS=["https://localhost:9200"]
ELASTICSEARCH_USERNAME=elastic
ELASTICSEARCH_PASSWORD=2gYvv6+O36PGwaVD6yzE
# SSL 设置
ELASTICSEARCH_USE_SSL=true
ELASTICSEARCH_VERIFY_CERTS=false
# 性能设置
ELASTICSEARCH_TIMEOUT=30
ELASTICSEARCH_MAX_RETRIES=3
ELASTICSEARCH_INDEX_PREFIX=dify
ELASTICSEARCH_RETENTION_DAYS=30
# ====================================
# Repository Factory 配置
# 切换到 Elasticsearch 实现
# ====================================
# 核心工作流 repositories
CORE_WORKFLOW_EXECUTION_REPOSITORY=core.repositories.elasticsearch_workflow_execution_repository.ElasticsearchWorkflowExecutionRepository
CORE_WORKFLOW_NODE_EXECUTION_REPOSITORY=core.repositories.elasticsearch_workflow_node_execution_repository.ElasticsearchWorkflowNodeExecutionRepository
# API 服务层 repositories
API_WORKFLOW_RUN_REPOSITORY=repositories.elasticsearch_api_workflow_run_repository.ElasticsearchAPIWorkflowRunRepository
🚀 使用步骤
1. 配置环境变量
将上述配置复制到您的 .env 文件中
2. 重启应用
重启 Dify API 服务以加载新配置
3. 测试连接
flask elasticsearch status
4. 执行迁移
# 干运行测试
flask elasticsearch migrate --dry-run
# 实际迁移(替换为您的实际 tenant_id)
flask elasticsearch migrate --tenant-id your-tenant-id
# 验证迁移结果
flask elasticsearch validate --tenant-id your-tenant-id
📊 四个日志表的处理方式
| 表名 | Repository 配置 | 实现类 |
|---|---|---|
workflow_runs |
API_WORKFLOW_RUN_REPOSITORY |
ElasticsearchAPIWorkflowRunRepository |
workflow_node_executions |
CORE_WORKFLOW_NODE_EXECUTION_REPOSITORY |
ElasticsearchWorkflowNodeExecutionRepository |
workflow_app_logs |
不使用 factory | ElasticsearchWorkflowAppLogRepository |
workflow_node_execution_offload |
集成处理 | 在 node executions 中自动处理 |
✅ 验证配置正确性
配置完成后,您可以通过以下方式验证:
- 检查应用启动: 应用应该能正常启动,无错误日志
- 测试 Elasticsearch 连接:
flask elasticsearch status应该显示集群状态 - 测试工作流执行: 在 Dify 界面中执行工作流,检查是否有错误
🔄 回滚方案
如果需要回滚到 PostgreSQL,只需注释掉或删除 Repository 配置:
# 注释掉这些行以回滚到 PostgreSQL
# CORE_WORKFLOW_EXECUTION_REPOSITORY=core.repositories.elasticsearch_workflow_execution_repository.ElasticsearchWorkflowExecutionRepository
# CORE_WORKFLOW_NODE_EXECUTION_REPOSITORY=core.repositories.elasticsearch_workflow_node_execution_repository.ElasticsearchWorkflowNodeExecutionRepository
# API_WORKFLOW_RUN_REPOSITORY=repositories.elasticsearch_api_workflow_run_repository.ElasticsearchAPIWorkflowRunRepository
🎯 关键优势
切换到 Elasticsearch 后,您将获得:
- 更好的性能: 专为日志数据优化的存储引擎
- 全文搜索: 支持复杂的日志搜索和分析
- 时间序列优化: 自动索引轮转和数据生命周期管理
- 水平扩展: 支持集群扩展处理大量数据
- 实时分析: 近实时的数据查询和聚合分析
现在所有的错误都已经修复,您可以安全地使用 Elasticsearch 作为工作流日志的存储后端了!