Browse Source

add index api

nameczz 4 years ago
parent
commit
56ca46656c

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

@@ -28,6 +28,7 @@ export class MilvusService {
       });
       return { address: this.milvusAddress };
     } catch (error) {
+      console.log(error);
       throw new Error('Connect milvus failed, check your milvus address.');
     }
   }

+ 1 - 1
server/src/partitions/dto.ts

@@ -35,7 +35,7 @@ export class ManagePartition {
   })
   readonly partition_name: string;
 
-  @ApiProperty()
+  @ApiProperty({ enum: ManageType })
   @IsEnum(ManageType, { message: 'Type allow delete and create' })
   readonly type: ManageType;
 }

+ 68 - 3
server/src/schema/dto.ts

@@ -1,5 +1,11 @@
 import { ApiProperty } from '@nestjs/swagger';
-import { IsNotEmpty, IsString, IsArray } from 'class-validator';
+import {
+  IsNotEmpty,
+  IsString,
+  IsArray,
+  IsEnum,
+  IsOptional,
+} from 'class-validator';
 
 class KeyValuePair {
   @ApiProperty()
@@ -8,7 +14,16 @@ class KeyValuePair {
   value: string;
 }
 
-export class CreateIndex {
+export enum ManageType {
+  DELETE = 'delete',
+  CREATE = 'create',
+}
+
+export class ManageIndex {
+  @ApiProperty({ enum: ManageType })
+  @IsEnum(ManageType, { message: 'Type allow delete and create' })
+  readonly type: ManageType;
+
   @ApiProperty()
   @IsString()
   @IsNotEmpty({
@@ -27,5 +42,55 @@ export class CreateIndex {
     type: [KeyValuePair],
   })
   @IsArray()
-  readonly extra_params: KeyValuePair[];
+  @IsOptional()
+  readonly extra_params?: KeyValuePair[];
+}
+
+export class DescribeIndex {
+  @ApiProperty()
+  @IsString()
+  @IsNotEmpty({
+    message: 'collection_name is empty',
+  })
+  readonly collection_name: string;
+
+  @ApiProperty()
+  @IsString()
+  @IsOptional()
+  readonly field_name?: string;
+}
+
+export class GetIndexState {
+  @ApiProperty()
+  @IsString()
+  @IsNotEmpty({
+    message: 'collection_name is empty',
+  })
+  readonly collection_name: string;
+
+  @ApiProperty()
+  @IsString()
+  @IsOptional()
+  readonly field_name?: string;
+}
+
+export class GetIndexProgress {
+  @ApiProperty()
+  @IsString()
+  @IsNotEmpty({
+    message: 'collection_name is empty',
+  })
+  readonly collection_name: string;
+
+  @ApiProperty()
+  @IsString()
+  @IsNotEmpty({
+    message: 'index_name is empty',
+  })
+  readonly index_name: string;
+
+  @ApiProperty()
+  @IsString()
+  @IsOptional()
+  readonly field_name?: string;
 }

+ 36 - 4
server/src/schema/schema.controller.ts

@@ -1,11 +1,19 @@
 import {
   Body,
   Controller,
+  Get,
   Post,
+  Query,
   UsePipes,
   ValidationPipe,
 } from '@nestjs/common';
-import { CreateIndex } from './dto';
+import {
+  DescribeIndex,
+  GetIndexProgress,
+  ManageIndex,
+  ManageType,
+  GetIndexState,
+} from './dto';
 import { SchemaService } from './schema.service';
 
 @Controller('schema')
@@ -14,8 +22,32 @@ export class SchemaController {
 
   @Post('index')
   @UsePipes(new ValidationPipe())
-  async createIndex(@Body() body: CreateIndex) {
-    console.log(body);
-    return await this.schemaService.createIndex(body);
+  async manageIndex(@Body() body: ManageIndex) {
+    const { type, collection_name, extra_params, field_name } = body;
+    return type === ManageType.CREATE
+      ? await this.schemaService.createIndex({
+          collection_name,
+          extra_params,
+          field_name,
+        })
+      : await this.schemaService.dropIndex({ collection_name, field_name });
+  }
+
+  @Get('index')
+  @UsePipes(new ValidationPipe())
+  async describeIndex(@Query() query: DescribeIndex) {
+    return await this.schemaService.describeIndex(query);
+  }
+
+  @Get('index/progress')
+  @UsePipes(new ValidationPipe())
+  async getIndexProgress(@Query() query: GetIndexProgress) {
+    return await this.schemaService.getIndexBuildProgress(query);
+  }
+
+  @Get('index/state')
+  @UsePipes(new ValidationPipe())
+  async getIndexState(@Query() query: GetIndexState) {
+    return await this.schemaService.getIndexState(query);
   }
 }

+ 2 - 0
server/src/schema/schema.service.ts

@@ -6,6 +6,7 @@ import {
   GetIndexBuildProgressReq,
   GetIndexStateReq,
 } from '@zilliz/milvus-sdk-node-dev/dist/milvus/types';
+import { throwErrorFromSDK } from 'src/utils/Error';
 import { MilvusService } from '../milvus/milvus.service';
 
 @Injectable()
@@ -28,6 +29,7 @@ export class SchemaService {
 
   async dropIndex(data: DropIndexReq) {
     const res = await this.milvusClient.dropIndex(data);
+    throwErrorFromSDK(res);
     return res;
   }