Py.ts 813 B

123456789101112131415161718192021222324252627
  1. import { getObjFromKeyValuePair } from '../Format';
  2. import { parseValue } from '../Insert';
  3. import { CreateIndexCodeParam } from './Types';
  4. // use replacer to parse extra param from JSON to original object
  5. const replacer = (key: string, value: any) => {
  6. if (typeof value === 'string') {
  7. return parseValue(value);
  8. }
  9. return value;
  10. };
  11. export const getCreateIndexPYCode = (params: CreateIndexCodeParam) => {
  12. const { collectionName, fieldName, extraParams } = params;
  13. const obj = getObjFromKeyValuePair(extraParams);
  14. const index = {
  15. ...obj,
  16. params: parseValue(obj.params),
  17. };
  18. const pyCode = `from pymilvus_orm import Collection
  19. collection = Collection('${collectionName}')
  20. index = ${JSON.stringify(index, replacer, 4)}
  21. collection.create_index(${fieldName}, index)`;
  22. return pyCode;
  23. };