Created
January 4, 2017 11:39
-
-
Save zammitjames/c56b713bc6fe095e92c1b96d9d656f5b to your computer and use it in GitHub Desktop.
Client Seach
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
////////////////////// BASE //////////////////////////// | |
var firebase = require('firebase'); | |
var express = require('express'); | |
var Queue = require('firebase-queue'); | |
var app = express(); | |
var admin = require("firebase-admin"); | |
// Initialisation | |
admin.initializeApp({ | |
credential: admin.credential.cert("service_account.json"), | |
databaseURL: "https://loc8-c03d0.firebaseio.com" | |
}); | |
// Firebase Database Instance | |
var db = admin.database(); | |
////////////////////// BASE //////////////////////////// | |
var options = { | |
'numWorkers': 1, | |
'sanitize': false | |
}; | |
// Listings Queue Ref Search | |
var listingRef = db.ref('queue/search/listings/requests'); | |
var specsRef = db.ref('queue/specs'); | |
var queue = new Queue({ tasksRef: listingRef, specsRef: specsRef }, options, function(data, progress, resolve, reject) { | |
// Search Text Queries | |
var startAtText = data.search_text.toLowerCase(); | |
var endAtText = data.search_text.toLowerCase() + '\uf8ff'; | |
// Define Results Response | |
var results = {}; | |
// Search By Tag | |
searchByTag(startAtText,endAtText,results,data,resolve,function(res) { | |
// Search By Category | |
searchByCategory(startAtText,endAtText,results,data,resolve,function(res) { | |
// Search By Listing Name | |
searchByTitle(startAtText,endAtText,results,data,resolve,function(res) { | |
// Set Response | |
db.ref("queue/search/listings/response/"+data._id).set(results); | |
resolve(data); | |
}); | |
}); | |
}); | |
}); | |
function searchByTitle(startAtText,endAtText,results,data,resolve,callback) { | |
db.ref("listings").orderByChild("canonical_title").startAt(startAtText).endAt(endAtText).limitToFirst(50).once("value",function(snapshot){ | |
snapshot.forEach(function(childSnapshot){ | |
addObject(results,childSnapshot.key,function(res) { | |
}); | |
}); | |
callback(0) | |
}); | |
} | |
function searchByCategory(startAtText,endAtText,results,data,resolve,callback) { | |
db.ref("categories").orderByChild("title").startAt(startAtText).endAt(endAtText).limitToFirst(50).once("value",function(snapshot){ | |
if (snapshot.val() == null) { | |
callback(0); | |
} else { | |
snapshot.forEach(function(childSnapshot) { | |
db.ref("additional/category_data/"+childSnapshot.key+"/listings").limitToLast(50).once("value",function(listings){ | |
listings.forEach(function(babySnapshot) { | |
// Check Results | |
addObject(results,babySnapshot.key,function(res){ | |
}); | |
}); | |
}); | |
}); | |
callback(0) | |
} | |
}); | |
} | |
function searchByTag(startAtText,endAtText,results,data,resolve,callback) { | |
db.ref("additional/tags_data").orderByKey().startAt(startAtText).endAt(endAtText).limitToFirst(50).once("value",function(snapshot){ | |
snapshot.forEach(function(childSnapshot) { | |
childSnapshot.forEach(function(babySnapshot) { | |
// Check Results | |
addObject(results,babySnapshot.key,function(res){ | |
}); | |
}); | |
}); | |
callback(0) | |
}); | |
} | |
function addObject(results,key,callback) { | |
if (results[key] == null) { | |
results[key] = true | |
} | |
callback(results); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment