collections.service.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { Injectable } from '@nestjs/common';
  2. import { MilvusService } from '../milvus/milvus.service';
  3. import {
  4. CreateCollectionReq,
  5. DescribeCollectionReq,
  6. DropCollectionReq,
  7. GetCollectionStatisticsReq,
  8. GetIndexStateReq,
  9. LoadCollectionReq,
  10. ReleaseLoadCollectionReq,
  11. } from '@zilliz/milvus-sdk-node-dev/dist/milvus/types';
  12. import { throwErrorFromSDK } from '../utils/Error';
  13. import { findKeyValue } from '../utils/Helper';
  14. import { ROW_COUNT } from '../utils/Const';
  15. @Injectable()
  16. export class CollectionsService {
  17. constructor(private milvusService: MilvusService) {}
  18. get milvusClient() {
  19. return this.milvusService.milvusClientGetter;
  20. }
  21. async getCollectionNames() {
  22. const res = await this.milvusClient.showCollections();
  23. throwErrorFromSDK(res.status);
  24. return res;
  25. }
  26. async createCollection(data: CreateCollectionReq) {
  27. const res = await this.milvusClient.createCollection(data);
  28. throwErrorFromSDK(res);
  29. return res;
  30. }
  31. async describeCollection(data: DescribeCollectionReq) {
  32. const res = await this.milvusClient.describeCollection(data);
  33. throwErrorFromSDK(res.status);
  34. return res;
  35. }
  36. async dropCollection(data: DropCollectionReq) {
  37. const res = await this.milvusClient.dropCollection(data);
  38. throwErrorFromSDK(res);
  39. return res;
  40. }
  41. async loadCollection(data: LoadCollectionReq) {
  42. const res = await this.milvusClient.loadCollection(data);
  43. throwErrorFromSDK(res);
  44. return res;
  45. }
  46. async releaseCollection(data: ReleaseLoadCollectionReq) {
  47. const res = await this.milvusClient.releaseCollection(data);
  48. throwErrorFromSDK(res);
  49. return res;
  50. }
  51. async getCollectionStatistics(data: GetCollectionStatisticsReq) {
  52. const res = await this.milvusClient.getCollectionStatistics(data);
  53. throwErrorFromSDK(res.status);
  54. return res;
  55. }
  56. /**
  57. * We do not throw error for this.
  58. * Because if collection dont have index, it will throw error.
  59. * We need wait for milvus error code.
  60. * @param data
  61. * @returns
  62. */
  63. async getIndexStatus(data: GetIndexStateReq) {
  64. const res = await this.milvusClient.getIndexState(data);
  65. return res;
  66. }
  67. async showCollections() {
  68. const data = [];
  69. const res = await this.getCollectionNames();
  70. if (res.collection_names.length > 0) {
  71. for (const name of res.collection_names) {
  72. const collectionInfo = await this.describeCollection({
  73. collection_name: name,
  74. });
  75. const collectionStatistics = await this.getCollectionStatistics({
  76. collection_name: name,
  77. });
  78. data.push({
  79. collection_name: name,
  80. schema: collectionInfo.schema,
  81. description: collectionInfo.schema.description,
  82. autoID: collectionInfo.schema.autoID,
  83. rowCount: findKeyValue(collectionStatistics.stats, ROW_COUNT),
  84. // id: collectionInfo.collectionId
  85. });
  86. }
  87. }
  88. return data;
  89. }
  90. async getCollectionsIndexStatus() {
  91. const data = [];
  92. const res = await this.getCollectionNames();
  93. if (res.collection_names.length > 0) {
  94. for (const name of res.collection_names) {
  95. const indexRes = await this.getIndexStatus({ collection_name: name });
  96. data.push({
  97. collectionName: name,
  98. indexState: indexRes.state,
  99. });
  100. }
  101. }
  102. return data;
  103. }
  104. }