0

I configured different firebase functions by following this

. Now in this, there is firebase full-text search. I tried to follow it but it seems to be incomplete. I have searched and somehow got success in deploying. But it is still not creating index in Algolia. Can someone tell me the steps to correctly perform this?

I created the blog-posts and search nodes in my firebase project but problem is still there.

CODE:

'use strict';

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

// Authenticate to Algolia Database.
// TODO: Make sure you configure the `algolia.app_id` and `algolia.api_key` Google Cloud environment variables.
const algoliasearch = require('algoliasearch');
const client = algoliasearch(functions.config().algolia.app_id, functions.config().algolia.api_key);

// Name fo the algolia index for Blog posts content.
const ALGOLIA_POSTS_INDEX_NAME = 'blogposts';

// Updates the search index when new blog entries are created or updated.
exports.indexentry = functions.database.ref('/blog-posts/{blogid}/text').onWrite(event => {
  const index = client.initIndex(ALGOLIA_POSTS_INDEX_NAME);
  const firebaseObject = {
    text: event.data.val(),
    objectID: event.params.blogid
  };

  return index.saveObject(firebaseObject).then(
      () => event.data.adminRef.parent.child('last_index_timestamp').set(
          Date.parse(event.timestamp)));
});

// Starts a search query whenever a query is requested (by adding one to the `/search/queries`
// element. Search results are then written under `/search/results`.
exports.searchentry = functions.database.ref('/search/queries/{queryid}').onWrite(event => {
  const index = client.initIndex(ALGOLIA_POSTS_INDEX_NAME);

  const query = event.data.val().query;
  const key = event.data.key;

  return index.search(query).then(content => {
    const updates = {
      '/search/last_query_timestamp': Date.parse(event.timestamp)
    };
    updates[`/search/results/${key}`] = content;
    return admin.database().ref().update(updates);
  });
});

SEE IMAGE OF FIREBASE NODE

Open Image

Your help will be appreciated. Thanks

Abdul Rehman Khan
  • 162
  • 1
  • 4
  • 23
  • Can you provide code or error messages that you are seeing? – Josh Dzielak Apr 18 '17 at 12:48
  • @JoshDzielak I received no error. I have successfully deoployed. My plan for firebase is Blaze The problem is why my algolia index is not created? I am currently using free plan for Algolia. I guess it has expired. But how to confirm it? – Abdul Rehman Khan Apr 18 '17 at 13:20
  • The Free plan doesn't expire as long as you've chosen it, if you can log in and look at the dashboard then it is available. The Blaze plan should let you make outbound network calls, as far as I know. I would suggest adding some logging to see what's going on, and also to make sure there is data in your Firebase at the correct node you are trying to index over. – Josh Dzielak Apr 18 '17 at 13:29
  • @JoshDzielak what if node does not exist? – Abdul Rehman Khan Apr 18 '17 at 13:31
  • @JoshDzielak check the code and image – Abdul Rehman Khan Apr 18 '17 at 14:27
  • @JoshDzielak when i use firebase functions:log command it gives the error like this "indexentry: undefined searchentry: undefined" – Abdul Rehman Khan Apr 18 '17 at 15:50

1 Answers1

2

So I used the sample code provided here and placed it into a Firebase cloud function. Writing to '/blog-posts/{blogid}/text' inside the database should index whatever value is under text to Algolia.

There are a few things that might be going wrong here:

  1. Check that your function is correctly placed into Firebase. You can do this from the console by clicking functions on the left side. You should see two functions named indexentry and searchentry. If you do not see those functions then you haven't correctly pushed your code to the Firebase cloud.

  2. If you code is in Firebase cloud then I recommend adding console.log("write on blog-posts fired"); to your searchentry function. Then write some more data to your database under '/blog-posts/{blogid}/text'. You can check the function log in the Firebase console. I have noticed a slight delay in log records displaying some times, so be patient if you don't see it right away. I'd write a few pieces of data to '/blog-posts/{blogid}/text' then after a couple minutes I'd check the log. If the log has "write on blog-posts fired" in it then you know the function is being activated when you write to the database.

  3. If all the above is operating correctly and you still don't have any data in Algolia then make sure you set your API keys. You can do this using the code firebase functions:config:set algolia.app_id="myAlgoliaAppId" algolia.api_key="myAlgoliaApiKey". You run this command in a terminal window inside the directory where you have your Firebase cloud functions. You can get you API keys by signing into your account. Remember not to share your API key with anyone.

DoesData
  • 6,594
  • 3
  • 39
  • 62