search.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { Field } from '../components/advancedSearch/Types';
  2. import { DataType, DataTypeEnum } from '../pages/collections/Types';
  3. import {
  4. FieldData,
  5. IndexType,
  6. IndexView,
  7. INDEX_TYPES_ENUM,
  8. } from '../pages/schema/Types';
  9. import {
  10. FieldOption,
  11. SearchResult,
  12. SearchResultView,
  13. } from '../pages/seach/Types';
  14. export const transferSearchResult = (
  15. result: SearchResult[]
  16. ): SearchResultView[] => {
  17. const resultView = result
  18. .sort((a, b) => a.score - b.score)
  19. .map((r, index) => ({
  20. rank: index + 1,
  21. ...r,
  22. distance: r.score,
  23. }));
  24. return resultView;
  25. };
  26. /**
  27. * function to get EmbeddingType
  28. * @param fieldType only vector type fields: 'BinaryVector' or 'FloatVector'
  29. */
  30. export const getEmbeddingType = (
  31. fieldType: DataType
  32. ): DataTypeEnum.BinaryVector | DataTypeEnum.FloatVector => {
  33. const type =
  34. fieldType === 'BinaryVector'
  35. ? DataTypeEnum.BinaryVector
  36. : DataTypeEnum.FloatVector;
  37. return type;
  38. };
  39. /**
  40. * function to get default index type according to embedding type
  41. * use FLAT as default float index type, BIN_FLAT as default binary index type
  42. * @param embeddingType float or binary
  43. * @returns index type
  44. */
  45. export const getDefaultIndexType = (embeddingType: DataTypeEnum): IndexType => {
  46. const defaultIndexType =
  47. embeddingType === DataTypeEnum.FloatVector
  48. ? INDEX_TYPES_ENUM.FLAT
  49. : INDEX_TYPES_ENUM.BIN_FLAT;
  50. return defaultIndexType;
  51. };
  52. /**
  53. * funtion to divide fields into two categories: vector or nonVector
  54. */
  55. export const classifyFields = (
  56. fields: FieldData[]
  57. ): { vectorFields: FieldData[]; nonVectorFields: FieldData[] } => {
  58. const vectorTypes: DataType[] = ['BinaryVector', 'FloatVector'];
  59. return fields.reduce(
  60. (result, cur) => {
  61. const changedFieldType = vectorTypes.includes(cur._fieldType)
  62. ? 'vectorFields'
  63. : 'nonVectorFields';
  64. result[changedFieldType].push(cur);
  65. return result;
  66. },
  67. { vectorFields: [] as FieldData[], nonVectorFields: [] as FieldData[] }
  68. );
  69. };
  70. export const getVectorFieldOptions = (
  71. fields: FieldData[],
  72. indexes: IndexView[]
  73. ): FieldOption[] => {
  74. const options: FieldOption[] = fields.map(f => {
  75. const embeddingType = getEmbeddingType(f._fieldType);
  76. const defaultIndex = getDefaultIndexType(embeddingType);
  77. const index = indexes.find(i => i._fieldName === f._fieldName);
  78. return {
  79. label: `${f._fieldName} (${index?._indexType || defaultIndex})`,
  80. value: f._fieldName,
  81. fieldType: f._fieldType,
  82. indexInfo: index || null,
  83. dimension: Number(f._dimension),
  84. };
  85. });
  86. return options;
  87. };
  88. export const getNonVectorFieldsForFilter = (fields: FieldData[]): Field[] => {
  89. const intTypes: DataType[] = ['Int8', 'Int16', 'Int32', 'Int64'];
  90. return fields.map(f => ({
  91. name: f._fieldName,
  92. type: intTypes.includes(f._fieldType) ? 'int' : 'float',
  93. }));
  94. };