Skip to content

Instantly share code, notes, and snippets.

@D-32
Created July 20, 2015 20:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save D-32/2dfde2bcf6f149a9ec09 to your computer and use it in GitHub Desktop.
Save D-32/2dfde2bcf6f149a9ec09 to your computer and use it in GitHub Desktop.
Firebase search service
var Firebase = require("firebase");
var lunr = require("lunr");
var http = require('http');
var url = require('url');
var index = lunr(function () {
this.field('title', {boost: 10})
this.field('description')
this.ref('id')
})
var ref = new Firebase("YOUR FIREBASE");
ref.child("topics").on("child_added", function(snapshot) {
ref.child("topics").child(snapshot.key()).on("value", function(snapshot) {
index.update({
id: snapshot.val().id,
title: snapshot.val().title,
description: snapshot.val().desc
});
console.log("updated index for " + snapshot.val().id);
});
});
ref.child("topics").on("child_removed", function(snapshot) {
index.remove({id:snapshot.key()});
console.log("removed index for " + snapshot.key());
});
http.createServer(function (req, res) {
var url_parts = url.parse(req.url, true);
var query = url_parts.query.q;
console.log("search query: "+query)
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(index.search(query)));
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment