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