简体   繁体   中英

How to index data from Firebase Firestore to Elastic App Search using Cloud Function?

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...}

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.

同时有一个 Firebase 扩展可以满足您的需求: https : //firebase.google.com/products/extensions/elastic-firestore-elastic-app-search

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM