Skip to content

Commit

Permalink
finish
Browse files Browse the repository at this point in the history
Signed-off-by: shanghaikid <[email protected]>
  • Loading branch information
shanghaikid committed Oct 29, 2024
1 parent 018eb1b commit c0c04f2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
1 change: 1 addition & 0 deletions client/src/i18n/cn/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const searchTrans = {
outputFields: '输出字段',
consistency: '一致性',
graphNodeHoverTip: '双击以查看更多',
inputVectorTip: '向量或实体ID',
};

export default searchTrans;
1 change: 1 addition & 0 deletions client/src/i18n/en/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const searchTrans = {
outputFields: 'Outputs',
consistency: 'Consistency',
graphNodeHoverTip: 'Double click to explore more',
inputVectorTip: 'Vector or entity id',
};

export default searchTrans;
56 changes: 33 additions & 23 deletions client/src/pages/databases/collections/search/VectorInputBox.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useRef, useEffect, useState, useCallback } from 'react';
import { useRef, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { EditorState, Compartment } from '@codemirror/state';
import { EditorView, keymap } from '@codemirror/view';
import { EditorView, keymap, placeholder } from '@codemirror/view';
import { insertTab } from '@codemirror/commands';
import { indentUnit } from '@codemirror/language';
import { minimalSetup } from 'codemirror';
Expand Down Expand Up @@ -114,6 +115,7 @@ let queryTimeout: NodeJS.Timeout;

export default function VectorInputBox(props: VectorInputBoxProps) {
const theme = useTheme();
const { t: searchTrans } = useTranslation('search');

// props
const { searchParams, onChange, collection } = props;
Expand Down Expand Up @@ -151,7 +153,7 @@ export default function VectorInputBox(props: VectorInputBoxProps) {
editor.current.dispatch({
changes: {
from: 0,
to: data.length+1,
to: data.length + 1,
insert: data,
},
});
Expand All @@ -160,36 +162,41 @@ export default function VectorInputBox(props: VectorInputBoxProps) {
}, [JSON.stringify(searchParams), onChange]);

const getVectorById = (text: string) => {
if (queryTimeout) {
clearTimeout(queryTimeout);
}
// only search for text that doesn't have space, comma, or brackets or curly brackets
if (!text.trim().match(/[\s,{}]/)) {
if (queryTimeout) {
clearTimeout(queryTimeout);
const isVarChar =
collection.schema.primaryField.data_type === DataTypeStringEnum.VarChar;

if (!isVarChar && isNaN(Number(text))) {
return;
}

queryTimeout = setTimeout(() => {
try {
CollectionService.queryData(collection.collection_name, {
expr:
collection!.schema.primaryField.data_type ===
DataTypeStringEnum.VarChar
? `${collection.schema.primaryField.name} == '${text}'`
: `${collection.schema.primaryField.name} == ${text}`,
expr: isVarChar
? `${collection.schema.primaryField.name} == '${text}'`
: `${collection.schema.primaryField.name} == ${text}`,
output_fields: [searchParamsRef.current.anns_field],
})
.then(res => {
onChangeRef.current(
searchParamsRef.current.anns_field,
res.data && res.data.length === 1
? JSON.stringify(
res.data[0][searchParamsRef.current.anns_field]
)
: text
);
if (res.data && res.data.length === 1) {
onChangeRef.current(
searchParamsRef.current.anns_field,
JSON.stringify(
res.data[0][searchParamsRef.current.anns_field]
)
);
}
})
.catch(e => {
console.log(0, e);
});
} catch (e) {}
}, 550);
}, 300);
}
};

Expand All @@ -201,6 +208,7 @@ export default function VectorInputBox(props: VectorInputBoxProps) {
extensions: [
minimalSetup,
javascript(),
placeholder('vector or entity id'),
linter(view => {
const text = view.state.doc.toString();

Expand Down Expand Up @@ -279,11 +287,13 @@ export default function VectorInputBox(props: VectorInputBoxProps) {
});

editor.current = view;
editor.current!.focus();

editorEl.current!.onclick = () => {
editor.current!.focus();
};
// focus editor, the cursor will be at the end of the text
const endPos = editor.current.state.doc.length;
editor.current.dispatch({
selection: { anchor: endPos },
});
editor.current.focus(); // 聚焦到编辑器

return () => {
view.destroy();
Expand Down

0 comments on commit c0c04f2

Please sign in to comment.