Browse Source

Merge remote-tracking branch 'upstream/main' into bugfix/default-partition

tumao 3 years ago
parent
commit
d17ae61220
2 changed files with 22 additions and 4 deletions
  1. 21 3
      client/src/pages/seach/VectorSearch.tsx
  2. 1 1
      client/src/utils/search.ts

+ 21 - 3
client/src/pages/seach/VectorSearch.tsx

@@ -95,11 +95,28 @@ const VectorSearch = () => {
     return nonVectorFields.map(f => f._fieldName);
     return nonVectorFields.map(f => f._fieldName);
   }, [selectedCollection, collections]);
   }, [selectedCollection, collections]);
 
 
+  const primaryKeyField = useMemo(() => {
+    const selectedCollectionInfo = collections.find(
+      c => c._name === selectedCollection
+    );
+    const fields = selectedCollectionInfo?._fields || [];
+    return fields.find(f => f._isPrimaryKey)?._fieldName;
+  }, [selectedCollection, collections]);
+
   const colDefinitions: ColDefinitionsType[] = useMemo(() => {
   const colDefinitions: ColDefinitionsType[] = useMemo(() => {
-    // filter id and score
+    /**
+     * id represents primary key, score represents distance
+     * since we transfer score to distance in the view, and original field which is primary key has already in the table
+     * we filter 'id' and 'score' to avoid redundant data
+     */
     return searchResult && searchResult.length > 0
     return searchResult && searchResult.length > 0
       ? Object.keys(searchResult[0])
       ? Object.keys(searchResult[0])
-          .filter(item => item !== 'id' && item !== 'score')
+          .filter(item => {
+            // if primary key field name is id, don't filter it
+            const invalidItems =
+              primaryKeyField === 'id' ? ['score'] : ['id', 'score'];
+            return !invalidItems.includes(item);
+          })
           .map(key => ({
           .map(key => ({
             id: key,
             id: key,
             align: 'left',
             align: 'left',
@@ -107,7 +124,7 @@ const VectorSearch = () => {
             label: key,
             label: key,
           }))
           }))
       : [];
       : [];
-  }, [searchResult]);
+  }, [searchResult, primaryKeyField]);
 
 
   const {
   const {
     metricType,
     metricType,
@@ -373,6 +390,7 @@ const VectorSearch = () => {
             value={selectedCollection}
             value={selectedCollection}
             onChange={(e: { target: { value: unknown } }) => {
             onChange={(e: { target: { value: unknown } }) => {
               const collection = e.target.value;
               const collection = e.target.value;
+
               setSelectedCollection(collection as string);
               setSelectedCollection(collection as string);
               // every time selected collection changed, reset field
               // every time selected collection changed, reset field
               setSelectedField('');
               setSelectedField('');

+ 1 - 1
client/src/utils/search.ts

@@ -19,8 +19,8 @@ export const transferSearchResult = (
     .sort((a, b) => a.score - b.score)
     .sort((a, b) => a.score - b.score)
     .map((r, index) => ({
     .map((r, index) => ({
       rank: index + 1,
       rank: index + 1,
-      distance: r.score,
       ...r,
       ...r,
+      distance: r.score,
     }));
     }));
 
 
   return resultView;
   return resultView;