Created
July 20, 2015 20:09
-
-
Save D-32/2dfde2bcf6f149a9ec09 to your computer and use it in GitHub Desktop.
Firebase search service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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