Browse Source

Updates to fulltext-search sample.

ryanpbrewster-patch-1
Nicolas Garnier 9 years ago
parent
commit
61e064b12e
  1. 2
      README.md
  2. 32
      fulltext-search/README.md
  3. 26
      fulltext-search/index.js

2
README.md

@ -42,7 +42,7 @@ Copies Firebase Database elements into BigQuery automatically. This can be usefu
### [Full-text search via Algolia](/fulltext-search)
Enable full text search on firebase database elements by using an Algolia hosted search service.
Enable full-text search on firebase database elements by using an Algolia hosted search service.
## Contributing

32
fulltext-search/README.md

@ -10,28 +10,46 @@ The dependencies are listed in [package.json](package.json).
## Sample Database Structure
As an example we'll be using a simple logs database structure:
As an example we'll be using a simple blog structure:
```
/functions-project-12345
/logs
/blog-posts
/key-123456
text: "User signed in."
text: "This is my first blog entry..."
/key-123457
text: "Error: Could not connect to Database"
text: "This is my second blog entry..."
/search
/queries
/key-546789
query: "first entry"
/key-078234
query: "second entry"
/results
/key-546789
result: "first entry"
/key-078234
result: "second entry"
```
## Trigger rules
Below is the trigger rule for the `addtobigquery` function making sure it's triggered when a new log entry is added.
Below is the trigger rule for the `indexentry` function making sure it's triggered when a new log entry is added and for the `searchentry` function making sure it's triggered when a new search query is added.
```
"functions": {
".source": "functions",
"addtobigquery": {
"indexentry": {
"triggers": {
"database": {
"path": "/logs/$logid"
"path": "/blog-posts/$blogid"
}
}
}
"searchentry": {
"triggers": {
"database": {
"path": "/search/queries/$queryid"
}
}
}

26
fulltext-search/index.js

@ -22,40 +22,42 @@ var ref = new Firebase(env.get('firebase.database.url'), 'admin');
ref.auth(env.get('firebase.database.token'));
// Authenticate to Algolia Database.
// TODO: Make sure you add your Algolia Key and Secret into env.json
// TODO: Make sure you add your Algolia Key and Secret into the env.json file.
var algoliasearch = require('algoliasearch');
var client = algoliasearch(env.get('algolia.key'), env.get('algolia.secret'));
var index = client.initIndex('users');
ref.child("search").remove();
// Updates the search index when new blog entries are created or updated.
function index_entry(context, data) {
ref.child(data.path).once("value", function (dataSnapshot) {
ref.child(data.path).once('value', function (dataSnapshot) {
var firebaseObject = dataSnapshot.val();
firebaseObject.objectID = dataSnapshot.key();
index.saveObject(firebaseObject, function(err, content) {
if (err) throw err;
ref.child('last_index').set(Firebase.ServerValue.TIMESTAMP);
context.done();
index.saveObject(firebaseObject, function(error) {
if (error) {
context.done(error);
} else {
ref.child('last_index').set(Firebase.ServerValue.TIMESTAMP);
context.done();
}
});
});
}
function search_entry(context, data) {
ref.child('last_query').set(Firebase.ServerValue.TIMESTAMP);
ref.child(data.path).once("value", function (dataSnapshot) {
ref.child(data.path).once('value', function (dataSnapshot) {
var query = dataSnapshot.val(),
key = dataSnapshot.key();
index.search(query, function searchDone(err, content) {
ref.child("search/results").child(key).set(content);
ref.child('search/results').child(key).set(content);
context.done();
});
});
}
module.exports = {
"index-entry": index_entry,
"search-entry": search_entry
'indexentry': index_entry,
'searchentry': search_entry
}
Loading…
Cancel
Save