collections.service.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import { MilvusService } from "../milvus/milvus.service";
  2. import {
  3. CreateCollectionReq,
  4. DescribeCollectionReq,
  5. DropCollectionReq,
  6. GetCollectionStatisticsReq,
  7. GetIndexStateReq,
  8. InsertReq,
  9. LoadCollectionReq,
  10. ReleaseLoadCollectionReq,
  11. SearchReq,
  12. } from "@zilliz/milvus2-sdk-node/dist/milvus/types";
  13. import { throwErrorFromSDK } from "../utils/Error";
  14. import { findKeyValue } from "../utils/Helper";
  15. import { ROW_COUNT } from "../utils/Const";
  16. import {
  17. AlterAliasReq,
  18. CreateAliasReq,
  19. DropAliasReq,
  20. ShowCollectionsReq,
  21. ShowCollectionsType,
  22. } from "@zilliz/milvus2-sdk-node/dist/milvus/types/Collection";
  23. import { QueryDto } from "./dto";
  24. import { DeleteEntitiesReq } from "@zilliz/milvus2-sdk-node/dist/milvus/types/Data";
  25. export class CollectionsService {
  26. constructor(private milvusService: MilvusService) {}
  27. get collectionManager() {
  28. return this.milvusService.collectionManager;
  29. }
  30. get dataManager() {
  31. return this.milvusService.dataManager;
  32. }
  33. get indexManager() {
  34. return this.milvusService.indexManager;
  35. }
  36. async getCollections(data?: ShowCollectionsReq) {
  37. const res = await this.collectionManager.showCollections(data);
  38. throwErrorFromSDK(res.status);
  39. return res;
  40. }
  41. async createCollection(data: CreateCollectionReq) {
  42. const res = await this.collectionManager.createCollection(data);
  43. throwErrorFromSDK(res);
  44. return res;
  45. }
  46. async describeCollection(data: DescribeCollectionReq) {
  47. const res = await this.collectionManager.describeCollection(data);
  48. throwErrorFromSDK(res.status);
  49. return res;
  50. }
  51. async dropCollection(data: DropCollectionReq) {
  52. const res = await this.collectionManager.dropCollection(data);
  53. throwErrorFromSDK(res);
  54. return res;
  55. }
  56. async loadCollection(data: LoadCollectionReq) {
  57. const res = await this.collectionManager.loadCollection(data);
  58. throwErrorFromSDK(res);
  59. return res;
  60. }
  61. async releaseCollection(data: ReleaseLoadCollectionReq) {
  62. const res = await this.collectionManager.releaseCollection(data);
  63. throwErrorFromSDK(res);
  64. return res;
  65. }
  66. async getCollectionStatistics(data: GetCollectionStatisticsReq) {
  67. const res = await this.collectionManager.getCollectionStatistics(data);
  68. throwErrorFromSDK(res.status);
  69. return res;
  70. }
  71. async insert(data: InsertReq) {
  72. const res = await this.dataManager.insert(data);
  73. throwErrorFromSDK(res.status);
  74. return res;
  75. }
  76. async deleteEntities(data: DeleteEntitiesReq) {
  77. const res = await this.dataManager.deleteEntities(data);
  78. return res;
  79. }
  80. async vectorSearch(data: SearchReq) {
  81. const res = await this.dataManager.search(data);
  82. throwErrorFromSDK(res.status);
  83. return res;
  84. }
  85. async createAlias(data: CreateAliasReq) {
  86. const res = await this.collectionManager.createAlias(data);
  87. throwErrorFromSDK(res);
  88. return res;
  89. }
  90. async alterAlias(data: AlterAliasReq) {
  91. const res = await this.collectionManager.alterAlias(data);
  92. throwErrorFromSDK(res);
  93. return res;
  94. }
  95. async dropAlias(data: DropAliasReq) {
  96. const res = await this.collectionManager.dropAlias(data);
  97. throwErrorFromSDK(res);
  98. return res;
  99. }
  100. async query(
  101. data: {
  102. collection_name: string;
  103. } & QueryDto
  104. ) {
  105. const res = await this.dataManager.query(data);
  106. throwErrorFromSDK(res.status);
  107. return res;
  108. }
  109. /**
  110. * We do not throw error for this.
  111. * Because if collection dont have index, it will throw error.
  112. * We need wait for milvus error code.
  113. * @param data
  114. * @returns
  115. */
  116. async getIndexStatus(data: GetIndexStateReq) {
  117. const res = await this.indexManager.getIndexState(data);
  118. return res;
  119. }
  120. /**
  121. * Get all collections meta data
  122. * @returns {id:string, collection_name:string, schema:Field[], autoID:boolean, rowCount: string}
  123. */
  124. async getAllCollections() {
  125. const data = [];
  126. const res = await this.getCollections();
  127. const loadedCollections = await this.getCollections({
  128. type: ShowCollectionsType.Loaded,
  129. });
  130. if (res.data.length > 0) {
  131. for (const item of res.data) {
  132. const { name } = item;
  133. const collectionInfo = await this.describeCollection({
  134. collection_name: name,
  135. });
  136. const collectionStatistics = await this.getCollectionStatistics({
  137. collection_name: name,
  138. });
  139. const indexRes = await this.getIndexStatus({
  140. collection_name: item.name,
  141. });
  142. const autoID = collectionInfo.schema.fields.find(
  143. (v) => v.is_primary_key === true
  144. )?.autoID;
  145. const loadCollection = loadedCollections.data.find(
  146. (v) => v.name === name
  147. );
  148. const loadedPercentage = !loadCollection
  149. ? "-1"
  150. : loadCollection.loadedPercentage;
  151. data.push({
  152. collection_name: name,
  153. schema: collectionInfo.schema,
  154. description: collectionInfo.schema.description,
  155. autoID,
  156. rowCount: findKeyValue(collectionStatistics.stats, ROW_COUNT),
  157. id: collectionInfo.collectionID,
  158. loadedPercentage,
  159. createdTime: parseInt(collectionInfo.created_utc_timestamp, 10),
  160. index_status: indexRes.state,
  161. });
  162. }
  163. }
  164. // add default sort - Descending order
  165. data.sort((a, b) => b.createdTime - a.createdTime);
  166. return data;
  167. }
  168. async getLoadedColletions() {
  169. const data = [];
  170. const res = await this.getCollections({
  171. type: ShowCollectionsType.Loaded,
  172. });
  173. if (res.data.length > 0) {
  174. for (const item of res.data) {
  175. const { id, name } = item;
  176. const collectionStatistics = await this.getCollectionStatistics({
  177. collection_name: name,
  178. });
  179. data.push({
  180. id,
  181. collection_name: name,
  182. rowCount: findKeyValue(collectionStatistics.stats, ROW_COUNT),
  183. });
  184. }
  185. }
  186. return data;
  187. }
  188. /**
  189. * Get collections statistics data
  190. * @returns {collectionCount:number, totalData:number}
  191. */
  192. async getStatistics() {
  193. const data = {
  194. collectionCount: 0,
  195. totalData: 0,
  196. };
  197. const res = await this.getCollections();
  198. data.collectionCount = res.data.length;
  199. if (res.data.length > 0) {
  200. for (const item of res.data) {
  201. const collectionStatistics = await this.getCollectionStatistics({
  202. collection_name: item.name,
  203. });
  204. const rowCount = findKeyValue(collectionStatistics.stats, ROW_COUNT);
  205. data.totalData += isNaN(Number(rowCount)) ? 0 : Number(rowCount);
  206. }
  207. }
  208. return data;
  209. }
  210. /**
  211. * Get all collection index status
  212. * @returns {collection_name:string, index_status: IndexState}[]
  213. */
  214. async getCollectionsIndexStatus() {
  215. const data = [];
  216. const res = await this.getCollections();
  217. if (res.data.length > 0) {
  218. for (const item of res.data) {
  219. const indexRes = await this.getIndexStatus({
  220. collection_name: item.name,
  221. });
  222. data.push({
  223. collection_name: item.name,
  224. index_status: indexRes.state,
  225. });
  226. }
  227. }
  228. return data;
  229. }
  230. }