Created
September 24, 2016 19:24
-
-
Save lsneucamp/47ed761f3cc789313e48e0f0491c17f6 to your computer and use it in GitHub Desktop.
[Jobs] Firebase->Elasticsearch
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
/** | |
* | |
* Created by luciano on 9/18/16. | |
*/ | |
const Firebase = require('firebase'); | |
const UUID = require('node-uuid'); | |
const app = Firebase.initializeApp({ | |
// apiKey: '<your-api-key>', | |
// authDomain: '<your-auth-domain>', | |
// databaseURL: '<your-firebase-url>' | |
// storageBucket: '<your-storage-bucket>' | |
}); | |
client.ping({ | |
// ping usually has a 3000ms timeout | |
requestTimeout: Infinity, | |
// undocumented params are appended to the query string | |
hello: "elasticsearch!" | |
}, function (error) { | |
if (error) { | |
console.trace('elasticsearch cluster is down!'); | |
} else { | |
console.log('All is well'); | |
} | |
}); | |
var body = { | |
job:{ | |
properties:{ | |
title : {"type" : "string", "index" : "not_analyzed"}, | |
description : {"type" : "string", "index" : "not_analyzed"}, | |
location : {"type" : "string", "index" : "not_analyzed"}, | |
companyName : {"type" : "string", "index" : "not_analyzed"} | |
} | |
} | |
} | |
client.indices.putMapping({index:"jobs", type:"job", body:body}); | |
var esIndexer = function(key,body){ | |
client.index({ | |
index: 'jobs', | |
type: 'job', | |
id: key, | |
body: body | |
}, function (error, response) { | |
if(error) | |
console.error(error); | |
console.log(response) | |
}); | |
}; | |
app | |
.database() | |
.ref('jobs') | |
.once("value", function(snapshot) { | |
// The callback function will only get called once since we return true | |
snapshot.forEach(function(childSnapshot) { | |
esIndexer(UUID.v4(),childSnapshot.val()); | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment