0

I have user generated data in firestore. for full text search feature I am trying to use elastic app search. I want to deploy Cloud function so whenever a document is written in firestore, a function need to be triggered then the function will index the document.

const functions = require("firebase-functions");
const admin = require("firebase-admin");
const axios = require("axios");
admin.initializeApp(functions.config().firebase);
exports.createBPS = functions.firestore
    .document("MyCollection/{docId}")
    .onWrite((snap, context) => {
      axios.post(
          "https://myapp.ent.us-west1.gcp.cloud.es.io/api/as/v1/engines/myengine/documents?auth_token=private-aadsafdsfadsfdsafdafd",
          snap.after.data()
      );
});

Any help will be appreciated. I am developing Mobile App using the following Flutter Firebase Elastic App Search

Sample document in firestore {"id":"1234566", "name": "My name", "description": "My description", etc...}

2 Answers2

1

I would recommend to use the offical ES SDK. It would make your code much more stable and easier to read.

Here is an instruction how to install it and here a simple example.

Tarik Huber
  • 7,061
  • 2
  • 12
  • 18
-1

You can see below guide on your firebase's extension page.


(Optional) Backfill or import existing documents This extension only sends the content of documents that have been changed – it does not export your full dataset of existing documents into App Search. So, to backfill your dataset with all the documents in your collection, you can run the import script provided by this extension.

Before running the script, first follow the instructions here to “To generate a private key file for your service account”. Download it and save it somewhere as serviceAccountKey.json.

GOOGLE_APPLICATION_CREDENTIALS= /path/to/your/serviceAccountKey.json \
COLLECTION_PATH=TodoItem \
INDEXED_FIELDS=content,isPublic, writerID \
ENTERPRISE_SEARCH_URL=https://todomateelastic.ent.us-central1.gcp.cloud.es.io \
APP_SEARCH_API_KEY= { your private app search API key here } \
APP_SEARCH_ENGINE_NAME=todomatecontent \
npx @elastic/app-search-firestore-extension import
DYL
  • 1