Fireship · @fireship
Algolia with Firebase Cloud Functions - Part 2
Uploaded July 18, 2017 · Length 3:01
Summary
Implementing Algolia search with Firebase Cloud Functions, creating a custom index on the backend and updating it with new data from the Firebase database.
- algolia
- firebase cloud functions
- custom index
- full text search
- firebase database
Transcript preview
The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.
- 0:00
now that we know how to implement algo
- 0:01
Lea's search on the front end we're
- 0:03
going to create a fire based cloud
- 0:04
function to build our own custom index
- 0:07
on the backend in this example we will
- 0:09
create an index of books from the Google
- 0:11
Books API anytime this book data changes
- 0:14
on the database it will automatically
- 0:16
invoke the function telling al Goliad to
- 0:19
update the index the first step is to
- 0:21
create a new index on the algo Lea
- 0:23
dashboard in this case we'll just call
- 0:25
it books currently the index is empty
- 0:28
but when we add new data to the firebase
- 0:30
database we want to see the index
- 0:31
updated with that new data so if we add
- 0:34
a new book to firebase then go back to
- 0:36
al Golia we should see that book in the
- 0:38
index now let's see how we make this
- 0:41
possible with firebase cloud functions
- 0:43
first initialize functions in your
- 0:45
project then CD into the functions
- 0:47
directory and install al kolya by
- 0:50
running NPM install al Golia search then
- 0:53
set your al Golia API keys in the
- 0:55
functions environment you want to make
- 0:57
sure to use the admin API key in the
- 1:01
index.js file we start by importing the
- 1:03
firebase admin and then initialize al
- 1:06
Golia search with the API keys that we
- 1:08
just said the name of the function will
- 1:11
be update index and it will run anytime
- 1:14
data is written to this node in the
- 1:16
database which is books slash book ID we
- 1:19
reference the index by calling and knit
- 1:21
index then pass the name we define in
- 1:23
the AL Golia dashboard then we set a
- 1:25
variable for the data in the firebase
- 1:27
database then we set the AL Golia object
- 1:31
ID to whatever the book ID happens to be
- 1:33
every record in your al Golia index is
- 1:36
required to have a unique object ID in
- 1:38
most cases you'd want to use the
- 1:40
firebase push key you can add new data
- 1:42
to the index by calling index dot save
- 1:45
object and then pass it that data object
- 1:47
then you have a callback from which you
- 1:49
can catch the error or in this case we
- 1:51
just console.log the success of the
- 1:53
update so far the function will only
- 1:55
work when data is created or updated so
- 1:58
we check to see if the data variable is


