Fireship · @fireship

Algolia with Firebase Cloud Functions - Part 2

Uploaded July 18, 2017 · Length 3:01

Algolia with Firebase Cloud Functions - Part 2

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.

  1. 0:00

    now that we know how to implement algo

  2. 0:01

    Lea's search on the front end we're

  3. 0:03

    going to create a fire based cloud

  4. 0:04

    function to build our own custom index

  5. 0:07

    on the backend in this example we will

  6. 0:09

    create an index of books from the Google

  7. 0:11

    Books API anytime this book data changes

  8. 0:14

    on the database it will automatically

  9. 0:16

    invoke the function telling al Goliad to

  10. 0:19

    update the index the first step is to

  11. 0:21

    create a new index on the algo Lea

  12. 0:23

    dashboard in this case we'll just call

  13. 0:25

    it books currently the index is empty

  14. 0:28

    but when we add new data to the firebase

  15. 0:30

    database we want to see the index

  16. 0:31

    updated with that new data so if we add

  17. 0:34

    a new book to firebase then go back to

  18. 0:36

    al Golia we should see that book in the

  19. 0:38

    index now let's see how we make this

  20. 0:41

    possible with firebase cloud functions

  21. 0:43

    first initialize functions in your

  22. 0:45

    project then CD into the functions

  23. 0:47

    directory and install al kolya by

  24. 0:50

    running NPM install al Golia search then

  25. 0:53

    set your al Golia API keys in the

  26. 0:55

    functions environment you want to make

  27. 0:57

    sure to use the admin API key in the

  28. 1:01

    index.js file we start by importing the

  29. 1:03

    firebase admin and then initialize al

  30. 1:06

    Golia search with the API keys that we

  31. 1:08

    just said the name of the function will

  32. 1:11

    be update index and it will run anytime

  33. 1:14

    data is written to this node in the

  34. 1:16

    database which is books slash book ID we

  35. 1:19

    reference the index by calling and knit

  36. 1:21

    index then pass the name we define in

  37. 1:23

    the AL Golia dashboard then we set a

  38. 1:25

    variable for the data in the firebase

  39. 1:27

    database then we set the AL Golia object

  40. 1:31

    ID to whatever the book ID happens to be

  41. 1:33

    every record in your al Golia index is

  42. 1:36

    required to have a unique object ID in

  43. 1:38

    most cases you'd want to use the

  44. 1:40

    firebase push key you can add new data

  45. 1:42

    to the index by calling index dot save

  46. 1:45

    object and then pass it that data object

  47. 1:47

    then you have a callback from which you

  48. 1:49

    can catch the error or in this case we

  49. 1:51

    just console.log the success of the

  50. 1:53

    update so far the function will only

  51. 1:55

    work when data is created or updated so

  52. 1:58

    we check to see if the data variable is