VectorSearch.tsx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. import { TextField, Typography, Button } from '@material-ui/core';
  2. import { useTranslation } from 'react-i18next';
  3. import { useNavigationHook } from '@/hooks/Navigation';
  4. import { ALL_ROUTER_TYPES } from '@/router/Types';
  5. import CustomSelector from '@/components/customSelector/CustomSelector';
  6. import { useCallback, useEffect, useMemo, useState } from 'react';
  7. import SearchParams from './SearchParams';
  8. import { DEFAULT_METRIC_VALUE_MAP } from '../../consts/Milvus';
  9. import { FieldOption, SearchResultView, VectorSearchParam } from './Types';
  10. import AttuGrid from '@/components/grid/Grid';
  11. import EmptyCard from '@/components/cards/EmptyCard';
  12. import icons from '@/components/icons/Icons';
  13. import { usePaginationHook } from '@/hooks/Pagination';
  14. import CustomButton from '@/components/customButton/CustomButton';
  15. import SimpleMenu from '@/components/menu/SimpleMenu';
  16. import { TOP_K_OPTIONS } from './Constants';
  17. import { Option } from '@/components/customSelector/Types';
  18. import { CollectionHttp } from '@/http/Collection';
  19. import { CollectionData, DataTypeEnum } from '../collections/Types';
  20. import { IndexHttp } from '@/http/Index';
  21. import { getVectorSearchStyles } from './Styles';
  22. import { parseValue } from '@/utils/Insert';
  23. import {
  24. classifyFields,
  25. getDefaultIndexType,
  26. getEmbeddingType,
  27. getNonVectorFieldsForFilter,
  28. getVectorFieldOptions,
  29. } from '@/utils/search';
  30. import { ColDefinitionsType } from '@/components/grid/Types';
  31. import Filter from '@/components/advancedSearch';
  32. import { Field } from '@/components/advancedSearch/Types';
  33. import { useLocation } from 'react-router-dom';
  34. import { parseLocationSearch } from '@/utils/Format';
  35. import { cloneObj, generateVector } from '@/utils/Common';
  36. import { CustomDatePicker } from '@/components/customDatePicker/CustomDatePicker';
  37. import { useTimeTravelHook } from '@/hooks/TimeTravel';
  38. import { LOADING_STATE } from '@/consts/Milvus';
  39. import { getLabelDisplayedRows } from './Utils';
  40. import { useSearchResult } from '@/hooks/Result';
  41. const VectorSearch = () => {
  42. useNavigationHook(ALL_ROUTER_TYPES.SEARCH);
  43. const location = useLocation();
  44. // i18n
  45. const { t: searchTrans } = useTranslation('search');
  46. const { t: btnTrans } = useTranslation('btn');
  47. const classes = getVectorSearchStyles();
  48. // data stored inside the component
  49. const [tableLoading, setTableLoading] = useState<boolean>(false);
  50. const [collections, setCollections] = useState<CollectionData[]>([]);
  51. const [selectedCollection, setSelectedCollection] = useState<string>('');
  52. const [fieldOptions, setFieldOptions] = useState<FieldOption[]>([]);
  53. // fields for advanced filter
  54. const [filterFields, setFilterFields] = useState<Field[]>([]);
  55. const [selectedField, setSelectedField] = useState<string>('');
  56. // search params form
  57. const [searchParam, setSearchParam] = useState<{ [key in string]: number }>(
  58. {}
  59. );
  60. // search params disable state
  61. const [paramDisabled, setParamDisabled] = useState<boolean>(true);
  62. // use null as init value before search, empty array means no results
  63. const [searchResult, setSearchResult] = useState<SearchResultView[] | null>(
  64. null
  65. );
  66. // default topK is 100
  67. const [topK, setTopK] = useState<number>(100);
  68. const [expression, setExpression] = useState<string>('');
  69. const [vectors, setVectors] = useState<string>('');
  70. // latency
  71. const [latency, setLatency] = useState<number>(0);
  72. const searchResultMemo = useSearchResult(searchResult as any, classes);
  73. const {
  74. pageSize,
  75. handlePageSize,
  76. currentPage,
  77. handleCurrentPage,
  78. total,
  79. data: result,
  80. order,
  81. orderBy,
  82. handleGridSort,
  83. } = usePaginationHook(searchResultMemo || []);
  84. const { timeTravel, setTimeTravel, timeTravelInfo, handleDateTimeChange } =
  85. useTimeTravelHook();
  86. const collectionOptions: Option[] = useMemo(
  87. () =>
  88. collections.map(c => ({
  89. label: c._name,
  90. value: c._name,
  91. })),
  92. [collections]
  93. );
  94. const outputFields: string[] = useMemo(() => {
  95. const fields =
  96. collections.find(c => c._name === selectedCollection)?._fields || [];
  97. // vector field can't be output fields
  98. const invalidTypes = ['BinaryVector', 'FloatVector'];
  99. const nonVectorFields = fields.filter(
  100. field => !invalidTypes.includes(field._fieldType)
  101. );
  102. return nonVectorFields.map(f => f._fieldName);
  103. }, [selectedCollection, collections]);
  104. const primaryKeyField = useMemo(() => {
  105. const selectedCollectionInfo = collections.find(
  106. c => c._name === selectedCollection
  107. );
  108. const fields = selectedCollectionInfo?._fields || [];
  109. return fields.find(f => f._isPrimaryKey)?._fieldName;
  110. }, [selectedCollection, collections]);
  111. const orderArray = ['id', 'distance', ...outputFields];
  112. const colDefinitions: ColDefinitionsType[] = useMemo(() => {
  113. /**
  114. * id represents primary key, score represents distance
  115. * since we transfer score to distance in the view, and original field which is primary key has already in the table
  116. * we filter 'id' and 'score' to avoid redundant data
  117. */
  118. return searchResult && searchResult.length > 0
  119. ? Object.keys(searchResult[0])
  120. .sort((a, b) => {
  121. const indexA = orderArray.indexOf(a);
  122. const indexB = orderArray.indexOf(b);
  123. return indexA - indexB;
  124. })
  125. .filter(item => {
  126. // if primary key field name is id, don't filter it
  127. const invalidItems =
  128. primaryKeyField === 'id' ? ['score'] : ['id', 'score'];
  129. return !invalidItems.includes(item);
  130. })
  131. .map(key => ({
  132. id: key,
  133. align: 'left',
  134. disablePadding: false,
  135. label: key,
  136. needCopy: primaryKeyField === key,
  137. }))
  138. : [];
  139. }, [searchResult, primaryKeyField, orderArray]);
  140. const [selectedMetricType, setSelectedMetricType] = useState<string>('');
  141. const {
  142. indexType,
  143. indexParams,
  144. fieldType,
  145. embeddingType,
  146. selectedFieldDimension,
  147. } = useMemo(() => {
  148. if (selectedField !== '') {
  149. // field options must contain selected field, so selectedFieldInfo will never undefined
  150. const selectedFieldInfo = fieldOptions.find(
  151. f => f.value === selectedField
  152. );
  153. const index = selectedFieldInfo?.indexInfo;
  154. const embeddingType = getEmbeddingType(selectedFieldInfo!.fieldType);
  155. const metric =
  156. index?._metricType || DEFAULT_METRIC_VALUE_MAP[embeddingType];
  157. const indexParams = index?._indexParameterPairs || [];
  158. const dim = selectedFieldInfo?.dimension || 0;
  159. setSelectedMetricType(metric);
  160. return {
  161. metricType: metric,
  162. indexType: index?._indexType || getDefaultIndexType(embeddingType),
  163. indexParams,
  164. fieldType: DataTypeEnum[selectedFieldInfo?.fieldType!],
  165. embeddingType,
  166. selectedFieldDimension: dim,
  167. };
  168. }
  169. return {
  170. indexType: '',
  171. indexParams: [],
  172. fieldType: 0,
  173. embeddingType: DataTypeEnum.FloatVector,
  174. selectedFieldDimension: 0,
  175. };
  176. }, [selectedField, fieldOptions]);
  177. /**
  178. * vector value validation
  179. * @return whether is valid
  180. */
  181. const vectorValueValid = useMemo(() => {
  182. // if user hasn't input value or not select field, don't trigger validation check
  183. if (vectors === '' || selectedFieldDimension === 0) {
  184. return true;
  185. }
  186. const dim =
  187. fieldType === DataTypeEnum.BinaryVector
  188. ? selectedFieldDimension / 8
  189. : selectedFieldDimension;
  190. const value = parseValue(vectors);
  191. const isArray = Array.isArray(value);
  192. return isArray && value.length === dim;
  193. }, [vectors, selectedFieldDimension, fieldType]);
  194. const searchDisabled = useMemo(() => {
  195. /**
  196. * before search, user must:
  197. * 1. enter vector value, it should be an array and length should be equal to selected field dimension
  198. * 2. choose collection and field
  199. * 3. set extra search params
  200. */
  201. const isInvalid =
  202. vectors === '' ||
  203. selectedCollection === '' ||
  204. selectedField === '' ||
  205. paramDisabled ||
  206. !vectorValueValid;
  207. return isInvalid;
  208. }, [
  209. paramDisabled,
  210. selectedField,
  211. selectedCollection,
  212. vectors,
  213. vectorValueValid,
  214. ]);
  215. // fetch data
  216. const fetchCollections = useCallback(async () => {
  217. const collections = await CollectionHttp.getCollections();
  218. setCollections(collections.filter(c => c._status === LOADING_STATE.LOADED));
  219. }, []);
  220. const fetchFieldsWithIndex = useCallback(
  221. async (collectionName: string, collections: CollectionData[]) => {
  222. const fields =
  223. collections.find(c => c._name === collectionName)?._fields || [];
  224. const indexes = await IndexHttp.getIndexInfo(collectionName);
  225. const { vectorFields, nonVectorFields } = classifyFields(fields);
  226. // only vector type fields can be select
  227. const fieldOptions = getVectorFieldOptions(vectorFields, indexes);
  228. setFieldOptions(fieldOptions);
  229. if (fieldOptions.length > 0) {
  230. // set first option value as default field value
  231. const [{ value: defaultFieldValue }] = fieldOptions;
  232. setSelectedField(defaultFieldValue as string);
  233. }
  234. // only non vector type fields can be advanced filter
  235. const filterFields = getNonVectorFieldsForFilter(nonVectorFields);
  236. setFilterFields(filterFields);
  237. },
  238. []
  239. );
  240. useEffect(() => {
  241. fetchCollections();
  242. }, [fetchCollections]);
  243. // get field options with index when selected collection changed
  244. useEffect(() => {
  245. if (selectedCollection !== '') {
  246. fetchFieldsWithIndex(selectedCollection, collections);
  247. }
  248. }, [selectedCollection, collections, fetchFieldsWithIndex]);
  249. // set default collection value if is from overview page
  250. useEffect(() => {
  251. if (location.search && collections.length > 0) {
  252. const { collectionName } = parseLocationSearch(location.search);
  253. // collection name validation
  254. const isNameValid = collections
  255. .map(c => c._name)
  256. .includes(collectionName);
  257. isNameValid && setSelectedCollection(collectionName);
  258. }
  259. }, [location, collections]);
  260. // icons
  261. const VectorSearchIcon = icons.vectorSearch;
  262. const ResetIcon = icons.refresh;
  263. const ArrowIcon = icons.dropdown;
  264. // methods
  265. const handlePageChange = (e: any, page: number) => {
  266. handleCurrentPage(page);
  267. };
  268. const handleReset = () => {
  269. /**
  270. * reset search includes:
  271. * 1. reset vectors
  272. * 2. reset selected collection and field
  273. * 3. reset search params
  274. * 4. reset advanced filter expression
  275. * 5. clear search result
  276. */
  277. setVectors('');
  278. setSelectedField('');
  279. setSelectedCollection('');
  280. setSearchResult(null);
  281. setFilterFields([]);
  282. setExpression('');
  283. setTimeTravel(null);
  284. };
  285. const handleSearch = async (topK: number, expr = expression) => {
  286. const clonedSearchParams = cloneObj(searchParam);
  287. delete clonedSearchParams.round_decimal;
  288. const searchParamPairs = {
  289. params: JSON.stringify(clonedSearchParams),
  290. anns_field: selectedField,
  291. topk: topK,
  292. metric_type: selectedMetricType,
  293. round_decimal: searchParam.round_decimal,
  294. };
  295. const params: VectorSearchParam = {
  296. output_fields: outputFields,
  297. expr,
  298. search_params: searchParamPairs,
  299. vectors: [parseValue(vectors)],
  300. vector_type: fieldType,
  301. travel_timestamp: timeTravelInfo.timestamp,
  302. };
  303. setTableLoading(true);
  304. try {
  305. const res = await CollectionHttp.vectorSearchData(
  306. selectedCollection,
  307. params
  308. );
  309. setTableLoading(false);
  310. setSearchResult(res.results);
  311. setLatency(res.latency);
  312. } catch (err) {
  313. setTableLoading(false);
  314. }
  315. };
  316. const handleAdvancedFilterChange = (expression: string) => {
  317. setExpression(expression);
  318. if (!searchDisabled) {
  319. handleSearch(topK, expression);
  320. }
  321. };
  322. const handleVectorChange = (value: string) => {
  323. setVectors(value);
  324. };
  325. const fillWithExampleVector = (selectedFieldDimension: number) => {
  326. const v = generateVector(selectedFieldDimension);
  327. setVectors(`[${v}]`);
  328. };
  329. return (
  330. <section className="page-wrapper">
  331. {/* form section */}
  332. <form className={classes.form}>
  333. {/* collection and field selectors */}
  334. <fieldset className="field">
  335. <Typography className="text">{searchTrans('secondTip')}</Typography>
  336. <CustomSelector
  337. options={collectionOptions}
  338. wrapperClass={classes.selector}
  339. variant="filled"
  340. label={searchTrans(
  341. collectionOptions.length === 0 ? 'noCollection' : 'collection'
  342. )}
  343. disabled={collectionOptions.length === 0}
  344. value={selectedCollection}
  345. onChange={(e: { target: { value: unknown } }) => {
  346. const collection = e.target.value;
  347. setSelectedCollection(collection as string);
  348. // every time selected collection changed, reset field
  349. setSelectedField('');
  350. setSearchResult([]);
  351. }}
  352. />
  353. <CustomSelector
  354. options={fieldOptions}
  355. // readOnly can't avoid all events, so we use disabled instead
  356. disabled={selectedCollection === ''}
  357. wrapperClass={classes.selector}
  358. variant="filled"
  359. label={searchTrans('field')}
  360. value={selectedField}
  361. onChange={(e: { target: { value: unknown } }) => {
  362. const field = e.target.value;
  363. setSelectedField(field as string);
  364. }}
  365. />
  366. </fieldset>
  367. {/**
  368. * vector value textarea
  369. * use field-params class because it also has error msg if invalid
  370. */}
  371. <fieldset className="field field-params field-second">
  372. <Typography className="text">
  373. {searchTrans('firstTip', {
  374. dimensionTip:
  375. selectedFieldDimension !== 0
  376. ? `(dimension: ${selectedFieldDimension})`
  377. : '',
  378. })}
  379. {selectedFieldDimension !== 0 ? (
  380. <Button
  381. variant="outlined"
  382. size="small"
  383. onClick={() => {
  384. const dim =
  385. fieldType === DataTypeEnum.BinaryVector
  386. ? selectedFieldDimension / 8
  387. : selectedFieldDimension;
  388. fillWithExampleVector(dim);
  389. }}
  390. >
  391. {btnTrans('example')}
  392. </Button>
  393. ) : null}
  394. </Typography>
  395. <TextField
  396. className="textarea"
  397. InputProps={{
  398. classes: {
  399. root: 'textfield',
  400. multiline: 'multiline',
  401. },
  402. }}
  403. multiline
  404. rows={5}
  405. placeholder={searchTrans('vectorPlaceholder')}
  406. value={vectors}
  407. onChange={(e: React.ChangeEvent<{ value: unknown }>) => {
  408. handleVectorChange(e.target.value as string);
  409. }}
  410. />
  411. {/* validation */}
  412. {!vectorValueValid && (
  413. <Typography variant="caption" className={classes.error}>
  414. {searchTrans('vectorValueWarning', {
  415. dimension:
  416. fieldType === DataTypeEnum.BinaryVector
  417. ? selectedFieldDimension / 8
  418. : selectedFieldDimension,
  419. })}
  420. </Typography>
  421. )}
  422. </fieldset>
  423. {/* search params selectors */}
  424. <fieldset className="field field-params">
  425. <Typography className="text">{searchTrans('thirdTip')}</Typography>
  426. <SearchParams
  427. wrapperClass={classes.paramsWrapper}
  428. metricType={selectedMetricType}
  429. embeddingType={
  430. embeddingType as
  431. | DataTypeEnum.BinaryVector
  432. | DataTypeEnum.FloatVector
  433. }
  434. indexType={indexType}
  435. indexParams={indexParams!}
  436. searchParamsForm={searchParam}
  437. handleFormChange={setSearchParam}
  438. handleMetricTypeChange={setSelectedMetricType}
  439. topK={topK}
  440. setParamsDisabled={setParamDisabled}
  441. />
  442. </fieldset>
  443. </form>
  444. {/**
  445. * search toolbar section
  446. * including topK selector, advanced filter, search and reset btn
  447. */}
  448. <section className={classes.toolbar}>
  449. <div className="left">
  450. <Typography variant="h5" className="text">
  451. {`${searchTrans('result')}: `}
  452. </Typography>
  453. {/* topK selector */}
  454. <SimpleMenu
  455. label={searchTrans('topK', { number: topK })}
  456. menuItems={TOP_K_OPTIONS.map(item => ({
  457. label: item.toString(),
  458. callback: () => {
  459. setTopK(item);
  460. if (!searchDisabled) {
  461. handleSearch(item);
  462. }
  463. },
  464. wrapperClass: classes.menuItem,
  465. }))}
  466. buttonProps={{
  467. className: classes.menuLabel,
  468. endIcon: <ArrowIcon />,
  469. }}
  470. menuItemWidth="108px"
  471. />
  472. <Filter
  473. title="Advanced Filter"
  474. fields={filterFields}
  475. filterDisabled={selectedField === '' || selectedCollection === ''}
  476. onSubmit={handleAdvancedFilterChange}
  477. />
  478. <CustomDatePicker
  479. label={timeTravelInfo.label}
  480. onChange={handleDateTimeChange}
  481. date={timeTravel}
  482. setDate={setTimeTravel}
  483. />
  484. </div>
  485. <div className="right">
  486. <CustomButton className="btn" onClick={handleReset}>
  487. <ResetIcon classes={{ root: 'icon' }} />
  488. {btnTrans('reset')}
  489. </CustomButton>
  490. <CustomButton
  491. variant="contained"
  492. disabled={searchDisabled}
  493. onClick={() => handleSearch(topK)}
  494. >
  495. {btnTrans('search')}
  496. </CustomButton>
  497. </div>
  498. </section>
  499. {/* search result table section */}
  500. {(searchResult && searchResult.length > 0) || tableLoading ? (
  501. <AttuGrid
  502. toolbarConfigs={[]}
  503. colDefinitions={colDefinitions}
  504. rows={result}
  505. rowCount={total}
  506. primaryKey="rank"
  507. page={currentPage}
  508. onChangePage={handlePageChange}
  509. rowsPerPage={pageSize}
  510. setRowsPerPage={handlePageSize}
  511. openCheckBox={false}
  512. isLoading={tableLoading}
  513. orderBy={orderBy}
  514. order={order}
  515. labelDisplayedRows={getLabelDisplayedRows(`(${latency} ms)`)}
  516. handleSort={handleGridSort}
  517. tableCellMaxWidth="100%"
  518. />
  519. ) : (
  520. <EmptyCard
  521. wrapperClass={`page-empty-card`}
  522. icon={<VectorSearchIcon />}
  523. text={
  524. searchResult !== null
  525. ? searchTrans('empty')
  526. : searchTrans('startTip')
  527. }
  528. />
  529. )}
  530. </section>
  531. );
  532. };
  533. export default VectorSearch;