collections.controller.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. ShowCollectionsDto,
  10. VectorSearchDto,
  11. QueryDto,
  12. } from "./dto";
  13. export class CollectionController {
  14. private collectionsService: CollectionsService;
  15. private router: Router;
  16. constructor() {
  17. this.collectionsService = new CollectionsService(milvusService);
  18. this.router = Router();
  19. }
  20. get collectionsServiceGetter() {
  21. return this.collectionsService;
  22. }
  23. generateRoutes() {
  24. /**
  25. * @swagger
  26. * /collections:
  27. * get:
  28. * description: Get all or loaded collection
  29. * responses:
  30. * 200:
  31. * Collections List
  32. */
  33. this.router.get(
  34. "/",
  35. dtoValidationMiddleware(ShowCollectionsDto),
  36. this.showCollections.bind(this)
  37. );
  38. this.router.post(
  39. "/",
  40. dtoValidationMiddleware(CreateCollectionDto),
  41. this.createCollection.bind(this)
  42. );
  43. this.router.get("/statistics", this.getStatistics.bind(this));
  44. this.router.get(
  45. "/:name/statistics",
  46. this.getCollectionStatistics.bind(this)
  47. );
  48. this.router.get(
  49. "/indexes/status",
  50. this.getCollectionsIndexStatus.bind(this)
  51. );
  52. this.router.delete("/:name", this.dropCollection.bind(this));
  53. this.router.get("/:name", this.describeCollection.bind(this));
  54. this.router.put("/:name/load", this.loadCollection.bind(this));
  55. this.router.put("/:name/release", this.releaseCollection.bind(this));
  56. this.router.post(
  57. "/:name/insert",
  58. dtoValidationMiddleware(InsertDataDto),
  59. this.insert.bind(this)
  60. );
  61. this.router.post(
  62. "/:name/search",
  63. dtoValidationMiddleware(VectorSearchDto),
  64. this.vectorSearch.bind(this)
  65. );
  66. this.router.post(
  67. "/:name/query",
  68. dtoValidationMiddleware(QueryDto),
  69. this.query.bind(this)
  70. );
  71. this.router.post(
  72. "/:name/alias",
  73. dtoValidationMiddleware(CreateAliasDto),
  74. this.createAlias.bind(this)
  75. );
  76. return this.router;
  77. }
  78. async showCollections(req: Request, res: Response, next: NextFunction) {
  79. const type = parseInt("" + req.query?.type, 10);
  80. try {
  81. const result =
  82. type === 1
  83. ? await this.collectionsService.getLoadedColletions()
  84. : await this.collectionsService.getAllCollections();
  85. res.send(result);
  86. } catch (error) {
  87. next(error);
  88. }
  89. }
  90. async getStatistics(req: Request, res: Response, next: NextFunction) {
  91. try {
  92. const result = await this.collectionsService.getStatistics();
  93. res.send(result);
  94. } catch (error) {
  95. next(error);
  96. }
  97. }
  98. async createCollection(req: Request, res: Response, next: NextFunction) {
  99. const createCollectionData = req.body;
  100. try {
  101. const result = await this.collectionsService.createCollection(
  102. createCollectionData
  103. );
  104. res.send(result);
  105. } catch (error) {
  106. next(error);
  107. }
  108. }
  109. async dropCollection(req: Request, res: Response, next: NextFunction) {
  110. const name = req.params?.name;
  111. try {
  112. const result = await this.collectionsService.dropCollection({
  113. collection_name: name,
  114. });
  115. res.send(result);
  116. } catch (error) {
  117. next(error);
  118. }
  119. }
  120. async describeCollection(req: Request, res: Response, next: NextFunction) {
  121. const name = req.params?.name;
  122. try {
  123. const result = await this.collectionsService.describeCollection({
  124. collection_name: name,
  125. });
  126. res.send(result);
  127. } catch (error) {
  128. next(error);
  129. }
  130. }
  131. async getCollectionStatistics(
  132. req: Request,
  133. res: Response,
  134. next: NextFunction
  135. ) {
  136. const name = req.params?.name;
  137. try {
  138. const result = await this.collectionsService.getCollectionStatistics({
  139. collection_name: name,
  140. });
  141. res.send(result);
  142. } catch (error) {
  143. next(error);
  144. }
  145. }
  146. async getCollectionsIndexStatus(
  147. req: Request,
  148. res: Response,
  149. next: NextFunction
  150. ) {
  151. try {
  152. const result = await this.collectionsService.getCollectionsIndexStatus();
  153. res.send(result);
  154. } catch (error) {
  155. next(error);
  156. }
  157. }
  158. async loadCollection(req: Request, res: Response, next: NextFunction) {
  159. const name = req.params?.name;
  160. try {
  161. const result = await this.collectionsService.loadCollection({
  162. collection_name: name,
  163. });
  164. res.send(result);
  165. } catch (error) {
  166. next(error);
  167. }
  168. }
  169. async releaseCollection(req: Request, res: Response, next: NextFunction) {
  170. const name = req.params?.name;
  171. try {
  172. const result = await this.collectionsService.releaseCollection({
  173. collection_name: name,
  174. });
  175. res.send(result);
  176. } catch (error) {
  177. next(error);
  178. }
  179. }
  180. async insert(req: Request, res: Response, next: NextFunction) {
  181. const name = req.params?.name;
  182. const data = req.body;
  183. try {
  184. const result = await this.collectionsService.insert({
  185. collection_name: name,
  186. ...data,
  187. });
  188. res.send(result);
  189. } catch (error) {
  190. next(error);
  191. }
  192. }
  193. async vectorSearch(req: Request, res: Response, next: NextFunction) {
  194. const name = req.params?.name;
  195. const data = req.body;
  196. try {
  197. const result = await this.collectionsService.vectorSearch({
  198. collection_name: name,
  199. ...data,
  200. });
  201. res.send(result);
  202. } catch (error) {
  203. next(error);
  204. }
  205. }
  206. async query(req: Request, res: Response, next: NextFunction) {
  207. const name = req.params?.name;
  208. const data = req.body;
  209. const resultiLmit: any = req.query?.limit;
  210. const resultPage: any = req.query?.page;
  211. try {
  212. const limit = isNaN(resultiLmit) ? 100 : parseInt(resultiLmit, 10);
  213. const page = isNaN(resultPage) ? 0 : parseInt(resultPage, 10);
  214. // TODO: add page and limit to node SDK
  215. // Here may raise "Error: 8 RESOURCE_EXHAUSTED: Received message larger than max"
  216. const result = await this.collectionsService.query({
  217. collection_name: name,
  218. ...data,
  219. });
  220. const queryResultList = result.data;
  221. const queryResultLength = result.data.length;
  222. const startNum = page * limit;
  223. const endNum = (page + 1) * limit;
  224. const slicedResult = queryResultList.slice(startNum, endNum);
  225. result.data = slicedResult;
  226. res.send({ ...result, limit, page, total: queryResultLength });
  227. } catch (error) {
  228. next(error);
  229. }
  230. }
  231. async createAlias(req: Request, res: Response, next: NextFunction) {
  232. const name = req.params?.name;
  233. const data = req.body;
  234. try {
  235. const result = await this.collectionsService.createAlias({
  236. collection_name: name,
  237. ...data,
  238. });
  239. res.send(result);
  240. } catch (error) {
  241. next(error);
  242. }
  243. }
  244. }