collections.service.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. RenameCollectionReq,
  13. AlterAliasReq,
  14. CreateAliasReq,
  15. DropAliasReq,
  16. ShowCollectionsReq,
  17. ShowCollectionsType,
  18. DeleteEntitiesReq,
  19. } from '@zilliz/milvus2-sdk-node';
  20. import { throwErrorFromSDK } from '../utils/Error';
  21. import { findKeyValue, genRows } from '../utils/Helper';
  22. import { ROW_COUNT } from '../utils/Const';
  23. import { QueryDto, ImportSampleDto, GetReplicasDto } from './dto';
  24. export class CollectionsService {
  25. constructor(private milvusService: MilvusService) {}
  26. async getCollections(data?: ShowCollectionsReq) {
  27. const res = await this.milvusService.client.showCollections(data);
  28. throwErrorFromSDK(res.status);
  29. return res;
  30. }
  31. async createCollection(data: CreateCollectionReq) {
  32. const res = await this.milvusService.client.createCollection(data);
  33. throwErrorFromSDK(res);
  34. return res;
  35. }
  36. async describeCollection(data: DescribeCollectionReq) {
  37. const res = await this.milvusService.client.describeCollection(data);
  38. throwErrorFromSDK(res.status);
  39. return res;
  40. }
  41. async renameCollection(data: RenameCollectionReq) {
  42. const res = await this.milvusService.client.renameCollection(data);
  43. throwErrorFromSDK(res);
  44. return res;
  45. }
  46. async dropCollection(data: DropCollectionReq) {
  47. const res = await this.milvusService.client.dropCollection(data);
  48. throwErrorFromSDK(res);
  49. return res;
  50. }
  51. async loadCollection(data: LoadCollectionReq) {
  52. const res = await this.milvusService.client.loadCollection(data);
  53. throwErrorFromSDK(res);
  54. return res;
  55. }
  56. async releaseCollection(data: ReleaseLoadCollectionReq) {
  57. const res = await this.milvusService.client.releaseCollection(data);
  58. throwErrorFromSDK(res);
  59. return res;
  60. }
  61. async getCollectionStatistics(data: GetCollectionStatisticsReq) {
  62. const res = await this.milvusService.client.getCollectionStatistics(data);
  63. throwErrorFromSDK(res.status);
  64. return res;
  65. }
  66. async insert(data: InsertReq) {
  67. const res = await this.milvusService.client.insert(data);
  68. throwErrorFromSDK(res.status);
  69. return res;
  70. }
  71. async deleteEntities(data: DeleteEntitiesReq) {
  72. const res = await this.milvusService.client.deleteEntities(data);
  73. throwErrorFromSDK(res.status);
  74. return res;
  75. }
  76. async vectorSearch(data: SearchReq) {
  77. const res = await this.milvusService.client.search(data);
  78. throwErrorFromSDK(res.status);
  79. return res;
  80. }
  81. async createAlias(data: CreateAliasReq) {
  82. const res = await this.milvusService.client.createAlias(data);
  83. throwErrorFromSDK(res);
  84. return res;
  85. }
  86. async alterAlias(data: AlterAliasReq) {
  87. const res = await this.milvusService.client.alterAlias(data);
  88. throwErrorFromSDK(res);
  89. return res;
  90. }
  91. async dropAlias(data: DropAliasReq) {
  92. const res = await this.milvusService.client.dropAlias(data);
  93. throwErrorFromSDK(res);
  94. return res;
  95. }
  96. async getReplicas(data: GetReplicasDto) {
  97. const res = await this.milvusService.client.getReplicas(data);
  98. return res;
  99. }
  100. async query(
  101. data: {
  102. collection_name: string;
  103. } & QueryDto
  104. ) {
  105. const res = await this.milvusService.client.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.milvusService.client.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, consistency_level:string}
  123. */
  124. async getAllCollections() {
  125. const data: any = [];
  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. let replicas;
  152. try {
  153. replicas = loadCollection
  154. ? await this.getReplicas({
  155. collectionID: collectionInfo.collectionID,
  156. })
  157. : replicas;
  158. } catch (e) {
  159. console.log('ignore getReplica');
  160. }
  161. data.push({
  162. aliases: collectionInfo.aliases,
  163. collection_name: name,
  164. schema: collectionInfo.schema,
  165. description: collectionInfo.schema.description,
  166. autoID,
  167. rowCount: findKeyValue(collectionStatistics.stats, ROW_COUNT),
  168. id: collectionInfo.collectionID,
  169. loadedPercentage,
  170. createdTime: parseInt(collectionInfo.created_utc_timestamp, 10),
  171. index_status: indexRes.state,
  172. consistency_level: collectionInfo.consistency_level,
  173. replicas: replicas && replicas.replicas,
  174. });
  175. }
  176. }
  177. // add default sort - Descending order
  178. data.sort((a: any, b: any) => b.createdTime - a.createdTime);
  179. return data;
  180. }
  181. async getLoadedColletions() {
  182. const data = [];
  183. const res = await this.getCollections({
  184. type: ShowCollectionsType.Loaded,
  185. });
  186. if (res.data.length > 0) {
  187. for (const item of res.data) {
  188. const { id, name } = item;
  189. const collectionStatistics = await this.getCollectionStatistics({
  190. collection_name: name,
  191. });
  192. data.push({
  193. id,
  194. collection_name: name,
  195. rowCount: findKeyValue(collectionStatistics.stats, ROW_COUNT),
  196. });
  197. }
  198. }
  199. return data;
  200. }
  201. /**
  202. * Get collections statistics data
  203. * @returns {collectionCount:number, totalData:number}
  204. */
  205. async getStatistics() {
  206. const data = {
  207. collectionCount: 0,
  208. totalData: 0,
  209. };
  210. const res = await this.getCollections();
  211. data.collectionCount = res.data.length;
  212. if (res.data.length > 0) {
  213. for (const item of res.data) {
  214. const collectionStatistics = await this.getCollectionStatistics({
  215. collection_name: item.name,
  216. });
  217. const rowCount = findKeyValue(collectionStatistics.stats, ROW_COUNT);
  218. data.totalData += isNaN(Number(rowCount)) ? 0 : Number(rowCount);
  219. }
  220. }
  221. return data;
  222. }
  223. /**
  224. * Get all collection index status
  225. * @returns {collection_name:string, index_status: IndexState}[]
  226. */
  227. async getCollectionsIndexStatus() {
  228. const data = [];
  229. const res = await this.getCollections();
  230. if (res.data.length > 0) {
  231. for (const item of res.data) {
  232. const indexRes = await this.getIndexStatus({
  233. collection_name: item.name,
  234. });
  235. data.push({
  236. collection_name: item.name,
  237. index_status: indexRes.state,
  238. });
  239. }
  240. }
  241. return data;
  242. }
  243. /**
  244. * Load sample data into collection
  245. */
  246. async importSample({ collection_name, size }: ImportSampleDto) {
  247. const collectionInfo = await this.describeCollection({ collection_name });
  248. const fields_data = genRows(
  249. collectionInfo.schema.fields,
  250. parseInt(size, 10)
  251. );
  252. return await this.insert({ collection_name, fields_data });
  253. }
  254. }