collections.service.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. InsertReq,
  10. LoadCollectionReq,
  11. ReleaseLoadCollectionReq,
  12. SearchReq,
  13. } from '@zilliz/milvus2-sdk-node/dist/milvus/types';
  14. import { throwErrorFromSDK } from '../utils/Error';
  15. import { findKeyValue } from '../utils/Helper';
  16. import { ROW_COUNT } from '../utils/Const';
  17. import {
  18. ShowCollectionsReq,
  19. ShowCollectionsType,
  20. } from '@zilliz/milvus2-sdk-node/dist/milvus/types/Collection';
  21. @Injectable()
  22. export class CollectionsService {
  23. constructor(private milvusService: MilvusService) {}
  24. get milvusClient() {
  25. return this.milvusService.milvusClientGetter;
  26. }
  27. async getCollectionNames(data?: ShowCollectionsReq) {
  28. const res = await this.milvusClient.showCollections(data);
  29. throwErrorFromSDK(res.status);
  30. return res;
  31. }
  32. async createCollection(data: CreateCollectionReq) {
  33. const res = await this.milvusClient.createCollection(data);
  34. throwErrorFromSDK(res);
  35. return res;
  36. }
  37. async describeCollection(data: DescribeCollectionReq) {
  38. const res = await this.milvusClient.describeCollection(data);
  39. throwErrorFromSDK(res.status);
  40. return res;
  41. }
  42. async dropCollection(data: DropCollectionReq) {
  43. const res = await this.milvusClient.dropCollection(data);
  44. throwErrorFromSDK(res);
  45. return res;
  46. }
  47. async loadCollection(data: LoadCollectionReq) {
  48. const res = await this.milvusClient.loadCollection(data);
  49. throwErrorFromSDK(res);
  50. return res;
  51. }
  52. async releaseCollection(data: ReleaseLoadCollectionReq) {
  53. const res = await this.milvusClient.releaseCollection(data);
  54. throwErrorFromSDK(res);
  55. return res;
  56. }
  57. async getCollectionStatistics(data: GetCollectionStatisticsReq) {
  58. const res = await this.milvusClient.getCollectionStatistics(data);
  59. throwErrorFromSDK(res.status);
  60. return res;
  61. }
  62. async insert(data: InsertReq) {
  63. const res = await this.milvusClient.insert(data);
  64. throwErrorFromSDK(res.status);
  65. return res;
  66. }
  67. async vectorSearch(data: SearchReq) {
  68. const res = await this.milvusClient.search(data);
  69. throwErrorFromSDK(res.status);
  70. return res;
  71. }
  72. /**
  73. * We do not throw error for this.
  74. * Because if collection dont have index, it will throw error.
  75. * We need wait for milvus error code.
  76. * @param data
  77. * @returns
  78. */
  79. async getIndexStatus(data: GetIndexStateReq) {
  80. const res = await this.milvusClient.getIndexState(data);
  81. return res;
  82. }
  83. /**
  84. * Get all collections meta data
  85. * @returns {id:string, collection_name:string, schema:Field[], autoID:boolean, rowCount: string}
  86. */
  87. async getAllCollections() {
  88. const data = [];
  89. const res = await this.getCollectionNames();
  90. const loadedCollections = await this.getCollectionNames({
  91. type: ShowCollectionsType.Loaded,
  92. });
  93. if (res.collection_names.length > 0) {
  94. for (const name of res.collection_names) {
  95. const collectionInfo = await this.describeCollection({
  96. collection_name: name,
  97. });
  98. const collectionStatistics = await this.getCollectionStatistics({
  99. collection_name: name,
  100. });
  101. const autoID = collectionInfo.schema.fields.find(
  102. (v) => v.is_primary_key === true,
  103. )?.autoID;
  104. data.push({
  105. collection_name: name,
  106. schema: collectionInfo.schema,
  107. description: collectionInfo.schema.description,
  108. autoID,
  109. rowCount: findKeyValue(collectionStatistics.stats, ROW_COUNT),
  110. id: collectionInfo.collectionID,
  111. isLoaded: loadedCollections.collection_names.includes(name),
  112. createdTime: collectionInfo.created_utc_timestamp,
  113. });
  114. }
  115. }
  116. return data;
  117. }
  118. async getLoadedColletions() {
  119. const data = [];
  120. const res = await this.getCollectionNames({
  121. type: ShowCollectionsType.Loaded,
  122. });
  123. if (res.collection_names.length > 0) {
  124. for (const [index, value] of res.collection_names.entries()) {
  125. const collectionStatistics = await this.getCollectionStatistics({
  126. collection_name: value,
  127. });
  128. data.push({
  129. id: res.collection_ids[index],
  130. collection_name: value,
  131. rowCount: findKeyValue(collectionStatistics.stats, ROW_COUNT),
  132. });
  133. }
  134. }
  135. return data;
  136. }
  137. /**
  138. * Get collections statistics data
  139. * @returns {collectionCount:number, totalData:number}
  140. */
  141. async getStatistics() {
  142. const data = {
  143. collectionCount: 0,
  144. totalData: 0,
  145. };
  146. const res = await this.getCollectionNames();
  147. data.collectionCount = res.collection_names.length;
  148. if (res.collection_names.length > 0) {
  149. for (const name of res.collection_names) {
  150. const collectionStatistics = await this.getCollectionStatistics({
  151. collection_name: name,
  152. });
  153. const rowCount = findKeyValue(collectionStatistics.stats, ROW_COUNT);
  154. data.totalData += isNaN(Number(rowCount)) ? 0 : Number(rowCount);
  155. }
  156. }
  157. return data;
  158. }
  159. /**
  160. * Get all collection index status
  161. * @returns {collection_name:string, index_state: IndexState}[]
  162. */
  163. async getCollectionsIndexStatus() {
  164. const data = [];
  165. const res = await this.getCollectionNames();
  166. if (res.collection_names.length > 0) {
  167. for (const name of res.collection_names) {
  168. const indexRes = await this.getIndexStatus({ collection_name: name });
  169. data.push({
  170. collection_name: name,
  171. index_state: indexRes.state,
  172. });
  173. }
  174. }
  175. return data;
  176. }
  177. }