Skip to content

Commit

Permalink
feat(firestore-vector-searc): existing index creation check
Browse files Browse the repository at this point in the history
  • Loading branch information
jauntybrain authored and cabljac committed Apr 19, 2024
1 parent 85a4a22 commit 13216eb
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion firestore-vector-search/functions/src/queries/setup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {firestoreAdminClient} from '../config';
import * as functions from 'firebase-functions';

interface CreateIndexOptions {
collectionName: string;
Expand All @@ -24,12 +25,28 @@ const getIndex = (options: CreateIndexOptions) => ({
});

export async function createIndex(options: CreateIndexOptions) {
const allIndexes = await firestoreAdminClient.listIndexes({
parent: getParent(options),
});
const indexExists = allIndexes[0].some(index => {
const hasCollectionName = index.name?.includes(options.collectionName);
const hasFieldPath = index.fields?.some(
field => field.fieldPath === options.fieldPath
);
return hasCollectionName && hasFieldPath;
});

if (indexExists) {
functions.logger.info(`Index already exists, skipping index creation`);
return;
}

const result = await firestoreAdminClient.createIndex({
parent: getParent(options),
index: getIndex(options),
});

console.log(`Index created: ${JSON.stringify(result)}`);
functions.logger.info(`Index created: ${JSON.stringify(result)}`);
}

export async function checkCreateIndexProgress(indexName: string) {
Expand Down

0 comments on commit 13216eb

Please sign in to comment.