Browse Source

update metric type options

tumao 4 years ago
parent
commit
8ed59fa66e

+ 6 - 7
client/src/consts/Milvus.tsx

@@ -91,11 +91,6 @@ export const INDEX_CONFIG: {
   },
 };
 
-export const INDEX_OPTIONS = Object.keys(INDEX_CONFIG).map(v => ({
-  label: v,
-  value: v,
-}));
-
 export const COLLECTION_NAME_REGX = /^[0-9,a-z,A-Z$_]+$/;
 
 export const m_OPTIONS = [
@@ -108,9 +103,13 @@ export const m_OPTIONS = [
 
 export const INDEX_OPTIONS_MAP = {
   FLOAT_POINT: Object.keys(INDEX_CONFIG).map(v => ({ label: v, value: v })),
-  BINARY_ONE: [{ label: 'FLAT', value: 'FLAT' }],
-  BINARY_TWO: [
+  BINARY: [
     { label: 'FLAT', value: 'FLAT' },
     { label: 'IVF_FLAT', value: 'IVF_FLAT' },
   ],
 };
+
+export enum EmbeddingTypeEnum {
+  float = 'FLOAT_POINT',
+  binary = 'BINARY',
+}

+ 25 - 8
client/src/pages/structure/Create.tsx

@@ -1,31 +1,39 @@
 import { useEffect, useMemo, useState } from 'react';
 import { useTranslation } from 'react-i18next';
 import DialogTemplate from '../../components/customDialog/DialogTemplate';
-import { INDEX_CONFIG, MetricType } from '../../consts/Milvus';
+import {
+  EmbeddingTypeEnum,
+  INDEX_CONFIG,
+  INDEX_OPTIONS_MAP,
+  MetricType,
+} from '../../consts/Milvus';
 import { useFormValidation } from '../../hooks/Form';
 import { formatForm, getMetricOptions } from '../../utils/Form';
+import { DataType } from '../collections/Types';
 import CreateStepTwo from './CreateStepTwo';
 import { IndexType, ParamPair } from './Types';
 
 const CreateIndex = (props: {
   collectionName: string;
-  dimension: string;
+  fieldType: DataType;
   handleCreate: (params: ParamPair[]) => void;
   handleCancel: () => void;
 }) => {
-  const { collectionName, dimension, handleCreate, handleCancel } = props;
+  const { collectionName, fieldType, handleCreate, handleCancel } = props;
 
   const { t: indexTrans } = useTranslation('index');
   const { t: dialogTrans } = useTranslation('dialog');
   const { t: btnTrans } = useTranslation('btn');
 
+  const defaultMetricType = fieldType === 'BinaryVector' ? 'Hamming' : 'L2';
+
   const [indexSetting, setIndexSetting] = useState<{
     index_type: IndexType;
     metric_type: MetricType;
     [x: string]: string;
   }>({
     index_type: 'IVF_FLAT',
-    metric_type: 'L2',
+    metric_type: defaultMetricType,
     M: '',
     m: '4',
     efConstruction: '',
@@ -45,10 +53,18 @@ const CreateIndex = (props: {
   }, [indexSetting.index_type]);
 
   const metricOptions = useMemo(
-    () => getMetricOptions(dimension, indexSetting.index_type),
-    [dimension, indexSetting.index_type]
+    () => getMetricOptions(indexSetting.index_type, fieldType),
+    [indexSetting.index_type, fieldType]
   );
 
+  const indexOptions = useMemo(() => {
+    const type =
+      fieldType === 'BinaryVector'
+        ? EmbeddingTypeEnum.binary
+        : EmbeddingTypeEnum.float;
+    return INDEX_OPTIONS_MAP[type];
+  }, [fieldType]);
+
   const checkedForm = useMemo(() => {
     const paramsForm: any = { metric_type: indexSetting.metric_type };
     indexCreateParams.forEach(v => {
@@ -65,7 +81,7 @@ const CreateIndex = (props: {
     setDisabled(true);
     setIndexSetting(v => ({
       ...v,
-      metric_type: 'L2',
+      metric_type: defaultMetricType,
       M: '',
       m: '4',
       efConstruction: '',
@@ -76,7 +92,7 @@ const CreateIndex = (props: {
       search_length: '',
       knng: '',
     }));
-  }, [indexCreateParams, setDisabled]);
+  }, [indexCreateParams, setDisabled, defaultMetricType]);
 
   const updateStepTwoForm = (type: string, value: string) => {
     setIndexSetting(v => ({ ...v, [type]: value }));
@@ -130,6 +146,7 @@ const CreateIndex = (props: {
       <CreateStepTwo
         updateForm={updateStepTwoForm}
         metricOptions={metricOptions}
+        indexOptions={indexOptions}
         formValue={indexSetting}
         checkIsValid={checkIsValid}
         validation={validation}

+ 4 - 2
client/src/pages/structure/CreateStepTwo.tsx

@@ -4,7 +4,7 @@ import { useTranslation } from 'react-i18next';
 import { ITextfieldConfig } from '../../components/customInput/Types';
 import CustomInput from '../../components/customInput/CustomInput';
 import CustomSelector from '../../components/customSelector/CustomSelector';
-import { INDEX_OPTIONS, m_OPTIONS } from '../../consts/Milvus';
+import { m_OPTIONS } from '../../consts/Milvus';
 import { FormHelperType } from '../../types/Common';
 import { Option } from '../../components/customSelector/Types';
 
@@ -27,6 +27,7 @@ const useStyles = makeStyles((theme: Theme) => ({
 const CreateStepTwo = (
   props: FormHelperType & {
     metricOptions: Option[];
+    indexOptions: Option[];
     indexParams: string[];
     indexTypeChange?: (type: string) => void;
   }
@@ -39,6 +40,7 @@ const CreateStepTwo = (
     validation,
     indexParams,
     indexTypeChange,
+    indexOptions,
     metricOptions,
   } = props;
 
@@ -148,7 +150,7 @@ const CreateStepTwo = (
       <CustomSelector
         label={indexTrans('type')}
         value={formValue.index_type}
-        options={INDEX_OPTIONS}
+        options={indexOptions}
         onChange={(e: { target: { value: unknown } }) => {
           const type = e.target.value;
           updateForm('index_type', type as string);

+ 1 - 1
client/src/pages/structure/IndexTypeElement.tsx

@@ -78,8 +78,8 @@ const IndexTypeElement: FC<{
       params: {
         component: (
           <CreateIndex
-            dimension={data._dimension}
             collectionName={collectionName}
+            fieldType={data._fieldType}
             handleCancel={handleCloseDialog}
             handleCreate={requestCreateIndex}
           />

+ 37 - 26
client/src/utils/Form.ts

@@ -1,8 +1,8 @@
 import { Option } from '../components/customSelector/Types';
-import { METRIC_TYPES, METRIC_TYPES_VALUES } from '../consts/Milvus';
+import { METRIC_TYPES_VALUES } from '../consts/Milvus';
 import { IForm } from '../hooks/Form';
+import { DataType } from '../pages/collections/Types';
 import { IndexType } from '../pages/structure/Types';
-import { checkMultiple } from './Validation';
 
 interface IInfo {
   [key: string]: any;
@@ -21,10 +21,10 @@ export const formatForm = (info: IInfo): IForm[] => {
 };
 
 export const getMetricOptions = (
-  dimension: string,
-  indexType: IndexType
+  indexType: IndexType,
+  fieldType: DataType
 ): Option[] => {
-  const baseOptions = [
+  const baseFloatOptions = [
     {
       value: METRIC_TYPES_VALUES.L2,
       label: 'L2',
@@ -34,32 +34,43 @@ export const getMetricOptions = (
       label: 'IP',
     },
   ];
-  if (!checkMultiple({ value: dimension, multipleNumber: 8 })) {
-    return baseOptions;
-  }
 
-  switch (indexType) {
-    case 'FLAT':
-      return METRIC_TYPES;
+  const baseBinaryOptions = [
+    {
+      value: METRIC_TYPES_VALUES.HAMMING,
+      label: 'Hamming',
+    },
+    {
+      value: METRIC_TYPES_VALUES.JACCARD,
+      label: 'Jaccard',
+    },
+    {
+      value: METRIC_TYPES_VALUES.TANIMOTO,
+      label: 'Tanimoto',
+    },
+  ];
 
-    case 'IVF_FLAT':
-      return [
-        ...baseOptions,
-        {
-          value: METRIC_TYPES_VALUES.HAMMING,
-          label: 'Hamming',
-        },
+  const type = fieldType === 'FloatVector' ? 'ALL' : indexType;
+
+  const baseOptionsMap: { [key: string]: any } = {
+    BinaryVector: {
+      FLAT: [
+        ...baseBinaryOptions,
         {
-          value: METRIC_TYPES_VALUES.JACCARD,
-          label: 'Jaccard',
+          value: METRIC_TYPES_VALUES.SUBSTRUCTURE,
+          label: 'Substructure',
         },
         {
-          value: METRIC_TYPES_VALUES.TANIMOTO,
-          label: 'Tanimoto',
+          value: METRIC_TYPES_VALUES.SUPERSTRUCTURE,
+          label: 'Superstructure',
         },
-      ];
+      ],
+      IVF_FLAT: baseBinaryOptions,
+    },
+    FloatVector: {
+      ALL: baseFloatOptions,
+    },
+  };
 
-    default:
-      return baseOptions;
-  }
+  return baseOptionsMap[fieldType][type];
 };