Skip to content

Instantly share code, notes, and snippets.

@haxorgist
Last active November 7, 2018 23:58
Show Gist options
  • Select an option

  • Save haxorgist/06b5554b93a327c143cd52eb1a74a151 to your computer and use it in GitHub Desktop.

Select an option

Save haxorgist/06b5554b93a327c143cd52eb1a74a151 to your computer and use it in GitHub Desktop.
algolia-autocomplete
ALGOLIA_APP_ID=<algolia-app-id>
ALGOLIA_API_KEY=<algolia-api-key>
ALGOLIA_INDEX_NAME='algolia-index-name'
FIREBASE_DATABASE_URL=https://<my-firebase-database>.firebaseio.com
{
"rules": {
".read": true,
".write": false
}
}
[
{
"name":"Hack the 6ix",
"date": "August 24-26",
"city": "Toronto",
"country": "Canada",
"director": "John States"
},
{
"name":"HackMTY",
"date": "August 25-26",
"city": "Monterrey",
"country": "Mexico",
"director": "Sally Jane"
},
{
"name":"ByteHacks",
"date": "September 1-2",
"city": "New York city",
"country": "United States of America",
"director": "Jack Doe"
},
{
"name":"MedHacks",
"date": "September 7-9",
"city": "Baltimore",
"country": "United States of America",
"director": "Rachel Diaz"
}
]
const contactsRef = database.ref('/');
contactsRef.on('child_added', addOrUpdateIndexRecord);
contactsRef.on('child_changed', addOrUpdateIndexRecord);
contactsRef.on('child_removed', deleteIndexRecord);
function addOrUpdateIndexRecord(contact) {
// Get Firebase object
const record = contact.val();
// Specify Algolia's objectID using the Firebase object key
record.objectID = contact.key;
// Add or update object
index
.saveObject(record)
.then(() => {
console.log('Firebase object indexed in Algolia', record.objectID);
})
.catch(error => {
console.error('Error when indexing contact into Algolia', error);
process.exit(1);
});
}
function deleteIndexRecord(contact) {
// Get Algolia's objectID from the Firebase object key
const objectID = contact.key;
// Remove the object from Algolia
index
.deleteObject(objectID)
.then(() => {
console.log('Firebase object deleted from Algolia', objectID);
})
.catch(error => {
console.error('Error when deleting contact from Algolia', error);
process.exit(1);
});
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<style>
.algolia-autocomplete {
width: 100%;
}
.algolia-autocomplete .aa-input, .algolia-autocomplete .aa-hint {
width: 100%;
}
.algolia-autocomplete .aa-hint {
color: #999;
}
.algolia-autocomplete .aa-dropdown-menu {
width: 100%;
background-color: #fff;
border: 1px solid #999;
border-top: none;
}
.algolia-autocomplete .aa-dropdown-menu .aa-suggestion {
cursor: pointer;
padding: 5px 4px;
}
.algolia-autocomplete .aa-dropdown-menu .aa-suggestion.aa-cursor {
background-color: #B2D7FF;
}
.algolia-autocomplete .aa-dropdown-menu .aa-suggestion em {
font-weight: bold;
font-style: normal;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<form action="#" class="form">
<h3>Algolia API Tutorial</h3>
<input class="form-control" id="search-input" name="contacts" type="text" placeholder='Search for events...' />
</form>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/algoliasearch/3/algoliasearch.min.js"></script>
<script src="https://cdn.jsdelivr.net/autocomplete.js/0/autocomplete.min.js"></script>
<script>
var client = algoliasearch('YourApplicationID', 'YourAPIKey');
var index = client.initIndex('your_index_name');
autocomplete('#search-input', { hint: false }, [
{
source: autocomplete.sources.hits(index, { hitsPerPage: 5 }),
displayKey: 'name',
templates: {
suggestion: function(suggestion) {
return suggestion._highlightResult.name.value;
}
}
}
]).on('autocomplete:selected', function(event, suggestion, dataset) {
console.log(suggestion, dataset);
alert('dataset: ' + dataset + ', ' + suggestion.name);
});
</script>
</body>
</html>
const algoliasearch = require('algoliasearch');
const dotenv = require('dotenv');
const firebase = require('firebase');
// load values from the .env file in this directory into process.env
dotenv.load();
// configure firebase
firebase.initializeApp({
databaseURL: process.env.FIREBASE_DATABASE_URL,
});
const database = firebase.database();
// configure algolia
const algolia = algoliasearch(
process.env.ALGOLIA_APP_ID,
process.env.ALGOLIA_API_KEY
);
const index = algolia.initIndex(process.env.ALGOLIA_INDEX_NAME);
// Get all contacts from Firebase
database.ref('/').once('value', (contacts) => {
// Build an array of all records to push to Algolia
const records = [];
contacts.forEach(contact => {
// get the key and data from the snapshot
const childKey = contact.key;
const childData = contact.val();
// We set the Algolia objectID as the Firebase .key
childData.objectID = childKey;
// Add object for indexing
records.push(childData);
});
// Add or update new objects
index
.saveObjects(records)
.then(() => {
console.log('Contacts imported into Algolia');
})
.catch(error => {
console.error('Error when importing contact into Algolia', error);
process.exit(1);
});
});
{
"name": "algolia-autocomplete",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"algoliasearch": "^3.30.0",
"dotenv": "^6.1.0",
"firebase": "^5.5.7"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment