index.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. """
  2. Infinity向量数据库索引管理器
  3. """
  4. from typing import Dict, Any, Optional
  5. from services.utils.infinity.base import InfinityConnection
  6. class InfinityIndexManager:
  7. """
  8. Infinity向量数据库索引管理器
  9. 负责索引(collection)的创建、删除和检查
  10. """
  11. def __init__(self, infinity_connection: Optional[InfinityConnection] = None):
  12. """
  13. 初始化索引管理器
  14. Args:
  15. infinity_connection: Infinity连接实例,可选
  16. """
  17. self.infinity_conn = infinity_connection or InfinityConnection()
  18. def create_index(self, index_name: str, mappings: Dict[str, Any] = None) -> bool:
  19. """
  20. 创建索引(Infinity中的collection)
  21. Args:
  22. index_name: 索引名称
  23. mappings: 自定义映射,会与默认映射合并
  24. Returns:
  25. bool: 创建是否成功
  26. """
  27. try:
  28. # 创建collection的API路径
  29. path = f"/api/collections/{index_name}"
  30. # 默认映射配置
  31. default_mappings = {
  32. "fields": [
  33. {"name": "file_name", "type": "string"},
  34. {"name": "file_page_count", "type": "integer"},
  35. {"name": "page_number", "type": "integer"},
  36. {"name": "text", "type": "string"},
  37. {"name": "image_path", "type": "string"},
  38. {"name": "sparse_vector", "type": "array<array<{string, float}>>"},
  39. {"name": "dense_vector_1024", "type": "array<float>"},
  40. {"name": "dataset_id", "type": "string"},
  41. {"name": "document_id", "type": "string"}
  42. ],
  43. "indexes": [
  44. {
  45. "name": f"{index_name}_vector_index",
  46. "field": "dense_vector_1024",
  47. "type": "vector",
  48. "params": {
  49. "dimension": 1024,
  50. "metric": "cosine"
  51. }
  52. }
  53. ]
  54. }
  55. # 合并用户映射和默认映射
  56. final_mappings = {**default_mappings, **(mappings or {})}
  57. response = self.infinity_conn._make_request("PUT", path, final_mappings)
  58. return "error" not in response
  59. except Exception as e:
  60. print(f"创建Infinity索引失败: {e}")
  61. return False
  62. def delete_index(self, index_name: str) -> bool:
  63. """
  64. 删除索引(collection)
  65. Args:
  66. index_name: 索引名称
  67. Returns:
  68. bool: 删除是否成功
  69. """
  70. try:
  71. path = f"/api/collections/{index_name}"
  72. response = self.infinity_conn._make_request("DELETE", path)
  73. return "error" not in response
  74. except Exception as e:
  75. print(f"删除Infinity索引失败: {e}")
  76. return False
  77. def index_exists(self, index_name: str) -> bool:
  78. """
  79. 检查索引是否存在
  80. Args:
  81. index_name: 索引名称
  82. Returns:
  83. bool: 索引是否存在
  84. """
  85. try:
  86. path = f"/api/collections/{index_name}"
  87. response = self.infinity_conn._make_request("GET", path)
  88. return "error" not in response
  89. except Exception as e:
  90. print(f"检查Infinity索引存在失败: {e}")
  91. return False
  92. def get_index_info(self, index_name: str) -> Dict[str, Any]:
  93. """
  94. 获取索引信息
  95. Args:
  96. index_name: 索引名称
  97. Returns:
  98. Dict[str, Any]: 索引信息
  99. """
  100. try:
  101. path = f"/api/collections/{index_name}"
  102. response = self.infinity_conn._make_request("GET", path)
  103. return response
  104. except Exception as e:
  105. print(f"获取Infinity索引信息失败: {e}")
  106. return {}
  107. def list_indexes(self) -> list:
  108. """
  109. 获取所有索引列表
  110. Returns:
  111. list: 索引列表
  112. """
  113. try:
  114. path = "/api/collections"
  115. response = self.infinity_conn._make_request("GET", path)
  116. return response.get("collections", [])
  117. except Exception as e:
  118. print(f"获取Infinity索引列表失败: {e}")
  119. return []