Field.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { DataType } from '../pages/collections/Types';
  2. import { FieldView } from '../pages/structure/Types';
  3. import { IndexState } from '../types/Milvus';
  4. import BaseModel from './BaseModel';
  5. import { IndexHttp } from './Index';
  6. export class FieldHttp extends BaseModel implements FieldView {
  7. data_type!: DataType;
  8. fieldID!: string;
  9. type_params!: { key: string; value: string }[];
  10. is_primary_key!: true;
  11. name!: string;
  12. // data from index http
  13. _indexType!: string;
  14. _indexParameterPairs!: { key: string; value: string }[];
  15. _indexStatus!: IndexState;
  16. _createIndexDisabled!: boolean;
  17. constructor(props: {}) {
  18. super(props);
  19. Object.assign(this, props);
  20. }
  21. static async getFields(collectionName: string): Promise<FieldHttp[]> {
  22. const path = `/collections/${collectionName}`;
  23. const res = await super.findAll({
  24. path,
  25. params: {},
  26. });
  27. return res.schema.fields.map((f: any) => new this(f));
  28. }
  29. static async getStructureListWithIndex(
  30. collectionName: string
  31. ): Promise<FieldHttp[]> {
  32. const vectorTypes: DataType[] = ['BinaryVector', 'FloatVector'];
  33. const indexList = await IndexHttp.getIndexInfo(collectionName);
  34. const structureList = [...(await this.getFields(collectionName))];
  35. let fields: FieldHttp[] = [];
  36. for (const structure of structureList) {
  37. if (vectorTypes.includes(structure.data_type)) {
  38. const index = indexList.find(i => i._fieldName === structure.name);
  39. structure._indexParameterPairs = index?._indexParameterPairs || [];
  40. structure._indexType = index?._indexType || '';
  41. structure._createIndexDisabled = indexList.length > 0;
  42. }
  43. fields = [...fields, structure];
  44. }
  45. return fields;
  46. }
  47. get _fieldId() {
  48. return this.fieldID;
  49. }
  50. get _isPrimaryKey() {
  51. return this.is_primary_key;
  52. }
  53. get _fieldName() {
  54. return this.name;
  55. }
  56. get _fieldType() {
  57. return this.data_type;
  58. }
  59. get _dimension() {
  60. return this.type_params.find(item => item.key === 'dim')?.value || '--';
  61. }
  62. }