2
0

collections.controller.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. import { NextFunction, Request, Response, Router } from 'express';
  2. import { dtoValidationMiddleware } from '../middlewares/validation';
  3. import { milvusService } from '../milvus';
  4. import { CollectionsService } from './collections.service';
  5. import {
  6. CreateAliasDto,
  7. CreateCollectionDto,
  8. InsertDataDto,
  9. ImportSampleDto,
  10. VectorSearchDto,
  11. QueryDto,
  12. RenameCollectionDto,
  13. } from './dto';
  14. import { LoadCollectionReq } from '@zilliz/milvus2-sdk-node';
  15. export class CollectionController {
  16. private collectionsService: CollectionsService;
  17. private router: Router;
  18. constructor() {
  19. this.collectionsService = new CollectionsService(milvusService);
  20. this.router = Router();
  21. }
  22. get collectionsServiceGetter() {
  23. return this.collectionsService;
  24. }
  25. generateRoutes() {
  26. this.router.get('/', this.showCollections.bind(this));
  27. this.router.post(
  28. '/',
  29. dtoValidationMiddleware(CreateCollectionDto),
  30. this.createCollection.bind(this)
  31. );
  32. this.router.get('/statistics', this.getStatistics.bind(this));
  33. this.router.get(
  34. '/:name/statistics',
  35. this.getCollectionStatistics.bind(this)
  36. );
  37. this.router.get(
  38. '/indexes/status',
  39. this.getCollectionsIndexStatus.bind(this)
  40. );
  41. this.router.delete('/:name', this.dropCollection.bind(this));
  42. this.router.post(
  43. '/:name',
  44. dtoValidationMiddleware(RenameCollectionDto),
  45. this.renameCollection.bind(this)
  46. );
  47. this.router.delete('/:name/alias/:alias', this.dropAlias.bind(this));
  48. this.router.get('/:name', this.describeCollection.bind(this));
  49. this.router.put('/:name/load', this.loadCollection.bind(this));
  50. this.router.put('/:name/release', this.releaseCollection.bind(this));
  51. this.router.post(
  52. '/:name/insert',
  53. dtoValidationMiddleware(InsertDataDto),
  54. this.insert.bind(this)
  55. );
  56. this.router.post(
  57. '/:name/importSample',
  58. dtoValidationMiddleware(ImportSampleDto),
  59. this.importSample.bind(this)
  60. );
  61. // we need use req.body, so we can't use delete here
  62. this.router.put('/:name/entities', this.deleteEntities.bind(this));
  63. this.router.post(
  64. '/:name/search',
  65. dtoValidationMiddleware(VectorSearchDto),
  66. this.vectorSearch.bind(this)
  67. );
  68. this.router.post(
  69. '/:name/query',
  70. dtoValidationMiddleware(QueryDto),
  71. this.query.bind(this)
  72. );
  73. this.router.post(
  74. '/:name/alias',
  75. dtoValidationMiddleware(CreateAliasDto),
  76. this.createAlias.bind(this)
  77. );
  78. return this.router;
  79. }
  80. async showCollections(req: Request, res: Response, next: NextFunction) {
  81. const type = parseInt('' + req.query?.type, 10);
  82. try {
  83. const result =
  84. type === 1
  85. ? await this.collectionsService.getLoadedCollections()
  86. : await this.collectionsService.getAllCollections();
  87. res.send(result);
  88. } catch (error) {
  89. next(error);
  90. }
  91. }
  92. async getStatistics(req: Request, res: Response, next: NextFunction) {
  93. try {
  94. const result = await this.collectionsService.getStatistics();
  95. res.send(result);
  96. } catch (error) {
  97. next(error);
  98. }
  99. }
  100. async createCollection(req: Request, res: Response, next: NextFunction) {
  101. const createCollectionData = req.body;
  102. try {
  103. const result = await this.collectionsService.createCollection(
  104. createCollectionData
  105. );
  106. res.send(result);
  107. } catch (error) {
  108. next(error);
  109. }
  110. }
  111. async renameCollection(req: Request, res: Response, next: NextFunction) {
  112. const name = req.params?.name;
  113. const data = req.body;
  114. try {
  115. const result = await this.collectionsService.renameCollection({
  116. collection_name: name,
  117. ...data,
  118. });
  119. res.send(result);
  120. } catch (error) {
  121. next(error);
  122. }
  123. }
  124. async dropCollection(req: Request, res: Response, next: NextFunction) {
  125. const name = req.params?.name;
  126. try {
  127. const result = await this.collectionsService.dropCollection({
  128. collection_name: name,
  129. });
  130. res.send(result);
  131. } catch (error) {
  132. next(error);
  133. }
  134. }
  135. async describeCollection(req: Request, res: Response, next: NextFunction) {
  136. const name = req.params?.name;
  137. try {
  138. const result = await this.collectionsService.describeCollection({
  139. collection_name: name,
  140. });
  141. res.send(result);
  142. } catch (error) {
  143. next(error);
  144. }
  145. }
  146. async getCollectionStatistics(
  147. req: Request,
  148. res: Response,
  149. next: NextFunction
  150. ) {
  151. const name = req.params?.name;
  152. try {
  153. const result = await this.collectionsService.getCollectionStatistics({
  154. collection_name: name,
  155. });
  156. res.send(result);
  157. } catch (error) {
  158. next(error);
  159. }
  160. }
  161. async getCollectionsIndexStatus(
  162. req: Request,
  163. res: Response,
  164. next: NextFunction
  165. ) {
  166. try {
  167. const result = await this.collectionsService.getCollectionsIndexStatus();
  168. res.send(result);
  169. } catch (error) {
  170. next(error);
  171. }
  172. }
  173. async loadCollection(req: Request, res: Response, next: NextFunction) {
  174. const collection_name = req.params?.name;
  175. const data = req.body;
  176. const param: LoadCollectionReq = { collection_name };
  177. if (data.replica_number) {
  178. param.replica_number = Number(data.replica_number);
  179. }
  180. try {
  181. const result = await this.collectionsService.loadCollection(param);
  182. res.send(result);
  183. } catch (error) {
  184. next(error);
  185. }
  186. }
  187. async releaseCollection(req: Request, res: Response, next: NextFunction) {
  188. const name = req.params?.name;
  189. try {
  190. const result = await this.collectionsService.releaseCollection({
  191. collection_name: name,
  192. });
  193. res.send(result);
  194. } catch (error) {
  195. next(error);
  196. }
  197. }
  198. async insert(req: Request, res: Response, next: NextFunction) {
  199. const name = req.params?.name;
  200. const data = req.body;
  201. try {
  202. const result = await this.collectionsService.insert({
  203. collection_name: name,
  204. ...data,
  205. });
  206. res.send(result);
  207. } catch (error) {
  208. next(error);
  209. }
  210. }
  211. async importSample(req: Request, res: Response, next: NextFunction) {
  212. const data = req.body;
  213. try {
  214. const result = await this.collectionsService.importSample({
  215. ...data,
  216. });
  217. res.send(result);
  218. } catch (error) {
  219. next(error);
  220. }
  221. }
  222. async deleteEntities(req: Request, res: Response, next: NextFunction) {
  223. const name = req.params?.name;
  224. const data = req.body;
  225. try {
  226. const result = await this.collectionsService.deleteEntities({
  227. collection_name: name,
  228. ...data,
  229. });
  230. res.send(result);
  231. } catch (error) {
  232. next(error);
  233. }
  234. }
  235. async vectorSearch(req: Request, res: Response, next: NextFunction) {
  236. const name = req.params?.name;
  237. const data = req.body;
  238. try {
  239. const result = await this.collectionsService.vectorSearch({
  240. collection_name: name,
  241. ...data,
  242. });
  243. res.send(result);
  244. } catch (error) {
  245. next(error);
  246. }
  247. }
  248. async query(req: Request, res: Response, next: NextFunction) {
  249. const name = req.params?.name;
  250. const data = req.body;
  251. const resultLimit: any = req.query?.limit;
  252. const resultPage: any = req.query?.page;
  253. try {
  254. const limit = isNaN(resultLimit) ? 100 : parseInt(resultLimit, 10);
  255. const page = isNaN(resultPage) ? 0 : parseInt(resultPage, 10);
  256. // TODO: add page and limit to node SDK
  257. // Here may raise "Error: 8 RESOURCE_EXHAUSTED: Received message larger than max"
  258. const result = await this.collectionsService.query({
  259. collection_name: name,
  260. ...data,
  261. });
  262. // const queryResultList = result.data;
  263. const queryResultLength = result.data.length;
  264. // const startNum = page * limit;
  265. // const endNum = (page + 1) * limit;
  266. // const slicedResult = queryResultList.slice(startNum, endNum);
  267. // result.data = slicedResult;
  268. res.send({ ...result, limit, page, total: queryResultLength });
  269. } catch (error) {
  270. next(error);
  271. }
  272. }
  273. async createAlias(req: Request, res: Response, next: NextFunction) {
  274. const name = req.params?.name;
  275. const data = req.body;
  276. try {
  277. const result = await this.collectionsService.createAlias({
  278. collection_name: name,
  279. ...data,
  280. });
  281. res.send(result);
  282. } catch (error) {
  283. next(error);
  284. }
  285. }
  286. async dropAlias(req: Request, res: Response, next: NextFunction) {
  287. const alias = req.params?.alias;
  288. try {
  289. const result = await this.collectionsService.dropAlias({ alias });
  290. res.send(result);
  291. } catch (error) {
  292. next(error);
  293. }
  294. }
  295. async getReplicas(req: Request, res: Response, next: NextFunction) {
  296. const collectionID = req.params?.collectionID;
  297. try {
  298. const result = await this.collectionsService.getReplicas({
  299. collectionID,
  300. });
  301. res.send(result);
  302. } catch (error) {
  303. next(error);
  304. }
  305. }
  306. }