Last active
May 4, 2017 12:00
-
-
Save meDavid/47901ff6242b1d5edd64f0eb6a5dc419 to your computer and use it in GitHub Desktop.
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
const functions = require('firebase-functions'); | |
const _ = require('lodash'); | |
const request = require('request-promise'); | |
exports.indexCarsToElastic = functions.database.ref('/cars/{carId}') | |
.onWrite(event => { | |
let carData = event.data.val(); | |
let carId = event.params.carId; | |
console.log('Indexing car ', carId, carData); | |
let elasticsearchFields = ['model','manufacturer','description','transmission_type','fuel_type','noise_level', | |
'euro_standard','year','co2','noise_level','urban_metric','extra_urban_metric','combined_metric']; | |
let elasticSearchConfig = functions.config().elasticsearch; | |
let elasticSearchUrl = elasticSearchConfig.url + 'cars/car/' + carId; | |
let elasticSearchMethod = carData ? 'POST' : 'DELETE'; | |
let elasticsearchRequest = { | |
method: elasticSearchMethod, | |
uri: elasticSearchUrl, | |
auth: { | |
username: elasticSearchConfig.username, | |
password: elasticSearchConfig.password, | |
}, | |
body: _.pick(carData, elasticsearchFields), | |
json: true | |
}; | |
return request(elasticsearchRequest).then(response => { | |
console.log('Elasticsearch response', response); | |
}) | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment