Browse Source

Merge pull request #124 from nameczz/main

add insert api
ryjiang 4 years ago
parent
commit
f17c0c65cf

+ 1 - 1
server/package.json

@@ -30,7 +30,7 @@
     "@nestjs/swagger": "^4.8.0",
     "@nestjs/swagger": "^4.8.0",
     "@types/passport-jwt": "^3.0.5",
     "@types/passport-jwt": "^3.0.5",
     "@types/passport-local": "^1.0.33",
     "@types/passport-local": "^1.0.33",
-    "@zilliz/milvus2-sdk-node": "^1.0.0",
+    "@zilliz/milvus2-sdk-node": "^1.0.2",
     "class-transformer": "^0.4.0",
     "class-transformer": "^0.4.0",
     "class-validator": "^0.13.1",
     "class-validator": "^0.13.1",
     "passport": "^0.4.1",
     "passport": "^0.4.1",

+ 9 - 1
server/src/collections/collections.controller.ts

@@ -12,7 +12,7 @@ import {
 } from '@nestjs/common';
 } from '@nestjs/common';
 import { ApiTags } from '@nestjs/swagger';
 import { ApiTags } from '@nestjs/swagger';
 import { CollectionsService } from './collections.service';
 import { CollectionsService } from './collections.service';
-import { CreateCollection, ShowCollections } from './dto';
+import { CreateCollection, InsertData, ShowCollections } from './dto';
 
 
 @ApiTags('collections')
 @ApiTags('collections')
 @Controller('collections')
 @Controller('collections')
@@ -77,4 +77,12 @@ export class CollectionsController {
       collection_name: name,
       collection_name: name,
     });
     });
   }
   }
+
+  @Post(':name/insert')
+  async insertData(@Param('name') name: string, @Body() data: InsertData) {
+    return await this.collectionsService.insert({
+      collection_name: name,
+      ...data,
+    });
+  }
 }
 }

+ 7 - 0
server/src/collections/collections.service.ts

@@ -6,6 +6,7 @@ import {
   DropCollectionReq,
   DropCollectionReq,
   GetCollectionStatisticsReq,
   GetCollectionStatisticsReq,
   GetIndexStateReq,
   GetIndexStateReq,
+  InsertReq,
   LoadCollectionReq,
   LoadCollectionReq,
   ReleaseLoadCollectionReq,
   ReleaseLoadCollectionReq,
 } from '@zilliz/milvus2-sdk-node/dist/milvus/types';
 } from '@zilliz/milvus2-sdk-node/dist/milvus/types';
@@ -66,6 +67,12 @@ export class CollectionsService {
     return res;
     return res;
   }
   }
 
 
+  async insert(data: InsertReq) {
+    const res = await this.milvusClient.insert(data);
+    throwErrorFromSDK(res.status);
+    return res;
+  }
+
   /**
   /**
    * We do not throw error for this.
    * We do not throw error for this.
    * Because if collection dont have index, it will throw error.
    * Because if collection dont have index, it will throw error.

+ 14 - 0
server/src/collections/dto.ts

@@ -53,3 +53,17 @@ export class ShowCollections {
   @IsEnum(ShowCollectionsType, { message: 'Type allow all->0 inmemory->1' })
   @IsEnum(ShowCollectionsType, { message: 'Type allow all->0 inmemory->1' })
   readonly type: ShowCollectionsType;
   readonly type: ShowCollectionsType;
 }
 }
+
+export class InsertData {
+  @ApiProperty({
+    description: 'Partition in this collection',
+  })
+  @IsOptional()
+  readonly partition_names: string[];
+
+  @ApiProperty({
+    description: 'The fields data in this collection',
+    default: [{ vector: [1, 2, 3], a: 1, b: 2 }],
+  })
+  readonly fields_data: any[];
+}

+ 13 - 3
server/src/milvus/dto.ts

@@ -1,9 +1,9 @@
 import { ApiProperty } from '@nestjs/swagger';
 import { ApiProperty } from '@nestjs/swagger';
-import { IsNotEmpty, IsString } from 'class-validator';
+import { ArrayMinSize, IsArray, IsNotEmpty, IsString } from 'class-validator';
 
 
 export class ConnectMilvus {
 export class ConnectMilvus {
   @ApiProperty({
   @ApiProperty({
-    description: 'Milvus url'
+    description: 'Milvus url',
   })
   })
   @IsString()
   @IsString()
   @IsNotEmpty({
   @IsNotEmpty({
@@ -14,7 +14,7 @@ export class ConnectMilvus {
 
 
 export class CheckMilvus {
 export class CheckMilvus {
   @ApiProperty({
   @ApiProperty({
-    description: 'Milvus url'
+    description: 'Milvus url',
   })
   })
   @IsString()
   @IsString()
   @IsNotEmpty({
   @IsNotEmpty({
@@ -22,3 +22,13 @@ export class CheckMilvus {
   })
   })
   readonly address: string;
   readonly address: string;
 }
 }
+
+export class Flush {
+  @ApiProperty({
+    description:
+      'The collection names you want flush, flush will flush data into disk.',
+  })
+  @IsArray()
+  @ArrayMinSize(1, { message: 'At least need one collection name.' })
+  readonly collection_names: string[];
+}

+ 15 - 2
server/src/milvus/milvus.controller.ts

@@ -1,7 +1,15 @@
-import { Body, Controller, Get, Post, Query, UsePipes } from '@nestjs/common';
+import {
+  Body,
+  Controller,
+  Get,
+  Post,
+  Put,
+  Query,
+  UsePipes,
+} from '@nestjs/common';
 import { ApiTags } from '@nestjs/swagger';
 import { ApiTags } from '@nestjs/swagger';
 import { ValidationPipe } from 'src/pipe/validation.pipe';
 import { ValidationPipe } from 'src/pipe/validation.pipe';
-import { CheckMilvus, ConnectMilvus } from './dto';
+import { CheckMilvus, ConnectMilvus, Flush } from './dto';
 import { MilvusService } from './milvus.service';
 import { MilvusService } from './milvus.service';
 
 
 @ApiTags('milvus')
 @ApiTags('milvus')
@@ -19,4 +27,9 @@ export class MilvusController {
   async checkConnect(@Query() query: CheckMilvus): Promise<any> {
   async checkConnect(@Query() query: CheckMilvus): Promise<any> {
     return await this.milvusService.checkConnect(query.address);
     return await this.milvusService.checkConnect(query.address);
   }
   }
+
+  @Put('flush')
+  async flush(@Body() data: Flush) {
+    return await this.milvusService.flush(data);
+  }
 }
 }

+ 6 - 0
server/src/milvus/milvus.service.ts

@@ -1,5 +1,6 @@
 import { Injectable } from '@nestjs/common';
 import { Injectable } from '@nestjs/common';
 import { MilvusClient } from '@zilliz/milvus2-sdk-node';
 import { MilvusClient } from '@zilliz/milvus2-sdk-node';
+import { FlushReq } from '@zilliz/milvus2-sdk-node/dist/milvus/types';
 @Injectable()
 @Injectable()
 export class MilvusService {
 export class MilvusService {
   private milvusAddress: string;
   private milvusAddress: string;
@@ -40,4 +41,9 @@ export class MilvusService {
       connected: res.address ? true : false,
       connected: res.address ? true : false,
     };
     };
   }
   }
+
+  async flush(data: FlushReq) {
+    const res = await this.milvusClient.flush(data);
+    return res;
+  }
 }
 }

+ 4 - 1
server/src/utils/ConsoleLogger.ts

@@ -4,7 +4,10 @@ import * as chalk from 'chalk';
 export class ConsoleLogger implements LoggerService {
 export class ConsoleLogger implements LoggerService {
   log(...messages: any[]) {
   log(...messages: any[]) {
     // tslint:disable-next-line:no-console
     // tslint:disable-next-line:no-console
-    console.log(chalk.green(`milvus-insight: [${ConsoleLogger.time()}]`), ...messages);
+    console.log(
+      chalk.green(`milvus-insight: [${ConsoleLogger.time()}]`),
+      ...messages,
+    );
   }
   }
   error(...messages: any[]) {
   error(...messages: any[]) {
     // tslint:disable-next-line:no-console
     // tslint:disable-next-line:no-console

+ 4 - 4
server/yarn.lock

@@ -1292,10 +1292,10 @@
   resolved "https://registry.yarnpkg.com/@xtuc/long/-/long-4.2.2.tgz#d291c6a4e97989b5c61d9acf396ae4fe133a718d"
   resolved "https://registry.yarnpkg.com/@xtuc/long/-/long-4.2.2.tgz#d291c6a4e97989b5c61d9acf396ae4fe133a718d"
   integrity sha512-NuHqBY1PB/D8xU6s/thBgOAiAP7HOYDQ32+BFZILJ8ivkUkAHQnWfn6WhL79Owj1qmUnoN/YPhktdIoucipkAQ==
   integrity sha512-NuHqBY1PB/D8xU6s/thBgOAiAP7HOYDQ32+BFZILJ8ivkUkAHQnWfn6WhL79Owj1qmUnoN/YPhktdIoucipkAQ==
 
 
-"@zilliz/milvus2-sdk-node@^1.0.0":
-  version "1.0.0"
-  resolved "https://registry.yarnpkg.com/@zilliz/milvus2-sdk-node/-/milvus2-sdk-node-1.0.0.tgz#8f846c446e2017812e560c6387f6351222eb26f0"
-  integrity sha512-QlVeqCEwdtP97err+cRq5niFMt7zbfxDynR2NnvvSBR8RyT7qIRz7zyWDqbqTCSDqyXI62QNT0OdA3O53k3HaA==
+"@zilliz/milvus2-sdk-node@^1.0.2":
+  version "1.0.2"
+  resolved "https://registry.yarnpkg.com/@zilliz/milvus2-sdk-node/-/milvus2-sdk-node-1.0.2.tgz#68aecdaa9d2f27058d9af8322e3fc7acaf31da1e"
+  integrity sha512-Uwz0OZqvu8hcaoUU5xXKkJpgx2xwDttzCJihqttB4iTMrn7HERZeFJZLYgeL3BbxdEgKF0jbxGlstZ2kUWx9fQ==
   dependencies:
   dependencies:
     "@grpc/grpc-js" "^1.2.12"
     "@grpc/grpc-js" "^1.2.12"
     "@grpc/proto-loader" "^0.6.0"
     "@grpc/proto-loader" "^0.6.0"