Change Attribute Type

I have some JSON entered into Algolia however while it is a number algolia sees it as a string. IS it possible to change that attribute to be a number within Algolia’s Dashboard?

Hello,

It is not possible to type the fields through the dashboard. Algolia stores data in a schemaless fashion, meaning that you can send any JSON with any arbitrary data in it.

If you want to have a key as a number, you should send it as a number in the JSON (ie, no quotes around the value). If you’re using one of our official API clients, the typing should be done automatically.

Can I ask which language you’re using and maybe some code sample so we can help you better?

Hi I am currently coding in nodejs. The code I am using is as follows:

var dotenv = require('dotenv');
var firebaseAdmin = require("firebase-admin");
var algoliasearch = require('algoliasearch');

// load values from the .env file in this directory into process.env
dotenv.load();

var serviceAccount = require("./serviceAccountKey.json");
firebaseAdmin.initializeApp({
 credential: firebaseAdmin.credential.cert(serviceAccount),
 databaseURL: process.env.FIREBASE_DATABASE_URL
});
var database = firebaseAdmin.database();

var algolia = algoliasearch(process.env.ALGOLIA_APP_ID, process.env.ALGOLIA_API_KEY);
var index = algolia.initIndex('contacts');

var contactsRef = database.ref("/contacts");
contactsRef.once('value', initialImport);
function initialImport(dataSnapshot) {
 // Array of data to index
 var objectsToIndex = [];
// Get all objects
var values = dataSnapshot.val();
// Process each child Firebase object
dataSnapshot.forEach((function(childSnapshot) {
 // get the key and data from the snapshot
    var childKey = childSnapshot.key;
    var childData = childSnapshot.val();
   // Specify Algolia's objectID using the Firebase object key
   childData.objectID = childKey;
   // Add object for indexing
   objectsToIndex.push(childData);
 }))
 // Add or update new objects
   index.saveObjects(objectsToIndex, function(err, content) {
 if (err) {
  throw err;
   }
    console.log('Firebase<>Algolia import done');
    process.exit(0);
 });
}

Are you sure your data is saved as an integer in Firebase? You could try to log objectsToIndex before sending it to Algolia and check if your fields are correctly set as integers.

I’m 100% positive that if the API receives an integer, it will save it as an integer, so I think the issue lies before it is actually sent.

This is what the data looks like in firebase

"-L1KjJ81Vj0QcDT7RCiG" : 
{
    "Date": "475748948575"
}

and this is the code I use to get the value before it is pushed to firebase.

var d = Date.parse(input.dt);

input.dt = Wed, 10 Jan 2018 15:03:00 GMT <-- This is example of data

d is pushed to Date in firebase.

Can you see where my error as to why my number is being stored as a string and lastly how can I then change all the stored values to a number?

Looking for this all over the internet but can’t seem to find it

Hmm, I think I see what is going on.

The data from Firebase seems to have been saved as a string (you have " quotes around it). I don’t really know why it is saved as a string and not a number into Firebase, but if you want to send that as a number to Algolia, you’ll have to call parseInt on it to convert it.

In your example, this should be something like this:

dataSnapshot.forEach((function(childSnapshot) {
 // get the key and data from the snapshot
    var childKey = childSnapshot.key;
    var childData = childSnapshot.val();
   // Specify Algolia's objectID using the Firebase object key
   childData.objectID = childKey;
   // Converting the date to a number
   childData.Date = parseInt(childData.Date, 10);
   // Add object for indexing
   objectsToIndex.push(childData);
 }))

Does that make sense?

oh thanks yes this makes sense will try it a bit later.