algoliasearch-client-node icon indicating copy to clipboard operation
algoliasearch-client-node copied to clipboard

saveObject problem

Open rodrigoreis22 opened this issue 11 years ago • 3 comments

When I call saveObject (without passing objectID) I'd expect the entity to be created per documentation. But a PUT call is being made: /1/indexes/dev_CHANNELS/undefined

My code:

function indexChannel(channel, callback) {
  var index = client.initIndex(prefix+'_CHANNELS');
  var indexedObj = {
    _id : channel._id,
    name : channel.name,
    thumb50x50 : channel.thumbnails && channel.thumbnails.small50x50.url,
    urlName : channel.urlName,
    subscribers : channel.statistics.subscribers,

  };

  if(channel.indexObjectId) {
    indexedObj.objectID = channel.indexObjectId;
  }
  console.log(indexedObj);

  index.saveObject(indexedObj, function(err, content) {
    if(err) {
      logger.error(err);
    }
    console.log(content);
    callback(err, content);
  });
}

rodrigoreis22 avatar Jan 24 '15 05:01 rodrigoreis22

Hi,

saveObject method require to have an objectID, if the object corresponding to this objectID does not exist in the index, it will be created. Auto-assignation of objectID is only handled in the addObject method and is really not recommended, this is useful to import a CSV really fast but will cause problem of duplicate for production usage. It is really better to specify the objectID = your unique identifier, so you can update/delete object easily.

Does it make sense?

Best Julien

speedblue avatar Jan 24 '15 09:01 speedblue

Oh got it. I didn't know I could specify my own objectID.. this makes things easier because I was saving the Algolia-generated objectID along with my entities for further updates.

When I read the documentation I thought I could leave the objectID field empty on the saveObject() and it would behave like addObject.

It's clear now.

Thanks,

rodrigoreis22 avatar Jan 24 '15 21:01 rodrigoreis22

Yes our goal is to make the usage as easy as possible! We will improve documentation to make it clear it is recommended to use your own ID :)

speedblue avatar Jan 25 '15 09:01 speedblue