|
| 1 | +/** |
| 2 | + * Copyright 2021 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the 'License'); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an 'AS IS' BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +const functions = require("firebase-functions"); |
| 17 | + |
| 18 | +// [START init_elastic] |
| 19 | +const { Client } = require("@elastic/elasticsearch"); |
| 20 | + |
| 21 | +// Initialize Elastic, requires installing Elastic dependencies: |
| 22 | +// https://github.com/elastic/elasticsearch-js |
| 23 | +// |
| 24 | +// ID, username, and password are stored in functions config variables |
| 25 | +const ELASTIC_ID = functions.config().elastic.id; |
| 26 | +const ELASTIC_USERNAME = functions.config().elastic.username; |
| 27 | +const ELASTIC_PASSWORD = functions.config().elastic.password; |
| 28 | + |
| 29 | +const client = new Client({ |
| 30 | + cloud: { |
| 31 | + id: ELASTIC_ID, |
| 32 | + username: ELASTIC_USERNAME, |
| 33 | + password: ELASTIC_PASSWORD, |
| 34 | + } |
| 35 | +}); |
| 36 | +// [END init_elastic] |
| 37 | + |
| 38 | +// [START update_index_function_elastic] |
| 39 | +// Update the search index every time a blog post is written. |
| 40 | +exports.onNoteCreated = functions.firestore.document('notes/{noteId}').onCreate(async (snap, context) => { |
| 41 | + // Get the note document |
| 42 | + const note = snap.data(); |
| 43 | + |
| 44 | + // Use the 'nodeId' path segment as the identifier for Elastic |
| 45 | + const id = context.params.noteId; |
| 46 | + |
| 47 | + // Write to the Elastic index |
| 48 | + client.index({ |
| 49 | + index: "notes", |
| 50 | + id, |
| 51 | + body: note, |
| 52 | + }); |
| 53 | +}); |
| 54 | +// [END update_index_function_elastic] |
| 55 | + |
| 56 | +// [START search_function_elastic] |
| 57 | +exports.searchNotes = functions.https.onCall(async (data, context) => { |
| 58 | + const query = data.query; |
| 59 | + |
| 60 | + // Search for any notes where the text field contains the query text. |
| 61 | + // For more search examples see: |
| 62 | + // https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/search_examples.html |
| 63 | + const searchRes = await client.search({ |
| 64 | + index: "notes", |
| 65 | + body: { |
| 66 | + query: { |
| 67 | + query_string: { |
| 68 | + query: `*${query}*`, |
| 69 | + fields: [ |
| 70 | + "text" |
| 71 | + ] |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + // Each entry will have the following properties: |
| 78 | + // _score: a score for how well the item matches the search |
| 79 | + // _source: the original item data |
| 80 | + const hits = searchRes.body.hits.hits; |
| 81 | + |
| 82 | + const notes = hits.map(h => h["_source"]); |
| 83 | + return { |
| 84 | + notes: notes |
| 85 | + }; |
| 86 | +}); |
| 87 | +// [END search_function_elastic] |
0 commit comments