panoet
panoet

Reputation: 3704

Where is the Typesense document ID

I am starting to learn to use Typesense, and I successfully import it from my Firestore. When I want to delete a document from dashboard, it asked the document id.

I know the document id is document id from my firestore collection, since I am not put id field explicitly. But, I can see any document id information from the Typesense dashboard.

Where can I easily find my document id?

Upvotes: 2

Views: 1410

Answers (3)

Ahmad Ali
Ahmad Ali

Reputation: 66

When creating a trigger for a particular collection, it's necessary to specify the collection ID. Here's an example of the code I've implemented:

    import { logger } from "firebase-functions/v2";
    import { onDocumentWritten } from "firebase-functions/v2/firestore";
    import { typesense } from "../utils/typesense";

    // Load environment variables
    const {
      TYPESENSE_KEY,
      COLLECTION,
      COLLECTION_DOCUMENT_PATH,
    } = process.env;

    export const syncTypesenseOnOffsiteNotificationInsert = onDocumentWritten(
      {
        secrets: [TYPESENSE_KEY],
        document: COLLECTION_DOCUMENT_PATH || "collectionName/{docId}",
        maxInstances: 5,
        concurrency: 500,
        retry: true,
        cpu: 2,
        timeoutSeconds: 180,
      },
      async (event) => {
        if (!event.data?.after || !event.data.after.data()) {
          return;
        }

        try {
          if (event.data.before && event.data.before.isEqual(event.data.after)) {
            // Before and after documents match, just return; no need to be in a loop!
            return;
          }

          const offsiteNotification = event.data?.after.data();

          // Additional checks and processing for offsite document can be added here

          logger.info(`Updating ${event.data.after.id}`);

          await typesense
            .collections(COLLECTION || "collectionName")
            .documents()
            .upsert({
              ...collectionName,
              id: event.data.after.id,
              createdAt: collectionName?.createdAt?._seconds,
            });

          logger.info("We are done");
          return;
        } catch (error) {
          logger.error("We encountered an error");
          logger.error(error);
          throw Error("We failed");
        }
      }
    );

When creating a trigger for a particular collection, it's necessary to specify the collection ID. Here's an example of the code I've implemented:

Upvotes: 0

Thiago Watanabe
Thiago Watanabe

Reputation: 1

I always define or before putting it in the collection

sample:

user = {
  id: v4(),
  ...etc
}

Upvotes: 0

Lécio Bourbon
Lécio Bourbon

Reputation: 163

I'm not 100% sure, but I tested and this worked for me: If you explicitly index the id in the collection schema, you can see the id in the dashboard (and it will be the same as the firestore id). If you don't index it, the id will be 0 for the first document; 1 for the second, and so on... Hope i could help

Upvotes: 0

Related Questions