ensureCollection method

Future<CollectionInfo> ensureCollection()

Implementation

Future<CollectionInfo> ensureCollection() async {
  CollectionInfo? inf = await getCollection();

  if (inf == null) {
    CollectionOperationResponse r = await collections.create(
      CreateCollection(
        collectionName: namespace,
        optimizersConfig: OptimizersConfigDiff(memmapThreshold: Int64(20000)),
        onDiskPayload: true,
        walConfig: WalConfigDiff(
          walCapacityMb: Int64(64),
          walSegmentsAhead: Int64(1),
        ),
        hnswConfig: HnswConfigDiff(
          onDisk: false,
          m: Int64(16),
          efConstruct: Int64(128),
        ),
        vectorsConfig: VectorsConfig(
          params: VectorParams(
            onDisk: false,
            datatype: Datatype.Float16,
            size: Int64(dimension),
            distance: Distance.Cosine,
          ),
        ),
      ),
    );
    verbose("Created Collection for $namespace as it diddnt exist yet");
    if (r.result) {
      inf = await getCollection();
      await Future.wait([addIndex("entry"), addIndex("record")]);
    } else {
      throw Exception("Failed to create collection");
    }
  }

  return inf!;
}